I\'m currently bulding a web app and would like to use Redis to store sessions. At login, the session is inserted into Redis with a corresponding user id, and expiration set at
In more recent versions of Redis (2.8.0 and up) Keyspace Notifications for expired
events are supported. I.e. when a key with a TTL expires this event is triggered.
This is what to subscribe to:
'__keyevent@0__:expired'
So subscribing to this event allows you to have a single index for all sessions and you can remove the key from the index when the key expires.
Example:
Use a sorted set as a secondary index with the uid as the weight:
ZADD "idx-session-uid"
Search for sessionkeys for a specific user with:
ZRANGEBYSCORE "idx-session-uid"
When a session is deleted or expired we do:
ZREM "idx-session-uid"