我正在寻找一种通过弹簧注释设置TTL的方法。
我尝试了
@CacheEvict(allEntries = true, value = { "mycache" })
@Scheduled(fixedDelay = 5000)
然后将@EnableScheduling
放到我的Application.java
上就没有成功。
我还在类上使用@RedisHash(timeToLive=2000)
的方法上尝试过@TimeToLive(unit = TimeUnit.SECONDS)
。
然后我尝试将@EnableRedisRepositories(keyspaceConfiguration = UserKeySpaceConfiguration.class)
放在课堂上
public class UserKeySpaceConfiguration extends KeyspaceConfiguration {
/**
* {@inheritDoc}
*
* @see org.springframework.data.redis.core.convert.KeyspaceConfiguration#getKeyspaceSettings(java.lang.Class)
*/
@Override
public KeyspaceSettings getKeyspaceSettings(final Class<?> type) {
final KeyspaceSettings keyspaceSettings = new KeyspaceSettings(type, "user-keyspace");
keyspaceSettings.setTimeToLive(172800L);
return keyspaceSettings;
}
/**
* {@inheritDoc}
* @see org.springframework.data.redis.core.convert.KeyspaceConfiguration#hasSettingsFor(java.lang.Class)
*/
@Override
public boolean hasSettingsFor(final Class<?> type) {
return true;
}
}
所有此方法均无效。
当我检查Redis的密钥是否具有TTL时,我总是具有-1
。
关于如何进行的任何想法?
谢谢。
答案 0 :(得分:1)
经过一周的搜索,我找到了解决方法。
要在不同的键上使用不同的TTL,必须创建自己的缓存管理器。
因此,在您的Application.java
中添加自定义缓存管理器。
@SpringBootApplication
@EnableSwagger2
@EnableCaching
public class Application extends SpringBootServletInitializer {
public static void main(final String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
@Primary
@Bean
public RedisCacheManager cacheManager(final RedisConnectionFactory connectionFactory) {
final RedisCacheWriter redisCacheWriter = RedisCacheWriter.lockingRedisCacheWriter(connectionFactory);
final SerializationPair<Object> valueSerializationPair = RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer());
final RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
final RedisCacheManager redisCacheManager = new RedisCacheManager(redisCacheWriter, cacheConfiguration);
return redisCacheManager;
}
@Bean(name = "pickleCacheManager")
public RedisCacheManager pickleCacheManager(final RedisConnectionFactory connectionFactory) {
final RedisCacheWriter redisCacheWriter = RedisCacheWriter.lockingRedisCacheWriter(connectionFactory);
final SerializationPair<Object> valueSerializationPair = RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer());
RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
cacheConfiguration = cacheConfiguration.entryTtl(Duration.ofSeconds(120));
final RedisCacheManager redisCacheManager = new RedisCacheManager(redisCacheWriter, cacheConfiguration);
return redisCacheManager;
}
@Bean(name = "userCacheManager")
public RedisCacheManager userCacheManager(final RedisConnectionFactory connectionFactory) {
final RedisCacheWriter redisCacheWriter = RedisCacheWriter.lockingRedisCacheWriter(connectionFactory);
final SerializationPair<Object> valueSerializationPair = RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer());
RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
cacheConfiguration = cacheConfiguration.entryTtl(Duration.ofSeconds(172800));
final RedisCacheManager redisCacheManager = new RedisCacheManager(redisCacheWriter, cacheConfiguration);
return redisCacheManager;
}
}
要在缓存之间有所作为,请使用属性name
和注释@Bean
。
您需要拥有@Primary
@Bean
,否则您的应用程序将崩溃。在我的情况下,我的主要人员将不做任何更改就返回缓存。
因此在此自定义缓存中,我只需添加
的TTLcacheConfiguration = cacheConfiguration.entryTtl(Duration.ofSeconds(120));
当您在某个方法上使用缓存时,只需像这样指定缓存:
@Cacheable(value = "value", key = "#key", cacheManager = "yourCacheName")
就我而言
@Cacheable(value = "pickle", key = "#pickleId", cacheManager = "pickleCacheManager")
Pickle findFirstById(String pickleId);
您在cacheManager
中的@Cacheable
是您在name
中的@Bean
。
答案 1 :(得分:0)
您需要更新
@CacheEvict(allEntries = true, value = { "mycache" })
到
@CacheEvict(allEntries = true, cacheNames = { "mycache" })
它可以在StackOverflow上参考此答案。