How to update a Redis key expiration on read/write? - spring

I'm using Redis with Spring/Lettuce with a org.springframework.data.repository.CrudRepository to persist a session defined as a #RedisHash to Redis. I want to update the TTL on every access, read or write. I thought there might be some hook I could use as with JPA Entity Lifecycle Events, but I couldn't find any.
Are there any other options?

Related

Is there any way to use redis transaction with Spring data redis reactive?

Usual org.springframework.data.redis.core.RedisTemplate have that multi() method, which allow to start a transaction and exec() to commit.
But org.springframework.data.redis.core.ReactiveRedisTemplate does not have that methods.
I searched a lot for an any way on how to use transaction with spring-boot-starter-data-redis-reactive and i found no solutions.
The only way i see it now is manualy create Lettuce Client Bean and use it along side Spring implementation. But that is not handy to have 2 separate redis clients.
Does anyone know how to use redis transaction with spring-boot-starter-data-redis-reactive? could you please write a simple example?

Spring boot Redis cache TTL property change on the fly

is it possible to change the TTL property of Redis cache during the runtime if the same property has been changed in app config server? is there a way to automate the process to refresh the Redis instance properties during runtime on the event of config server change?
If you want to get the latest property in Config Server, it is recommended to do it through the client polling method, which can be found at
https://learn.microsoft.com/en-us/azure/spring-apps/how-to-config-server#config-server-refresh
Regarding the load to Redis instances, you may need to write some code to send out the event.

How to minimize interaction with Redis when using it as a Spring Session cache?

We are using Spring Cloud Gateway for OAuth2 authentication, after which it stores users session information in Redis with default settings set by #EnableRedisWebSession and
#Bean
fun redisConnectionFactory(): LettuceConnectionFactory {
return LettuceConnectionFactory("redis-cache", 6379);
}
#Bean
fun authorizedClientRepository(): ServerOAuth2AuthorizedClientRepository {
return WebSessionServerOAuth2AuthorizedClientRepository()
}
application.yml cache settings:
spring:
session:
store-type: redis
redis:
save-mode: on_set_attribute
flush-mode: on_save
It works fine, though I can see it makes requests to Redis on every user requests like it doesn't have in-memory cache at all. Is there any option to change this behaviour (i.e. make requests though the network to Redis only if current user session is not found in local memory cache)? May be I can reimplement some classes or there is no way to do it except of rewriting all cache logic? Sorry, for quite a broad question, but I didn't find any information on this topic in the documentation. Or maybe you could point me at classes in Spring Session source code, where this logic is implemented, so I could figure out what are my options.
I'm using spring-cloud-starter-gateway 2.2.5.RELEASE, spring-session-core 2.3.1.RELEASE, spring-boot-starter-data-redis-reactive and spring-session-data-redis.
From reading the documentation, I don't believe it is possible out of the box as using a local cache could result in inconsistent state amongst all connecting SCG instances to that Redis Instance.
You would need to define your own implementation of a SessionRepository that will try a local caffeine cache, and if not found then go to Redis instead. As a starting point, you could duplicate or trying extending the RedisSessionRepository.
The only thing you'd need to be careful of then is if you have multiple instances of SCG running how to handle if another instance updates redis, how the other instances would handle it if they have a locally cached instance already.

How can Spring JPA use two different database connections for read and write operations?

we use in our backend AWS Aurora which has two instances, one for read+write-access and one only for read access.
In our backend we use Spring data JPA to manage our Entities in the database. Would it now be possible, that spring will use the read node for selecting data and the read/write node only for write operations?
How could we configure that? Therefore the read instance has another hostname this instance do nothing.
Thx.

How to selectively store attributes in Redis when using Spring Session

Using Redis with Spring Session, is there any possibility to "select" which attributes to be stored and updated in Redis and filter out the others?
Something similar to "sessionAttributeFilter" or "sessionAttributeNameFilter" properties. For example, sessionAttributeFilter is used by the memcached session manager for the same purpose.

Resources