Spring Cache使用RedisCache案例解析
一、RedisCache使用演示
Redis是一個(gè)key-value存儲系統(tǒng),在web應(yīng)用上被廣泛應(yīng)用,這里就不對其過多描述了。
本章節(jié)示例是在Spring Boot集成Spring Cache的源碼基礎(chǔ)上進(jìn)行改造。源碼地址:https://github.com/imyanger/springboot-project/tree/master/p20-springboot-cache
使用RedisCache作為緩存,我們先引入相關(guān)依賴。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--redis依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
然后SpringBoot配置文件中,對redis進(jìn)行配置。
server: port: 10900 spring: profiles: active: dev redis: host: localhost #redis服務(wù)器地址 port: 6379 #redis端口 password: 1234 #redis密碼 timeout: 60000 #連接超時(shí)時(shí)間 database: 0 #數(shù)據(jù)庫索引,默認(rèn)為0
SpringBoot中使用Redis,可以通過Spring Cache的注解,也可以使用RedisTemplate來實(shí)現(xiàn),大部分情況下,因?yàn)樽⒔獯嬖谝欢ň窒扌圆粔蜢`活,一般實(shí)際開發(fā)中都是使用的RedisTemplate。
附上CacheConfig注入RedisTemplate,如果不需要使用RedisTemplate,直接將@EnableCaching注解加在SpringBoot啟動類上即可。
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
// 使用Jackson2JsonRedisSerializer來序列化和反序列化redis的value值(默認(rèn)使用JDK的序列化方式)
Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
serializer.setObjectMapper(mapper);
redisTemplate.setValueSerializer(serializer);
// 使用StringRedisSerializer來序列化和反序列化redis的key值
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
這樣就可以開始使用RedisCache了,測試代碼與Spring Boot集成Spring Cache一致。
CacheApi接口調(diào)用類,方便調(diào)用進(jìn)行測試。
@RestController
@RequestMapping("cache")
public class CacheApi {
@Autowired
private CacheService cacheService;
@GetMapping("get")
public User get(@RequestParam int id){
return cacheService.get(id);
}
@PostMapping("set")
public User set(@RequestParam int id, @RequestParam String code, @RequestParam String name){
User u = new User(code, name);
return cacheService.set(id, u);
}
@DeleteMapping("del")
public void del(@RequestParam int id){
cacheService.del(id);
}
}
CacheService緩存業(yè)務(wù)處理類,添加緩存,更新緩存和刪除。
@Slf4j
@Service
public class CacheService {
private Map<Integer, User> dataMap = new HashMap <Integer, User>(){
{
for (int i = 1; i < 100 ; i++) {
User u = new User("code" + i, "name" + i);
put(i, u);
}
}
};
// 獲取數(shù)據(jù)
@Cacheable(value = "cache", key = "'user:' + #id")
public User get(int id){
log.info("通過id{}查詢獲取", id);
return dataMap.get(id);
}
// 更新數(shù)據(jù)
@CachePut(value = "cache", key = "'user:' + #id")
public User set(int id, User u){
log.info("更新id{}數(shù)據(jù)", id);
dataMap.put(id, u);
return u;
}
//刪除數(shù)據(jù)
@CacheEvict(value = "cache", key = "'user:' + #id")
public void del(int id){
log.info("刪除id{}數(shù)據(jù)", id);
dataMap.remove(id);
}
}
啟動服務(wù)進(jìn)行測試,可以看到緩存功能正常,且打開redis進(jìn)行查看,也能看到對應(yīng)的緩存數(shù)據(jù)。
源碼地址:https://github.com/imyanger/springboot-project/tree/master/p21-springboot-cache-redis
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot配置lombok與logback過程解析
這篇文章主要介紹了SpringBoot配置lombok與logback過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
基于mybatis-plus QueryWrapper 排序的坑
這篇文章主要介紹了mybatis-plus QueryWrapper 排序的坑,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
Java基于socket服務(wù)實(shí)現(xiàn)UDP協(xié)議的方法
這篇文章主要介紹了Java基于socket服務(wù)實(shí)現(xiàn)UDP協(xié)議的方法,通過兩個(gè)簡單實(shí)例分析了java通過socket實(shí)現(xiàn)UDP發(fā)送與接收的技巧,需要的朋友可以參考下2015-05-05
詳解SpringBoot實(shí)現(xiàn)JPA的save方法不更新null屬性
直接調(diào)用原生Save方法會導(dǎo)致null屬性覆蓋到數(shù)據(jù)庫,使用起來十分不方便。本文詳細(xì)的介紹了如何解決這個(gè)問題,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-12-12
詳解java一維數(shù)組及練習(xí)題實(shí)例
在本篇文章里小編給大家整理了關(guān)于java一維數(shù)組及練習(xí)題的相關(guān)知識點(diǎn)和實(shí)例代碼,有需要的朋友們跟著學(xué)習(xí)下。2019-07-07
SpringBoot連接Hive實(shí)現(xiàn)自助取數(shù)的示例
這篇文章主要介紹了SpringBoot連接Hive實(shí)現(xiàn)自助取數(shù)的示例,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-12-12

