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. 
I've read that it's not a good idea to use cache.redis.r_server.keys() 
method on production 
so I written this code based on what I saw in the clear() method at 

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 
    if buckets:  # get all keys in buckets
        keys = current.cache.redis.r_server.sunion(buckets)
        return result
    for a in keys:
        if r.match(str(a).replace(prefix, '', 1)):
    return result

With that code, I'm able to list all the keys cached by a web2py 
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.


- (Documentation)
- (Source code)
- (Report Issues)
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
For more options, visit

Reply via email to