1.接口每次查询耗费时间很长,考虑该接口数据是不经常变数据,故新增缓存处理。
1.1maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
1.2 配置文件yml添加redis配置文件
redis:
database: 0
host: xxxxx
port: 8101
password: xxxx
timeout: 20000
1.3 把redis数据源加载到spring容器中
//项目启动缓存
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(2*60))//过期超时时间 2分钟
.disableCachingNullValues();
return RedisCacheManager.builder(factory)
.cacheDefaults(config)
.transactionAware()
.build();
}
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate(factory);
RedisSerializer keySerializer = new StringRedisSerializer(); // 设置key序列化类,否则key前面会多了一些乱码
template.setKeySerializer(keySerializer);
setValueSerializer(template);//设置value序列化
template.afterPropertiesSet();
template.setEnableTransactionSupport(true);
return template;
}
private void setValueSerializer(StringRedisTemplate template) {
@SuppressWarnings({"rawtypes", "unchecked"})
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setValueSerializer(jackson2JsonRedisSerializer);
}
}
1.4 实现
@Cacheable(cacheNames = "xxxx",key = "#name")
public Result queryEnterpriseIcInfo(String id, String name){
//查询数据库操作,cacheable先去缓存查找,如果没有则查询数据库,查询结果会存入缓存。下次查询会自动从缓存加载。
}
1.5 超时时间设置
在1.3设置了默认了2分钟的超时时间,但对于不同场景,我们希望有不同的超时时间。于是将1.3的代码做了一些小修改
首先我新增了一个类,并对createRedisCache函数做改写。
public class MyRedisCacheManager extends RedisCacheManager{
public MyRedisCacheManager(RedisCacheWriter writer,RedisCacheConfiguration defaultCacheConfiguration) {
super(writer,defaultCacheConfiguration);
}
@Override
protected RedisCache createRedisCache(String name,RedisCacheConfiguration cacheConfig) {
if(!StringUtils.isEmpty(name) && name.contains("#")) {
String numStr = name.split("#")[1];
if(StringUtils.isNumeric(numStr)) {
return super.createRedisCache(name, cacheConfig.entryTtl(Duration.ofSeconds(Integer.parseInt(numStr))));
}
}
return super.createRedisCache(name, cacheConfig);
}
}
然后在1.3的代码中将RedisCacheManager注释掉,使用我自定义的类。
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(2*60))//过期超时时间 2分钟
.disableCachingNullValues();
// return RedisCacheManager.builder(factory)
// .cacheDefaults(config)
// .transactionAware()
// .build();
return new MyRedisCacheManager(RedisCacheWriter.nonLockingRedisCacheWriter(factory), config);
}
最后我就可以在需要做缓存的地方,按格式写超时时间了。其中#14400是14400秒。
@Cacheable(cacheNames="PhoneBusinessTable#14400",key="#phone")
public String getPhoneBusinessTable(String phone) {
}