项目使用@Cacheable注解来实现方法级别的缓存,需求中有些方法适合使用仅缓存一两小时即可,但现有的@Cacheable注解中没有直接设置缓存时间的字段,所以需要单独配置
在使用网上百度的配置时出现了序列化异常问题,但不配置CacheConfig却能正常缓存,启用自己的配置文件却出现异常,这里说明自己的配置CacheConfig与SpringBoot的默认配置有所出入,但自己写的配置无非就是更改某些配置参数,所以解决思路就是使用SpringBoot默认的配置,来排查原因
/**
* @Author: ZeRen.
*/
@Configuration
public class TestConfig {
@Autowired
CacheManager cacheManager;
@PostConstruct
public void viewDefaultCacheManager() {
RedisCacheManager redisCacheManager = (RedisCacheManager) this.cacheManager;
System.out.println(redisCacheManager);
}
}
IDEA点击此处即可看到SpringBoot的默认缓存配置CacheManager的默认声明所在位置
这里就是SpringBoot的默认键值序列化配置项了
想看源码的同学可以按照我上面描述的使用IDEA进入源码中研究,此处就不一一描述了,这里需要注意的是这两个键值序列化方式,和一个RedisCacheConfiguration类的工厂构造方法入参的一个类加载器ClassLoader(实现为ResourceLoader)
package com.sun.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import javax.annotation.PostConstruct;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
/**
* @Author: ZeRen.
* @title 该配置文件大部分使用SpringBoot默认配置,仅加入了有期限缓存的键
*/
@Configuration
public class CacheConfig {
@Autowired
ResourceLoader resourceLoader;
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
return RedisCacheManager.builder(redisConnectionFactory)
.cacheDefaults(getDefaultCacheConfiguration())//默认的缓存配置(没有配置键的key均使用此配置)
.withInitialCacheConfigurations(getCacheConfigurations())
.transactionAware() //在spring事务正常提交时才缓存数据
.build();
}
private Map<String, RedisCacheConfiguration> getCacheConfigurations() {
Map<String, RedisCacheConfiguration> configurationMap = new HashMap<>();
//缓存键,且30秒后过期,30秒后再次调用方法时需要重新缓存
configurationMap.put("expireKey", this.getDefaultCacheConfiguration(Duration.ofSeconds(30)));
return configurationMap;
}
/**
* 获取redis的缓存配置(针对于键)
*
* @param ttl 键过期时间
* @return
*/
private RedisCacheConfiguration getDefaultCacheConfiguration(Duration ttl) {
// 获取Redis缓存配置,此处获取的为默认配置
final RedisCacheConfiguration defaultCacheConfiguration = getDefaultCacheConfiguration();
// 设置键过期的时间,用 java.time 下的Duration表示持续时间,进入entryTtl()方法的源码中可看到
// 当设置为 0 即 Duration.ZERO 时表示键无过期时间,其也是默认配置
return defaultCacheConfiguration.entryTtl(ttl);
}
/**
* 获取Redis缓存配置,此处获取的为默认配置
* 如对键值序列化方式,是否缓存null值,是否使用前缀等有特殊要求
* 可另行调用 RedisCacheConfiguration 的构造方法
*
* @return
*/
private RedisCacheConfiguration getDefaultCacheConfiguration() {
// 注意此构造函数为 spring-data-redis-2.1.9 及以上拥有,经试验 已知spring-data-redis-2.0.9及以下版本没有此构造函数
// 但观察源码可得核心不过是在值序列化器(valueSerializationPair)的构造中注入 ClassLoader 即可
return RedisCacheConfiguration.defaultCacheConfig(resourceLoader.getClassLoader());
}
}
在上面关键字段配置处都有注释描述需要注意的是,如果报错,一般是因为spring-redis-data版本不一致了,自行升级版本,或修改入参构造方法即可
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.11.RELEASEversion>
<relativePath/>
parent>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
本次经历虽耗费半天时间,但却体现了个SpringBoot配置问题的解决思路,仅此记录,
望抛砖引玉