I am trying to configure TTL on RedisHash. I want to set same expiry to all the keys.
1st: I tried by adding annotation @RedisHash(value=”MyHash”,timeToLive=60) on the entity class.
2nd: Add a new field as expiration with @TimetoLive along with @RedisHash(value=”MyHash”,timeToLive=60)
@RedisHash(value = "MyHash", timeToLive = 60L)
public class MyHash {
.../attributes with few indexes
@TimeToLive
private Long expiration;
}
3rd: Added @EnableRedisRepositories with KeyspaceConfiguration
@EnableRedisRepositories(basePackageClasses = MyHash.class, keyspaceConfiguration = MyKeyspaceConfiguration.class)
public class RedisConfig {
//LettuceConnectionFactory
//RedisTemplate
}
public class MyKeyspaceConfiguration extends KeyspaceConfiguration {
@Override
public boolean hasSettingsFor(Class<?> type) {
return true;
}
@Override
public KeyspaceSettings getKeyspaceSettings(Class<?> type) {
KeyspaceSettings keyspaceSettings = new KeyspaceSettings(MyHash.class, "MyHashlog");
keyspaceSettings.setTimeToLive(60L);
return keyspaceSettings;
}
}
My Repository:
public interface MyHashRepository extends CrudRepository<MyHash, Long> {
List<MyHash> findByApplicationId(String applicationId) ;
}
All above approach do not set any expiry. When I check in Redis it shows -1.
TTL MyHash:applicationId:e1hd9-w6q0s-5jd3e-wi2h4
(integer) -1
2
Answers
Found the solution: We need to add enableKeyspaceEvents = RedisKeyValueAdapter.EnableKeyspaceEvents.ON_STARTUP attribute as below,
Solution 1: Using @RedisHash to set TTL
Solution 2: Use KeySapceConfiguration for setting TTL
No changes to Repository:
Also Spring will create multiple keys based on the attributes that are annotated as @Indexed in your entity class. However, TTL is only applied on the primary key i.e. @Id. For example, When I run keys command in redis-cli
I was able to solve this two ways:
Solution 1:
Adding the
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 3600)
to my configuration classSolution 2:
Extending the
RedisHttpSessionConfiguration
class and callingsetMaxInactiveIntervalInSeconds
I opted for the latter so that I can eventually add the ttl as a config value.