大家好,又见面了,我是你们的朋友全栈君。
查询操作是应用中最常见的操作,如果每次查询都从 MySQL 中查询则会影响效率,通常需要引入缓存来实现查询性能的优化。缓存可以选择本地缓存,远程缓存或本地缓存结合远程缓存。本地缓存可以使用 Guava 或 Caffeine 提供的解决方案,而远程缓存则可以选择 Redis 这样的内存数据库。本文记录一下 SpringBoot 集成 Redis 做缓存的相关配置。
引入相应 Starter。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
SpringBoot 中缓存由 CacheManager 管理,实现自己的 CacheManager 即可。
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//解决查询缓存转换异常的问题,为Jackson配置ObjectMapper
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.activateDefaultTyping(
LaissezFaireSubTypeValidator.instance,
ObjectMapper.DefaultTyping.NON_FINAL,
JsonTypeInfo.As.WRAPPER_ARRAY);
om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
jackson2JsonRedisSerializer.setObjectMapper(om);
// 配置序列化(解决乱码的问题)
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.disableCachingNullValues();
Set<String> cacheNames = new HashSet<>();
cacheNames.add("FlashActivity");
cacheNames.add("FlashItem");
Map<String, RedisCacheConfiguration> configMap = new ConcurrentHashMap<>();
configMap.put("FlashActivity", config.entryTtl(Duration.ofMinutes(60)));
configMap.put("FlashItem", config.entryTtl(Duration.ofSeconds(3)));
return RedisCacheManager.builder(factory)
.cacheDefaults(config)
.initialCacheNames(cacheNames)
.withInitialCacheConfigurations(configMap)
.build();
}
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 必要配置,否则反序列化得到的是LinkedHashMap对象
om.activateDefaultTyping(
LaissezFaireSubTypeValidator.instance,
ObjectMapper.DefaultTyping.NON_FINAL,
JsonTypeInfo.As.WRAPPER_ARRAY);
// 出现未知字段不报错
om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 配置序列化(解决乱码的问题)
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.disableCachingNullValues();
Set<String> cacheNames = new HashSet<>();
cacheNames.add("FlashActivity");
cacheNames.add("FlashItem");
Map<String, RedisCacheConfiguration> configMap = new ConcurrentHashMap<>();
configMap.put("FlashActivity", config.entryTtl(Duration.ofMinutes(60)));
configMap.put("FlashItem", config.entryTtl(Duration.ofSeconds(3)));
// 类上指定缓存到哪些 Cache
@CacheConfig(cacheNames = "FlashItem")
@CacheEvict(key = "'FlashItemCache'.concat(#itemId)")
// sync指定为true,缓存失效只会有一个线程取请求数据库,其他线程使用请求回的数据
@Cacheable(key = "'ActivityFlashItemCache'.concat(#activityId)", sync = true)
使用 RedisTemplate 可以实现手动缓存或其他对 Redis 操作,但之前只是配置缓存相关配置,直接使用RedisTemplate 时不会生效,需要单独配置。
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key-value结构序列化数据结构
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
// hash数据结构序列化方式,必须这样否则存hash 就是基于jdk序列化的
redisTemplate.setHashKeySerializer(stringRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/192673.html原文链接:https://javaforall.cn