Recently I moved the sessions from the database to Redis, and I'm wondering: is there a way to retrieve info about sessions when they are stored in Redis?
For example, when sessions are stored in the database, you have the option to use SQL to do some stuff like counting or deleting sessions. How to do it when sessions are stored in Redis?
I also use Redis to cache HTML responses from web2py and any other stuff that can be cached (lists, dictionaries, etc). In order to be able to list the keys cached by one specific web2py application, I have written this custom function to retrieve those keys.
def get_cache_keys(application, prefix=''):
import re
result = []
regex = ':%s*' % prefix
prefix = 'w2p:%s' % application
cache_set = 'w2p:%s:___cache_set' % application
r = re.compile(regex)
buckets = current.cache.redis.r_server.smembers(cache_set) # get all buckets
if buckets: # get all keys in buckets
keys = current.cache.redis.r_server.sunion(buckets)
else:
return result
for a in keys:
if r.match(str(a).replace(prefix, '', 1)):
result.append(a)
return result
With that code, I'm able to list all the keys cached by a web2py application.
As I'm also using Redis to store sessions, I want to be able to list all the session keys.
I've tried a similar code to the one showed above, replacing this:
prefix = 'w2p:sess:%s' % application
cache_set = 'w2p:sess:%s:id_idx' % application
But that doesn't work. Is it possible to achieve what I want? Any suggestion will be much appreciated.
Regards,
Lisandro.