why spring redis session 'touch' method called in 'cleanExpiredSessions' method can clean expired session

324 Views Asked by At

i am chinese,My English is so poor,I try my best to describe my problem clearly I recently learned about the spring redis session cleaning mechanism,the clean method is 'cleanExpiredSessions' in RedisSessionExpirationPolicy class,What makes me wonder is the 'touch(String key)' method can clean expire session,why? i think 'spring:session:sessions:d5e0f376-69d1-4fd4-9802-78eb5a3db144' expire time is fiveMinutesAfterExpires,when spring cleanup task runing,the key does not expired,so 'touch(String key)' method can not clean the key

Please explain to me thank you very much

1

There are 1 best solutions below

0
On

This method runs periodically and based on below property (default is every rounded minute):

    @Scheduled(cron = "${spring.session.cleanup.cron.expression:0 * * * * *}")

It “touches” the key not in order to expire it, but to make sure it is deleted and expiration event processing is handled - fire SessionDestroyedEvent for all the subscriptions.

And this stands for handling the synchronization issue to predict and force Redis to fire those events near to the expiration time since Redis does not guarantee this. same described here:

When a session expires key is deleted or expires, the keyspace notification triggers a lookup of the actual session and a SessionDestroyedEvent is fired. One problem with relying on Redis expiration exclusively is that Redis makes no guarantee of when the expired event will be fired if the key has not been accessed. Specifically the background task that Redis uses to clean up expired keys is a low priority task and may not trigger the key expiration. For additional details see Timing of expired events section in the Redis documentation. To circumvent the fact that expired events are not guaranteed to happen we can ensure that each key is accessed when it is expected to expire. This means that if the TTL is expired on the key, Redis will remove the key and fire the expired event when we try to access the key.