SpringCache如何与Redis缓存结合实现高效数据缓存?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1142个文字,预计阅读时间需要5分钟。
plaintext整合缓存功能,引入依赖:groupId: org.springframework.bootartifactId: spring-boot-starter-cachegroupId: org.springframework.bootartifactId: spring-boot-starter-data-redis
整合&体验@Cacheable
引入依赖
<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>自动配置 使用redis
application.properties 设置 spring.cache.type=redis缓存注解
@Cacheable triggers cache population //触发将数据保存到缓存的操作 @CacheEvict triggers cache eviction //触发将数据从缓存删除的操作 @CachePut updates the cache without interfering with the method execution //不影响方法执行更新缓存 @Caching regroups multiple cache operations to be applied on a method //组合以上多个操作 @CacheConfig shares some common cache-related settings at class-level 在类级别共享缓存的相同配置 使用缓存 开启缓存注解 并使用注解 @EnableCaching 启动程序 @Cacheable({"category"}) @Override 实现类 //每一个需要缓存的数据我们都来指定要放到那个名字的缓存。[缓存的分区 (按照业务类型分)] //代表当前方法的结果需要缓存,如果缓存中有,方法不用调用。如果缓存中没有,会调用方法,最后将方法的结果放入缓存自定义缓存 SPEL 语法
指定生成的缓存使用的key -》 key属性指定 接受一个sqel 指定缓存的数据的存话时间 配置文件中设置失效时间 -》spring.cache.redis.time-to-live=360000 //毫秒 将数据保存为Json格式 @Cacheable(value = {"category"},key ="#root.method.name" ) //key ="'level1Categorys'" CacheAutoConfiguration->RedisCacheConfiguration 自动配置->RedisCacheManager->初始化缓存-> ->初始化所有的缓存-)每个缓存决定使用什么配置- ->如果redisCacheConfiguration有就用已有的,没有就用默认配置 ->想改缓存的配置,只需要给容器中放一-个RedisCacheConfiguration即可 ->就会应用到当前RedisCacheManager管理的所有缓存分区中
redis 客户端倒计时TTL 不生效 自定义配置
MyCacheConfig
配置规则
spring.cache.type=redis #缓存过期时 毫秒单位 spring.cache.redis.time-to-live=3600000 #开启缓存前缀 不指定默认使用缓存名字 spring.cache.redis.key-prefix=CAHCE_ spring.cache.redis.use-key-prefix=false #是否缓存空数,防止缓存穿透 spring.cache.redis.cache-null-values=true #规定规范 #开启缓存前缀 不指定默认使用缓存名字 #spring.cache.redis.key-prefix=CAHCE_ spring.cache.redis.use-key-prefix=true 层次分明 //@CacheEvict 删除缓存 CacheEvict 删除缓存 并指定删除哪个 @CacheEvict(value = {"category"}, key = "'getLevel1Categorys'") @Override //同时进行多种缓存操作 @Caching(evict = { @CacheEvict(value = {"category"}, key = "'getLevel1Categorys'"), @CacheEvict(value = {"category"}, key = "'getCatalogJson'") }) 或 @CacheEvict(value = "category", allEntries = true) //删除分区下面的所有缓存springcache不足
缓存穿透:查询-个nul数据。解决:缓存空数据; 解决ache- null-values=true 缓存击穿:大量并发进来同时查询一个正好过期的数据。解决:加锁; ? 默认不加锁 sync = true @Cacheable(value = {"category"}, key = "#root.method.name",sync = true) 缓存雪崩:大量的key同时过期。解决:加随机时间。加上过期时间 解决spring.cache.redis.time-to-live=3600000代码
//远程调用加入缓存 当前被调用的方法的参数列表 @Cacheable(value = "attr",key = "'attrinfo:'+#root.args[0]") @Override public AttrRespVo getAttrInfo(Long attrId) //同时进行多种缓存操作 //@Caching(evict = { // @CacheEvict(value = {"category"}, key = "'getLevel1Categorys'"), // @CacheEvict(value = {"category"}, key = "'getCatalogJson'") //}) @CacheEvict(value = "category", allEntries = true) //删除分区下面的所有缓存 @Transactional //事务 @Override public void updateCascade(CategoryEntity category) //每一个需要缓存的数据我们都来指定要放到那个名字的缓存。[缓存的分区 (按照业务类型分)] //代表当前方法的结果需要缓存,如果缓存中有,方法不用调用。如果缓存中没有,会调用方法,最后将方法的结果放入缓存 @Cacheable(value = {"category"}, key = "#root.method.name",sync = true) //key ="'level1Categorys'" @Override public List<CategoryEntity> getLevel1Categorys()本文共计1142个文字,预计阅读时间需要5分钟。
plaintext整合缓存功能,引入依赖:groupId: org.springframework.bootartifactId: spring-boot-starter-cachegroupId: org.springframework.bootartifactId: spring-boot-starter-data-redis
整合&体验@Cacheable
引入依赖
<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>自动配置 使用redis
application.properties 设置 spring.cache.type=redis缓存注解
@Cacheable triggers cache population //触发将数据保存到缓存的操作 @CacheEvict triggers cache eviction //触发将数据从缓存删除的操作 @CachePut updates the cache without interfering with the method execution //不影响方法执行更新缓存 @Caching regroups multiple cache operations to be applied on a method //组合以上多个操作 @CacheConfig shares some common cache-related settings at class-level 在类级别共享缓存的相同配置 使用缓存 开启缓存注解 并使用注解 @EnableCaching 启动程序 @Cacheable({"category"}) @Override 实现类 //每一个需要缓存的数据我们都来指定要放到那个名字的缓存。[缓存的分区 (按照业务类型分)] //代表当前方法的结果需要缓存,如果缓存中有,方法不用调用。如果缓存中没有,会调用方法,最后将方法的结果放入缓存自定义缓存 SPEL 语法
指定生成的缓存使用的key -》 key属性指定 接受一个sqel 指定缓存的数据的存话时间 配置文件中设置失效时间 -》spring.cache.redis.time-to-live=360000 //毫秒 将数据保存为Json格式 @Cacheable(value = {"category"},key ="#root.method.name" ) //key ="'level1Categorys'" CacheAutoConfiguration->RedisCacheConfiguration 自动配置->RedisCacheManager->初始化缓存-> ->初始化所有的缓存-)每个缓存决定使用什么配置- ->如果redisCacheConfiguration有就用已有的,没有就用默认配置 ->想改缓存的配置,只需要给容器中放一-个RedisCacheConfiguration即可 ->就会应用到当前RedisCacheManager管理的所有缓存分区中
redis 客户端倒计时TTL 不生效 自定义配置
MyCacheConfig

