网站建设与推广实训心得,推广赚钱群,网站建设中页面模板下载,oa办公系统软件哪家好前言 SpringBoot的众多Starter有两个很重要的缓存Starter#xff0c;其中一个是我们经常用到的Redis#xff08;spring-boot-starter-data-redis#xff09;还有一个是 spring-boot-starter-cache。 今天主要是简单介绍一个如何整合这两个组件#xff0c;达到相互合作的关系…前言 SpringBoot的众多Starter有两个很重要的缓存Starter其中一个是我们经常用到的Redisspring-boot-starter-data-redis还有一个是 spring-boot-starter-cache。 今天主要是简单介绍一个如何整合这两个组件达到相互合作的关系。
开始
引入相关的依赖 这里redis需要注意几点详细可以参考SpringBoot整合Redis要注意的那些 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId
/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-cache/artifactId
/dependency!-- lettuce默认连接池使用 common-pool2 --
dependencygroupIdorg.apache.commons/groupIdartifactIdcommons-pool2/artifactId
/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-configuration-processor/artifactIdoptionaltrue/optional
/dependencyyml配置
spring: redis:host: 127.0.0.1port: 6379database: 0password: 123456# 连接超时时间timeout: 10slettuce:pool:# 连接池中的最小空闲连接min-idle: 0# 连接池中的最大空闲连接max-idle: 8# 连接池的最大数据库连接数max-active: 8# #连接池最大阻塞等待时间使用负值表示没有限制max-wait: -1ms配置类
Configuration
EnableCaching
RequiredArgsConstructor
public class CacheConfiguration implements CachingConfigurer {/*** redis连接工厂* 由于使用的lettuce* 所以由LettuceConnectionFactory创建*/private final RedisConnectionFactory factory;/*** 此处使用的lettuce连接池* factory初始化位置在 LettuceConnectionConfiguration*/Beanpublic RedisTemplateObject, Object redisTemplate() {RedisTemplateObject, Object template new RedisTemplate();template.setConnectionFactory(factory);// 序列化器template.setValueSerializer(serializer());// 使用StringRedisSerializer来序列化和反序列化redis的key值template.setKeySerializer(new StringRedisSerializer());template.afterPropertiesSet();return template;}/*** 缓存管理器* - 使用redis做cache缓存注解管理* - 修改原生的序列化方式改为json方式*/Overridepublic CacheManager cacheManager() {RedisCacheConfiguration cacheConfiguration RedisCacheConfiguration.defaultCacheConfig().disableCachingNullValues().serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(serializer()));return RedisCacheManager.builder(factory).cacheDefaults(cacheConfiguration).build();}/*** json序列化器*/private Jackson2JsonRedisSerializerObject serializer() {Jackson2JsonRedisSerializerObject jackson2JsonRedisSerializer new Jackson2JsonRedisSerializer(Object.class);ObjectMapper objectMapper new ObjectMapper();objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);jackson2JsonRedisSerializer.setObjectMapper(objectMapper);return jackson2JsonRedisSerializer;}
}验证
简单介绍下思路首先我们平时使用redis时都会先将redisTemplate注入到spring容器中。 这是为什么呢因为我们需要修改默认的序列化方式让序列化而成的数据更加的直观而不是领人发指的数据结构。 在未重写CacheManage时序列化生成的数据如下图所示 可以明显看到保存在redis中的数据格式。这种格式无法让我们直观的看到redis中的数据是否正确所以需要将数据加工。。这个时候根据以往的redisTemplate经验就知道这里需要定义一个序列化器来完成这一操作
重写 CachingConfigurer 实现类提供的cacheManage()方法。将redisTemplate使用的序列化器交给他使用转化我们要保存的值。
/*** 缓存管理器* - 使用redis做cache缓存注解管理* - 修改原生的序列化方式改为json方式*/Overridepublic CacheManager cacheManager() {RedisCacheConfiguration cacheConfiguration RedisCacheConfiguration.defaultCacheConfig().disableCachingNullValues().serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(serializer()));return RedisCacheManager.builder(factory).cacheDefaults(cacheConfiguration).build();}》 修改后结果如下 可以看到想法成立。将原生的序列化器改为json格式的序列化器来优化我们的日常开发是必不可少的一步。
结束
感谢朋友看到这篇文章。希望我的文章能对你有所帮助后续在补充更多cache的相关使用操作。拜拜~~