当前位置 : 主页 > 编程语言 > java >

redis缓存-SpringCache-简介

来源:互联网 收集:自由互联 发布时间:2023-03-22
整合体验@Cacheable 引入依赖 dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-cache/artifactId/dependencydependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-data-redis/arti

image.png

整合&体验@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

//CacheProperties.class 生效 配置redisProperties 开启功能 @EnableConfigurationProperties(CacheProperties.class) @EnableCaching @Configuration public class MyCacheConfig { @Bean RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties){ //改造 覆蓋原來的 RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig(); //传入序列化器K V config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())); // GenericJackson2JsonRedisSerializer转换json序列化 config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); //装配CacheProperties 获取redist 使下面生效 CacheProperties.Redis redisProperties = cacheProperties.getRedis(); if (redisProperties.getTimeToLive() != null) { config = config.entryTtl(redisProperties.getTimeToLive()); } if (redisProperties.getKeyPrefix() != null) { config = config.prefixKeysWith(redisProperties.getKeyPrefix()); } if (!redisProperties.isCacheNullValues()) { config = config.disableCachingNullValues(); } if (!redisProperties.isUseKeyPrefix()) { config = config.disableKeyPrefix(); } return config; } }

配置规则

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 层次分明

image.png

//@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()
上一篇:JVM系统优化实践(10):G1混合回收
下一篇:没有了
网友评论