• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Spring Cache使用RedisCache案例解析

java 搞代码 4年前 (2022-01-05) 44次浏览 已收录 0个评论

这篇文章主要介绍了Spring Cache使用RedisCache案例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

一、RedisCache使用演示

Redis是一个key-value存储系统,在web应用上被广泛应用,这里就不对其过多描述了。

本章节示例是在Spring Boot集成Spring Cache的源码基础上进行改造。源码地址:https://github.com/imyanger/springboot-project/tree/master/p20-springboot-cache

使用RedisCache作为缓存,我们先引入相关依赖。

  org.springframework.bootspring-boot-starter-web<!--redis依赖--> org.springframework.bootspring-boot-starter-data-redis

然后SpringBoot配置文件中,对redis进行配置。

 server: port: 10900 spring: profiles: active: dev redis: host: localhost #redis服务器地址 port: 6379 #redis端口 password: 1234 #redis密码 timeout: 60000 #连接超时时间 database: 0 #数据库索引,默认为0

SpringBoot中使用Redis,可以通过Spring Cache的注解,也可以使用RedisTemplate来实现,大部分情况下,因为注解存在一定局限性不够灵活,一般实际开发中都是使用的RedisTemplate。

附上CacheConfig注入RedisTemplate,如果不需要使用RedisTemplate,直接将@EnableCaching注解加在SpringBoot启动类上即可。

 @Configuration @EnableCaching public class CacheConfig { @Bean public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate redisTemplate = new RedisTemplate(); redisTemplate.setConnectionFactory(connectionFactory); // 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式) Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper mapper = new ObjectMapper(); mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL<div style="color:transparent">来源gaodai.ma#com搞##代!^码@网</div>); serializer.setObjectMapper(mapper); redisTemplate.setValueSerializer(serializer); // 使用StringRedisSerializer来序列化和反序列化redis的key值 redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate; } }

这样就可以开始使用RedisCache了,测试代码与Spring Boot集成Spring Cache一致。

CacheApi接口调用类,方便调用进行测试。

 @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缓存业务处理类,添加缓存,更新缓存和删除。

 @Slf4j @Service public class CacheService { private Map dataMap = new HashMap (){ { for (int i = 1; i 

启动服务进行测试,可以看到缓存功能正常,且打开redis进行查看,也能看到对应的缓存数据。

源码地址:https://github.com/imyanger/springboot-project/tree/master/p21-springboot-cache-redis

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是Spring Cache使用RedisCache案例解析的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Spring Cache使用RedisCache案例解析

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址