You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to load the timeToLive from a configuration properties.
Problem
Here is the attempt:
@Configuration
@EnableRedisRepositories // NOT PROVIDED because using RedisMappingContext: (keyspaceConfiguration = KeyspaceConfig::class)classRedisRepositoryConfig(
valmyConfigurationProperties:MyConfigurationProperties
) {
@Bean
funredisTemplate(redisConnectionFactory:RedisConnectionFactory?): RedisTemplate<*, *> {
val template =RedisTemplate<ByteArray?, ByteArray?>()
template.connectionFactory = redisConnectionFactory
return template
}
@Bean
funkeyValueMappingContext() =RedisMappingContext(
MappingConfiguration(IndexConfiguration(), KeyspaceConfig(
ttl = myConfigurationProperties.redisTTLSeconds.also { println("Creating KeyspaceConfig with TTL=$it") }
))
)
}
// It does not make a difference whether this class is defined within the RedisRepositoryConfig or notclassKeyspaceConfig(
valttl:Long,
) : KeyspaceConfiguration() {
init {
println("KeyspaceConfig constructor called with TTL=$ttl")
}
overridefuninitialConfiguration(): Iterable<KeyspaceSettings> =listOf(
KeyspaceSettings(MyData::class.java, "MyHash").apply {
timeToLive = ttl.also { println("initialConfiguration(): TTL property is $ttl") }
}
)
}
@RedisHash // This annotation seems still neededdata classMyData(
@Id
valresourceId:String,
valdata:String
)
As opposed to the documentation, the KeyspaceSettings are not static, so we have to pass or access the configuration somehow.
I tried the timeToLivePropertyName = "my.config.redis-ttl-seconds" field in KeyspaceSettings, but it did not have any effect.
I tried injecting the MyConfigurationProperties in the KeyspaceConfig constructor (and using @EnableRedisRepositories(keyspaceConfiguration = KeyspaceConfig::class)) , but that doesn't work
So I tried instead passing the value in the constructor (as shown above), in the keyValueMappingContext() bean, but it doesn't work as expected, see below
Running the application results in the following output:
Creating KeyspaceConfig with TTL=5
initialConfiguration(): TTL property is 0
KeyspaceConfig constructor called with TTL=5
This shows that KeyspaceConfig is instantiated from elsewhere, and that instance is used to call initialConfiguration() on, instead of the one that is constructed in keyValueMappingContext(), which I would expect.
Workaround
If not declaring a separate KeyspaceConfig class, but instead implement a KeyspaceConfiguration on-the-fly, it works:
Why does it behave in the way it does, and is this correct?
Can the documentation be updated to show how to properly use KeyspaceConfig when one needs to load properties dynamically or from configuration properties?
The text was updated successfully, but these errors were encountered:
I am trying to set up a Redis repository with programmatic keyspaces configuration according to https://docs.spring.io/spring-data/redis/reference/redis/redis-repositories/keyspaces.html
I would like to load the
timeToLive
from a configuration properties.Problem
Here is the attempt:
As opposed to the documentation, the
KeyspaceSettings
are not static, so we have to pass or access the configuration somehow.timeToLivePropertyName = "my.config.redis-ttl-seconds"
field inKeyspaceSettings
, but it did not have any effect.MyConfigurationProperties
in theKeyspaceConfig
constructor (and using@EnableRedisRepositories(keyspaceConfiguration = KeyspaceConfig::class)
) , but that doesn't workkeyValueMappingContext()
bean, but it doesn't work as expected, see belowRunning the application results in the following output:
This shows that
KeyspaceConfig
is instantiated from elsewhere, and that instance is used to callinitialConfiguration()
on, instead of the one that is constructed inkeyValueMappingContext()
, which I would expect.Workaround
If not declaring a separate
KeyspaceConfig
class, but instead implement aKeyspaceConfiguration
on-the-fly, it works:Output:
Issues
KeyspaceConfig
when one needs to load properties dynamically or from configuration properties?The text was updated successfully, but these errors were encountered: