我正在尝试将我的redis前哨客户端配置为在到期事件中针对我放入缓存的不同密钥执行特定的作业。
此作业应检查是否续订缓存值或使其过期。不幸的是,我找不到Redis这样的东西。
redis甚至支持它吗?或者在过期事件中无法对存储在缓存中的值做任何事情吗?
我的配置:
@Bean(destroyMethod = "shutdown")
public RedissonClient redisson() {
Config config = new Config();
SentinelServersConfig sentinelServersConfig = config.useSentinelServers();
for (String node : redisNodes) {
sentinelServersConfig.addSentinelAddress(REDIS_PROTOCOL + node);
}
sentinelServersConfig.setMasterName(redisMaster);
return Redisson.create(config);
}
@Bean
public MyCache myCache() {
return new MyCache(redisTemplate(), cacheTTL);
}
private <K, V> RedisTemplate<K, V> redisTemplate() {
RedisTemplate<K, V> template = new RedisTemplate<>();
template.setConnectionFactory(redissonConnectionFactory(redisson()));
GenericJackson2JsonRedisSerializer valueSerializer = new GenericJackson2JsonRedisSerializer();
StringRedisSerializer keySerializer = new StringRedisSerializer();
template.setKeySerializer(keySerializer);
template.setValueSerializer(valueSerializer);
template.setHashKeySerializer(keySerializer);
template.setHashValueSerializer(valueSerializer);
template.afterPropertiesSet();
return template;
}
我将元素放入缓存的方式:
public void putElement(String key, T value, Duration duration) {
String formattedKey = getKey(key);
redisTemplate.opsForValue().set(formattedKey, value);
redisTemplate.expire(formattedKey, duration.toMillis(), MILLISECONDS);
}
pom.xml
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-data-18</artifactId>
<version>${redisson-spring-data-version}</version>
</dependency>