idm*_*iev 3 redis spring-data spring-data-redis
我需要为我拥有的每个实体设置全局 TTL,并且它应该可以在一个地方进行配置。有机会通过 @RedisHash 注释来做到这一点:
@RedisHash(value = "persons",timeToLive = 100)
public class Person{
...
}
Run Code Online (Sandbox Code Playgroud)
或者我可以有一个领域
public class Person{
@TimeToLeave
Long ttl;
}
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,我无法在一个地方更改它,并且维护它并不是很舒服。我在 applicaiton.properties 中有一个属性:
app.redis.ttl=100
Run Code Online (Sandbox Code Playgroud)
有机会在财产层面改变它会很棒。
您可以通过创建 的子类KeyspaceConfiguration
并配置@EnableRedisRepositories
. 全局 TTL 没有基于属性的配置。
@EnableRedisRepositories(keyspaceConfiguration = MyKeyspaceConfiguration.class)
public class MyConfig {
}
public class MyKeyspaceConfiguration extends KeyspaceConfiguration {
@Override
public boolean hasSettingsFor(Class<?> type) {
return true;
}
@Override
public KeyspaceSettings getKeyspaceSettings(Class<?> type) {
KeyspaceSettings keyspaceSettings = new KeyspaceSettings(type, "my-keyspace");
keyspaceSettings.setTimeToLive(3600L);
return keyspaceSettings;
}
}
Run Code Online (Sandbox Code Playgroud)
派生 fromKeyspaceConfiguration
旨在首先提供Iterable<KeyspaceSettings> initialConfiguration()
,但由于您希望将该设置应用于所有类,就地创建KeyspaceSettings
更有意义。
您可能还希望缓存KeyspaceSettings
不创建实例,因此 Java 8Map.computeIfAbsent(…)
将是一个不错的选择。
归档时间: |
|
查看次数: |
2168 次 |
最近记录: |