Re: [web2py] How to retrieve info about sessions when they are stored in Redis?

2018-04-15 Thread Lisandro
Thank you very much Richard!
With your help I was able to retrieve the keys of the sessions stored in 
Redis with this simple line of code:

session_keys = cache.redis.r_server.smembers('w2p:sess:%s:id_idx' % 
application_name)

That line returns a set with all the session keys stored in Redis.
Notice that the keys retrieved may be expired, so if you need to check how 
many of those keys are still valid, you would have to iterate over the set 
checking the ttl of each key, like this:

valid_session_keys = [key for key in session_keys if cache.redis.r_server.
ttl(key) > 0]

I'm not sure why the deleted keys remain in Redis with a negative TTL, but 
I presume this is because redis server would be doing some automatically 
cleaning once every time, deleting definitively those keys with negative 
TTL.

Thanks again!
Regards,
Lisandro.



El miércoles, 11 de abril de 2018, 13:49:25 (UTC-3), Richard escribió:
>
> I gave a look directly in the redis shell
>
> redis-cli -h localhost -p 6379 -a PASSWORD_IF_YOU_SET_ONE
>
> It appears that each session store will have a different key which goes 
> like that : w2p:sess:APP_NAME:SOME_ID
>
> And w2p:sess:APP_NAME:id_idx will contains a set of each unique session 
> existings, so you would have to access this list of session id then access 
> the actual session.
>
> You can list keys in redis with :
>
> SCAN 0
>
> Better then KEYS that could cause lock and lot of memory usage 
>
> To list the set of existing sessions in ...:id_idx you need to use
>
> smembers w2p:sess:APP_NAME:id_idx
>
> It migth help you figure out how to manage you redis sessions form python 
> and better understand session contrib : 
> https://github.com/web2py/web2py/blob/0d646fa5e7c731cb5c392adf6a885351e77e4903/gluon/contrib/redis_session.py
>
> Good luck
>
> Richard
>
>
>
> On Mon, Apr 9, 2018 at 12:11 AM, Lisandro  > wrote:
>
>> 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 
>> gluon.contrib.redis_cache 
>> 
>> :
>>
>> 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+un...@googlegroups.com .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
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, 

Re: [web2py] How to retrieve info about sessions when they are stored in Redis?

2018-04-11 Thread Richard Vézina
I gave a look directly in the redis shell

redis-cli -h localhost -p 6379 -a PASSWORD_IF_YOU_SET_ONE

It appears that each session store will have a different key which goes
like that : w2p:sess:APP_NAME:SOME_ID

And w2p:sess:APP_NAME:id_idx will contains a set of each unique session
existings, so you would have to access this list of session id then access
the actual session.

You can list keys in redis with :

SCAN 0

Better then KEYS that could cause lock and lot of memory usage

To list the set of existing sessions in ...:id_idx you need to use

smembers w2p:sess:APP_NAME:id_idx

It migth help you figure out how to manage you redis sessions form python
and better understand session contrib :
https://github.com/web2py/web2py/blob/0d646fa5e7c731cb5c392adf6a885351e77e4903/gluon/contrib/redis_session.py

Good luck

Richard



On Mon, Apr 9, 2018 at 12:11 AM, Lisandro 
wrote:

> 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
> gluon.contrib.redis_cache
> 
> :
>
> 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.
>

-- 
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.


[web2py] How to retrieve info about sessions when they are stored in Redis?

2018-04-08 Thread Lisandro
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 
gluon.contrib.redis_cache 

:

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.