irpas技术客

SpringCache_最好束手就擒_springcache

大大的周 3350

Spring 从3.1 开始定义了org.springframework.cache.Cache 和org.springframework.cache.CacheManager 接口来统一不同的缓存技术; 并支持使用JCache(JSR-107)注解简化我们开发;

Cache 接口为缓存的组件规范定义,包含缓存的各种操作集合; Cache 接口下Spring 提供了各种xxxCache 的实现; 如RedisCache , EhCacheCache , ConcurrentMapCache 等;

每次调用需要缓存功能的方法时,Spring 会检查检查指定参数的指定的目标方法是否已 经被调用过;如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法并缓 存结果后返回给用户。下次调用直接从缓存中获取。

CacheManager 缓存管理器 public interface CacheManager { //根据缓存名字获取缓存 @Nullable Cache getCache(String name); //获取管理的所有缓存的名字 Collection<String> getCacheNames(); }

支持多种类型的缓存

整合SpringCache <!-- 引入spring-boot-starter-cache --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>

这里我们使用redis作为缓存

<!-- 引入redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <exclusions> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency> <!-- jedis不写版本springboot控制 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> 配置spring-boot-starter-cache

1.缓存的自动配置 CacheProperties封装了配置文件中可以配置的属性

@ConfigurationProperties(prefix = "spring.cache") org.springframework.boot.autoconfigure.cache.CacheProperties

CacheConfiguration会根据缓存类型导入RedisCacheConfiguration

RedisCacheConfiguration自动配好了缓存管理器

@Bean public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory, ResourceLoader resourceLoader) { RedisCacheManagerBuilder builder = RedisCacheManager.builder(redisConnectionFactory) .cacheDefaults(determineConfiguration(resourceLoader.getClassLoader())); List<String> cacheNames = this.cacheProperties.getCacheNames(); if (!cacheNames.isEmpty()) { builder.initialCacheNames(new LinkedHashSet<>(cacheNames)); } return this.customizerInvoker.customize(builder.build()); } 手动配置

配置使用Redis作为缓存

spring: cache: #cache-names: 可以自动配置 type: redis 测试使用缓存

常用注解

1.开启缓存功能 @Cacheable({"…"})

在启动类添加@EnableCaching 注解,不需要重复配Redis

@EnableCaching 注解

2.使用注解 就能完成缓存操作

//每一个需要缓存的数据都需要指定要放到哪个名字的缓存,缓存的分区,按照业务类型分 @Cacheable({"categroy"}) //代表方法的结果需要缓存 ,如果缓存中有 方法都不用调用,如果缓存中没有,调用方法,将结果放入缓存 @Override public List<CategoryEntity> getLevel1Categorys() { long l = System.currentTimeMillis(); List<CategoryEntity> categoryEntityList = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", 0)); System.out.println("消耗时间:" + (System.currentTimeMillis() - l)); return categoryEntityList; }

使用@Cacheable 默认行为 如果缓存中有缓存 方法都不用调用 key值 自动生成 缓存的Value值默认使用JDK序列化机制,将序列化后的数据存到Redis 默认过期时间TTL -1 永不过去

应该要指定缓存存活时间 在配置文件中修改

cache: #cache-names: 可以自动配置 type: redis redis: time-to-live: 3600000 #设置存活时间毫秒 key-prefix: CACHE_ #key前缀 如果制定了前缀就用指定的前缀,如果没有就默认使用缓存的名字作为前缀 use-key-prefix: true # 是否使用前缀 cache-null-values: true # 是否缓存控制 解决缓存穿透

应该要指定缓存的Key 使用key属性 接收SpEL表达式

@Cacheable(value = {"categroy"},key = "#root.method.name") @Cacheable(value = {"categroy"},key = "'name'")

SpEL语法

将数据保存为JSON格式

CacheConfiguration —> RedisCacheConfiguration —> 自动配置了RedisCacheManager —> 初始化所有的缓存 —> 每个缓存决定使用什么配置,配置有就用配置的,配置没有就用默认的(key的前缀等)—>想要修改配置只需要在容器中放一个RedisCacheConfiguration 即可 —> 这个容器就会应用到当前RedisCacheManager 管理的所有缓存分区中

package cn.cloud.xmall.product.config; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * @Description: ··· * @author: Freedom * @QQ: 1556507698 * @date:2022/3/17 13:26 */ @EnableCaching @Configuration public class MyCacheConfig { @Bean RedisCacheConfiguration redisCacheConfiguration(){ RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig(); //config = config.entryTtl(); //key的序列化 config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())); //值的序列化 config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); return config; }; }

问题: 配置文件中的东西没有用上 原因 : 使用我们自己的配置不会从配置文件中取出来配置

package cn.cloud.xmall.product.config; import org.springframework.boot.autoconfigure.cache.CacheProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * @Description: ··· * @author: Freedom * @QQ: 1556507698 * @date:2022/3/17 13:26 */ @EnableConfigurationProperties(CacheProperties.class) @Configuration @EnableCaching public class MyCacheConfig { // @Autowired // public CacheProperties cacheProperties; /** * 配置文件的配置没有用上 * @return */ @Bean public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) { RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig(); // config = config.entryTtl(); //设置Key的序列化方式 config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())); //设置值的序列化方式 config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); 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; } } 从缓存中删除数据 @CacheEvict 失效模式 @CacheEvict(value = "category",key = "'getLevel1Categorys'")

分区value和key必须相同才可以删除

问题: 如果一个修改需要删除两个缓存?如何删除? @CaChing 组合操作,同时进行多个缓存操作

@Caching(evict = { @CacheEvict(value = "category",key = "'getLevel1Categorys'"), @CacheEvict(value = "category",key = "'getCatalogJson'") }) @Transactional @Override public void updateCascade(CategoryEntity category) { this.updateById(category); //更新关联表 categoryBrandRelationService.updateCategory(category.getCatId(), category.getName()); }

成功删除

第二种方式: 删除指定分区下所有的缓存,存储统一个类型的数据可以指定为同一个分区,分区名默认就是缓存前缀

@CacheEvict(value = {"category"},allEntries = true)

推荐使用@CaChing

@CaChePut 双写模式使用此注解(解决分布式缓存一致性问题)

Spring-Cache不足 读模式

缓存穿透:查询一个null数据 解决:缓存null数据,并设置短暂过期时间, cache-null-values: true # 是否缓存控制 解决缓存穿透, springcache解决方案

缓存击穿:大量并发进来同时查询一个正好过期的数据 解决:加锁 ??? 默认情况下是没有加锁的 1.手写添加缓存方法使用Redisson作为分布式锁 2.@Cacheable(value = {“categroy”},key = “#root.method.name”,sync = true),sync只有Cacheable有这个属性 会进入加锁的方法

缓存雪崩: 大量的key同时过期 解决:添加随机时间,指定过期时间 ,time-to-live: 3600000 #设置存活时间毫秒

写模式 (缓存与数据库一致)

读写加锁

引入Canal,感知数据库更新去更新缓存数据

读多写多直接去数据库查询

常规数据的缓存(读多,写少,缓存一致性要求不高)就算没有锁,完全可以使用SpringCache, 但是要求数据高的特殊数据,即想要缓存提升他的速度,又想要缓存一致性,特殊设计,读写锁…Canal…一致性。常规数据只要有缓存的过期时间就可以满足需求了


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #SpringCache #Spring #从31 #Cache #接口下Spring