当前位置:   article > 正文

spring-cache学习_spring.cache.redis.time-to-live

spring.cache.redis.time-to-live

(分区)

常用注解

  • @Cacheable :触发将数据保存到缓存的操作;
  • @CacheEvict : 触发将数据从缓存删除的操作;(清除模式)
  • @CachePut :不影响方法执行更新缓存;(双写模式:在修改后返回要存在缓存的数据,再次更新缓存)
  • @Cacheing:组合以上多个操作;(清除多个缓存)
  • @CacheConfig:在类级别共享缓存的相同配置;

案例

  1. 缓存一般适用于 热点数据和业务代码写的比较水的程序员(四级类目用递归和for循环实现每次都进行重复查询)
  2. //缓存redis key值是以方法名加两个参数形成
  3. @Cacheable(value = {"category"}, key = "#root.method.name+#type+#status", sync = true)
  4. public String selectCatalogue(int type,int status)
  5. //删除缓存 在增删改的方法上面需要都进行加注解清除 适用于一些不经常变化的数据
  6. @CacheEvict(value ={"category"},key ="'selectCatalogue'+#catalogue.type+#catalogue.status")
  7. public String updateCatalogue(PositionCatalogue catalogue)
  8. Map<String,List<Catalogue>> map=new HashMap<>();
  9. 也可以通过map将数据缓存到内存实现缓存

最终实现

pom.xml

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. <version>2.3.5.RELEASE</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>redis.clients</groupId>
  8. <artifactId>jedis</artifactId>
  9. <version>2.9.0</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-cache</artifactId>
  14. </dependency>

yml文件

  1. #指定接入缓存的类型
  2. spring.cache.type=redis
  3. #指定存活时间
  4. spring.cache.redis.time-to-live=3600000
  5. #如果指定了前缀就用我们指定的前缀,如果没有就默认使用缓存的名字作为前缀
  6. #spring.cache.redis.key-prefix=CACHE_
  7. #是否自动分区形成前缀
  8. #spring.cache.redis.use-key-prefix=true
  9. #是否缓存空值,防止缓存穿透 缓存不存在的值
  10. spring.cache.redis.cache-null-values=true

其他知识

  1. /**
  2. * 1、每一个需要缓存的数据我们都来指定要放到那个名字的缓存。【缓存的分区(按照业务类型分)】
  3. * 2、@Cacheable 代表当前方法的结果需要缓存,如果缓存中有,方法都不用调用,如果缓存中没有,会调用方法。最后将方法的结果放入缓存
  4. * 3、默认行为
  5. * 3.1 如果缓存中有,方法不再调用
  6. * 3.2 key是默认生成的:缓存的名字::SimpleKey::[](自动生成key值)
  7. * 3.3 缓存的value值,默认使用jdk序列化机制,将序列化的数据存到redis中
  8. * 3.4 默认时间是 -1:永不过期
  9. *
  10. * 自定义操作:key的生成
  11. * 1. 指定生成缓存的key:key属性指定,接收一个 SpEl
  12. * 2. 指定缓存的数据的存活时间:配置文档中修改存活时间 ttl
  13. * 3. 将数据保存为json格式: 自定义配置类 MyCacheManager
  14. * <p>
  15. * 4、Spring-Cache的不足之处:
  16. * 1)、读模式
  17. * 缓存穿透:查询一个null数据。解决方案:缓存空数据
  18. * 缓存击穿:大量并发进来同时查询一个正好过期的数据。解决方案:加锁 ? 默认是无加锁的;使用sync = true来解决击穿问题
  19. * 缓存雪崩:大量的key同时过期。解决:加随机时间。加上过期时间
  20. * 2)、写模式:(缓存与数据库一致)
  21. * 1)、读写加锁。
  22. * 2)、引入Canal,感知到MySQL的更新去更新Redis
  23. * 3)、读多写多,直接去数据库查询就行
  24. * <p>
  25. * 总结:
  26. * 常规数据(读多写少,即时性,一致性要求不高的数据,完全可以使用Spring-Cache):写模式(只要缓存的数据有过期时间就足够了)
  27. * 特殊数据:特殊设计
  28. * <p>
  29. * 原理:
  30. * CacheManager(RedisCacheManager)->Cache(RedisCache)->Cache负责缓存的读写
  31. *
  32. * @return
  33. */
  34. 举例
  35. @Cacheable(value = {"category"}, key = "#root.method.name", sync = true)

自定义RedisCacheConfiguration 将缓存的数据序列化成json格式

  1. @EnableConfigurationProperties(CacheProperties.class)
  2. @Configuration
  3. @EnableCaching
  4. public class SpringbootRedisConfiguration {
  5. /**
  6. * 配置文件的配置没有用上
  7. * 1. 原来和配置文件绑定的配置类为:@ConfigurationProperties(prefix = "spring.cache")
  8. * public class CacheProperties
  9. * <p>
  10. * 2. 要让他生效,要加上 @EnableConfigurationProperties(CacheProperties.class)
  11. */
  12. @Bean
  13. public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
  14. RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
  15. //修改序列化机制
  16. config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
  17. config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
  18. CacheProperties.Redis redisProperties = cacheProperties.getRedis();
  19. //将配置文件中所有的配置都生效
  20. if (redisProperties.getTimeToLive() != null) {
  21. config = config.entryTtl(redisProperties.getTimeToLive());
  22. }
  23. if (redisProperties.getKeyPrefix() != null) {
  24. config = config.prefixKeysWith(redisProperties.getKeyPrefix());
  25. }
  26. if (!redisProperties.isCacheNullValues()) {
  27. config = config.disableCachingNullValues();
  28. }
  29. if (!redisProperties.isUseKeyPrefix()) {
  30. config = config.disableKeyPrefix();
  31. }
  32. return config;
  33. }
  34. }

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/389709
推荐阅读
相关标签
  

闽ICP备14008679号