Question

I use redis to store user sessions by a guid i generate when the log in. I use that as the key in their session object.

If i was to lock a user's account I currently have to go through all sessions and check if they are the user's then delete them.

Is there a way to also query by the user id? Should I be using the sorted set insured of just standard key value?

Était-ce utile?

La solution

Going through all keys is probably not the best idea. What you could do is store every user sessions' guids in another key - the set data type seems to be the best choice for that - and add/remove from it as the user opens/closes a session. So, when a user opens a new session you will:

SET session:<guid> <session_object>
SADD user_sessions:<user_id> <session_guid>

and when the session is closed, you'll do:

DEL session:<guid>
SREM user_sessions:<user_id> <session_guid>

To find which session guids are a user's, e.g. for an account lock down, do:

SMEMBERS sessions:<user_id>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top