概述
cache的缓冲注解并没有专门的属性来设置缓冲的有效时间,那么使用什么样的方法来设置缓冲注解的有效时间呢?我个人这边有2种方式。
1、不灵活方式设置
我们在配置cacheManger的时候里面都会配置redis的连接,这样我们就可以直接在redis配置上设置好缓冲数据的有效时间,这种方式设置出来的有效时间,所有的数据的有效时间都是一样的而且无法进行灵活的修改。一般不采用。
2、灵活的方式设置有效时间
spring配置cache注解的时候我们一般都采用对的是RedisCacheManager,在new对象的时候里面可以设置RedisCacheConfiguration,通过这个以及注解里面的value就可以设置不同注解的有效时间。
package org.loulan.dawn.application.common.config.cache;
import lombok.extern.slf4j.Slf4j;
import org.loulan.dawn.application.common.pojo.exception.ConfigurationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
/*********************************************************
** Description: 缓冲注解配置
** <br><br>
** Date: Created in 2019/7/4 11:41
** @author 楼兰
** @version 0.0.0
*********************************************************/
@Slf4j
public class CacheConfiguration {
/**
* Description :安全管理部分配置的缓冲管理器
* @param
* @return
* @exception
* @author :loulan
* */
@Bean
@Primary
public CacheManager cacheManager(
@Autowired RedisConnectionFactory redisConnectionFactory
) {
log.info("进入cacheManager配置");
//初始化一个RedisCacheWriter
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
//初始化RedisCacheManager
return new RedisCacheManager(
redisCacheWriter,
this.getRedisCacheConfigurationWithTtl(0), //默认策略,没有配置过的value(cacheName)就会使用这个时间策略
this.getRedisCacheConfigurationMap()); //指定value 策略
}
/**
* Description : 指定value策略,只要注解的value(cacheName)设置的是这里面的值,就会使用这种测率
* 个人认为这里是spring redis的一种缓冲缺陷,可以使用缓冲连接rendis但是却无法设置缓冲数据的有效时间,所以采用这种策略的方式
* @param
* @return 指定value的策略集合
* @exception
* @author :loulan
* */
private Map<String,RedisCacheConfiguration> getRedisCacheConfigurationMap () {
Map<String, RedisCacheConfiguration> map = new HashMap<>();
// 有效时间1分钟
map.put("m1",this.getRedisCacheConfigurationWithTtl(1 * 60));
// 有效时间30分钟
map.put("m30",this.getRedisCacheConfigurationWithTtl(30 * 60))
return map;
}
/**
* Description :设置Redis缓冲配置以及配置redis有效时间
* @param seconds 数据有效时间
* @return
* @exception
* @author :loulan
* */
private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {
//设置CacheManager的值序列化方式为json序列化
RedisSerializer<Object> jsonSerializer = new GenericJackson2JsonRedisSerializer();
RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair
.fromSerializer(jsonSerializer);
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(pair) //设置redis的序列话方式
.entryTtl(Duration.ofSeconds(seconds))
.disableKeyPrefix() //禁止该缓冲使用redis前缀
.disableCachingNullValues(); //禁止对null值进行缓冲
return redisCacheConfiguration;
}
}
如上,如果注解的value设置为m1,那么当前注解缓冲数据的有效时间就是1分钟。如果value设置为m30,那么当前注解缓冲数据的有效时间就是30分钟。如果设置的不是m1也不是m30(当前程序里面就设置了m1和m30),那么就会启用默认策略,有效时间为永久有效。
@Cacheable(value = "m30",
key = "T(org.loulan.dawn.application.common.pojo.config.redis.RedisArea).USER_INFO + #id",
unless = "#result == null")