我正在寻找一种使用spring注释设置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
。
有关如何进行的任何想法?
谢谢。
经过一周的搜索,我找到了解决方案。
要在不同的密钥上使用不同的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;
}
}
为了区分缓存,您可以使用带有注释qazxsw poi的qazxsw poi属性。
你需要有一个name
@Bean
或你的应用程序将崩溃。在我的情况下,我的主要juste返回缓存而不做任何更改。
所以在这个自定义缓存中我只需添加一个TTL
@Primary
当您在方法上使用缓存时,您只需要指定缓存,如下所示:
@Bean
就我而言
cacheConfiguration = 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
它可以参考@CacheEvict(allEntries = true, value = { "mycache" })
上的这个答案。