前言
之前已經(jīng)寫(xiě)過(guò)一篇文章介紹SpringBoot整合Spring Cache,SpringBoot默認(rèn)使用的是ConcurrentMapCacheManager,在實(shí)際項(xiàng)目中,我們需要一個(gè)高可用的、分布式的緩存解決方案,使用默認(rèn)的這種緩存方式,只是在當(dāng)前進(jìn)程里緩存了而已。Spring Cache整合Redis來(lái)實(shí)現(xiàn)緩存,其實(shí)也不是一件復(fù)雜的事情,下面就開(kāi)始吧。
關(guān)于Spring Cache的運(yùn)用,請(qǐng)參考[快學(xué)SpringBoot]快速上手好用方便的Spring Cache緩存框架
新建一個(gè)SpringBoot項(xiàng)目
依賴(lài)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
主要是最下面兩個(gè)依賴(lài):spring-boot-starter-cache 和 spring-boot-starter-data-redis。
配置Redis
在application.properties中添加redis的配置
spring.redis.host=127.0.0.1
# spring.redis.password=
spring.redis.port=6379
這是最基礎(chǔ)的三個(gè)配置(其實(shí)默認(rèn)值就是這樣,就算不寫(xiě)也可以)。當(dāng)然,還有空閑連接數(shù),超時(shí)時(shí)間,最大連接數(shù)等參數(shù),我這里都沒(méi)有設(shè)置,在生產(chǎn)項(xiàng)目中,根據(jù)實(shí)際情況設(shè)置。
RedisCacheConfig
新建RedisCacheConfig.class
@Configuration
@EnableCaching
public class RedisCacheConfig {
/**
* 緩存管理器
*/
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
//初始化一個(gè)RedisCacheWriter
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
//設(shè)置CacheManager的值序列化方式為json序列化
RedisSerializer<Object> jsonSerializer = new GenericJackson2JsonRedisSerializer();
RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair
.fromSerializer(jsonSerializer);
RedisCacheConfiguration defaultCacheConfig=RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(pair);
return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
}
}
如果要設(shè)置緩存管理器所管理的緩存名字,RedisCacheManager構(gòu)造方法提供一個(gè)可變參數(shù)的構(gòu)造器:
測(cè)試
新建一個(gè)MockService.java,代碼如下:
@Service
public class MockService {
/**
* value 緩存的名字,與cacheName是一個(gè)東西
* key 需要緩存的鍵,如果為空,則會(huì)根據(jù)參數(shù)自動(dòng)拼接
* 寫(xiě)法:SpEL 表達(dá)式
*/
@Cacheable(value = "listUsers", key = "#username")
public List<String> listUsers(String username) {
System.out.println("執(zhí)行了listUsers方法");
return Arrays.asList("Happyjava", "Hello-SpringBoot", System.currentTimeMillis() + "");
}
}
新建一個(gè)TestController.java,代碼如下:
@RestController
public class TestController {
private final MockService mockService;
public TestController(MockService mockService) {
this.mockService = mockService;
}
@GetMapping(value = "/listUsers")
public Object listUsers(String username) {
return mockService.listUsers(username);
}
}
請(qǐng)求接口:
Redis中的數(shù)據(jù):
原創(chuàng)聲明
本文發(fā)布于掘金號(hào)【Happyjava】。Happy的掘金地址:https://juejin.im/user/5cc2895df265da03a630ddca,Happy的個(gè)人博客:http://blog.happyjava.cn。歡迎轉(zhuǎn)載,但須保留此段聲明。
關(guān)注公眾號(hào)領(lǐng)資料
搜索公眾號(hào)【Happyjava】,回復(fù)【電子書(shū)】和【視頻】,即可獲取大量?jī)?yōu)質(zhì)電子書(shū)和大數(shù)據(jù)、kafka、nginx、MySQL等視頻資料