经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Redis » 查看文章
Spring Cache使用RedisCache案例解析
来源:jb51  时间:2019/10/24 9:01:50  对本文有异议

一、RedisCache使用演示

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

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

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

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <!--redis依赖-->
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-data-redis</artifactId>
  9. </dependency>

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

  1. server:
  2. port: 10900
  3.  
  4. spring:
  5. profiles:
  6. active: dev
  7. redis:
  8. host: localhost #redis服务器地址
  9. port: 6379 #redis端口
  10. password: 1234 #redis密码
  11. timeout: 60000 #连接超时时间
  12. database: 0 #数据库索引,默认为0

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

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

  1. @Configuration
  2. @EnableCaching
  3. public class CacheConfig {
  4.  
  5. @Bean
  6. public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
  7. RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
  8. redisTemplate.setConnectionFactory(connectionFactory);
  9.  
  10. // 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
  11. Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
  12.  
  13. ObjectMapper mapper = new ObjectMapper();
  14. mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  15. mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  16. serializer.setObjectMapper(mapper);
  17.  
  18. redisTemplate.setValueSerializer(serializer);
  19. // 使用StringRedisSerializer来序列化和反序列化redis的key值
  20. redisTemplate.setKeySerializer(new StringRedisSerializer());
  21. redisTemplate.afterPropertiesSet();
  22. return redisTemplate;
  23. }
  24. }

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

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

  1. @RestController
  2. @RequestMapping("cache")
  3. public class CacheApi {
  4.  
  5. @Autowired
  6. private CacheService cacheService;
  7.  
  8. @GetMapping("get")
  9. public User get(@RequestParam int id){
  10. return cacheService.get(id);
  11. }
  12.  
  13. @PostMapping("set")
  14. public User set(@RequestParam int id, @RequestParam String code, @RequestParam String name){
  15. User u = new User(code, name);
  16. return cacheService.set(id, u);
  17. }
  18.  
  19. @DeleteMapping("del")
  20. public void del(@RequestParam int id){
  21. cacheService.del(id);
  22. }
  23. }

CacheService缓存业务处理类,添加缓存,更新缓存和删除。

  1. @Slf4j
  2. @Service
  3. public class CacheService {
  4.  
  5. private Map<Integer, User> dataMap = new HashMap <Integer, User>(){
  6. {
  7. for (int i = 1; i < 100 ; i++) {
  8. User u = new User("code" + i, "name" + i);
  9. put(i, u);
  10. }
  11. }
  12. };
  13.  
  14. // 获取数据
  15. @Cacheable(value = "cache", key = "'user:' + #id")
  16. public User get(int id){
  17. log.info("通过id{}查询获取", id);
  18. return dataMap.get(id);
  19. }
  20.  
  21. // 更新数据
  22. @CachePut(value = "cache", key = "'user:' + #id")
  23. public User set(int id, User u){
  24. log.info("更新id{}数据", id);
  25. dataMap.put(id, u);
  26. return u;
  27. }
  28.  
  29. //删除数据
  30. @CacheEvict(value = "cache", key = "'user:' + #id")
  31. public void del(int id){
  32. log.info("删除id{}数据", id);
  33. dataMap.remove(id);
  34. }
  35. }

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号