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 
<https://stackoverflow.com/questions/23296681/redis-safely-retrieving-a-small-set-of-keys-in-production-database>,
 
so I written this code based on what I saw in the clear() method at 
gluon.contrib.redis_cache 
<https://github.com/web2py/web2py/blob/master/gluon/contrib/redis_cache.py#L233>
:

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.

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (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 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to