gpt4 book ai didi

Java Spring Redis : Set time to live with annotation

转载 作者:可可西里 更新时间:2023-11-01 11:15:11 28 4
gpt4 key购买 nike

我正在寻找一种使用 spring 注释设置 TTL 的方法。

我试过了

@CacheEvict(allEntries = true, value = { "mycache" })
@Scheduled(fixedDelay = 5000)

然后将 @EnableScheduling 放在我的 Application.java 上,但没有成功。

我还在类上尝试了 @RedisHash(timeToLive=2000) 有和没有 @TimeToLive(unit = TimeUnit.SECONDS) 方法。

然后我尝试将 @EnableRedisRepositories(keyspaceConfiguration = UserKeySpaceConfiguration.class) 放在我的类(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

关于如何进行的任何想法?

谢谢。

最佳答案

经过一周的搜索,我找到了解决方案。

要在不同的 key 上使用不同的 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;
}

}

为了区分你的缓存,你有一个带有注释 @Bean 的属性 name

您需要有一个@Primary @Bean 否则您的应用程序将崩溃。在我的例子中,我的主要 juste 返回缓存而不改变。

所以在这个自定义缓存中我只是添加了一个 TTL

cacheConfiguration = cacheConfiguration.entryTtl(Duration.ofSeconds(120));

当你在一个方法上使用你的缓存时,你只需要像这样指定你的缓存:

@Cacheable(value = "value", key = "#key", cacheManager = "yourCacheName")

以我为例

@Cacheable(value = "pickle", key = "#pickleId", cacheManager = "pickleCacheManager")
Pickle findFirstById(String pickleId);

@Cacheable 中的cacheManager@Bean 中的name

关于Java Spring Redis : Set time to live with annotation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51269477/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com