java cache过期_Spring Cache 实现失效时间设置

Spring cache 注解式缓存用起来非常方便,但是却没有ttl 失效时间设置。这就很让人头疼了。

虽然在 @Cacheable 没有ttl注解时间的设置,但是我发现在注解上有cacheManage 的参数设置。

然后查看源码:

public class RedisCacheConfiguration {

private final Duration ttl;

private final boolean cacheNullValues;

private final CacheKeyPrefix keyPrefix;

private final boolean usePrefix;

private final SerializationPair keySerializationPair;

private final SerializationPair valueSerializationPair;

private final ConversionService conversionService;

private RedisCacheConfiguration(Duration ttl, Boolean cacheNullValues, Boolean usePrefix, CacheKeyPrefix keyPrefix, SerializationPair keySerializationPair, SerializationPair> valueSerializationPair, ConversionService conversionService) {

this.ttl = ttl;

this.cacheNullValues = cacheNullValues;

this.usePrefix = usePrefix;

this.keyPrefix = keyPrefix;

this.keySerializationPair = keySerializationPair;

this.valueSerializationPair = valueSerializationPair;

this.conversionService = conversionService;

}

public static RedisCacheConfiguration defaultCacheConfig() {

return defaultCacheConfig((ClassLoader)null);

}

public static RedisCacheConfiguration defaultCacheConfig(@Nullable ClassLoader classLoader) {

DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();

registerDefaultConverters(conversionService);

return new RedisCacheConfiguration(Duration.ZERO, true, true, CacheKeyPrefix.simple(), SerializationPair.fromSerializer(RedisSerializer.string()), SerializationPair.fromSerializer(RedisSerializer.java(classLoader)), conversionService);

}

public RedisCacheConfiguration entryTtl(Duration ttl) {

Assert.notNull(ttl, "TTL duration must not be null!");

return new RedisCacheConfiguration(ttl, this.cacheNullValues, this.usePrefix, this.keyPrefix, this.keySerializationPair, this.valueSerializationPair, this.conversionService);

}

RedisCacheConfiguration 是支持ttl设置的。但是默认的缓存时间设置是0。所以为什么一定要使用默认的cacheManager呢?

我们可以定义自己的cacheManager,甚至可以多设置几个。根据不同的缓存时间设置不同的cacheManager。

然后代码如下:

@Configuration

@EnableCaching

public class RedisCacheConfig {

@Bean

@Primary

public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {

RedisCacheConfiguration config = instanceConfig(1800L);

return RedisCacheManager.builder(connectionFactory)

.cacheDefaults(config)

.transactionAware()

.build();

}

@Bean

public RedisCacheManager cacheManager15d(RedisConnectionFactory connectionFactory) {

RedisCacheConfiguration config = instanceConfig(3600 * 24 * 15L);

return RedisCacheManager.builder(connectionFactory)

.cacheDefaults(config)

.transactionAware()

.build();

}

private RedisCacheConfiguration instanceConfig(Long ttl) {

Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);

ObjectMapper objectMapper = new ObjectMapper();

objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

objectMapper.registerModule(new JavaTimeModule());

// 去掉各种@JsonSerialize注解的解析

objectMapper.configure(MapperFeature.USE_ANNOTATIONS, false);

// 只针对非空的值进行序列化

objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

// 将类型序列化到属性json字符串中

objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);

jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

return RedisCacheConfiguration.defaultCacheConfig()

.entryTtl(Duration.ofSeconds(ttl))

.disableCachingNullValues()

.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer));

}

}

使用方式:

@Cacheable(value = "article", key = "#id", condition = "#id != null", cacheManager = "cacheManager15d")

默认的cacheManager的过期时间是永不过期。所以覆盖掉就行了。一个不够用,然后就再多配置几个。简单,粗暴,有效...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值