How do you change memcache session expiry?

Showing 1-9 of 9 messages
How do you change memcache session expiry? Robert Clark 9/27/12 3:27 PM
(Using web2py 1.99.7.  Running on Ubuntu/EC2 with Elasticache)

I've followed the deployment recipe for storing sessions in memcache but they expire after 300 seconds of inactivity.  That appears to be because MEMDB doesn't look to pass down a time_expiry, therefore the set() function from gluon/contrib/memcache/__init__.py always uses the default of 300.

The only way I've found to change this is to explicitly modify the default value from this source file.

Is there a better way to configure this value for session expiry from within application code?  Cheers.

Re: How do you change memcache session expiry? Jose C 9/28/12 5:27 AM
The only way I've found to change this is to explicitly modify the default value from this source file.

Is there a better way to configure this value for session expiry from within application code?  Cheers.

Far as I know it's done like this: 

set(key=key, value=value, time=<seconds>)

where (from the source):
@param time: Tells memcached the time which this value should expire, either
       
as a delta number of seconds, or an absolute unix time-since-the-epoch
        value
. See the memcached protocol docs section "Storage Commands"
       
for more info on <exptime>. We default to 0 == cache forever.

 Are you saying that doesn't work?

Re: How do you change memcache session expiry? Robert Clark 9/28/12 8:24 PM
Thanks, I am not having any problems with the memcached api & python interface, that part all works as advertised.

The problem is that if you follow the deployment recipe for storing sessions in Memcached, then they always expire after 300s and there's no way to provide an expiry.  Here's what's the code from deployment recipe chapter of web2py book:

from gluon.contrib.memcache import MemcacheClient
memcache_servers = ['127.0.0.1:11211']
cache.memcache = MemcacheClient(request, memcache_servers)
cache.ram = cache.disk = cache.memcache
..and..

from gluon.contrib.memdb import MEMDB
session.connect(request,response,db=MEMDB(cache.memcache))

If you do this and connect to memcached with e.g. "-vv" you can see that session data is passed in with 300s expiry.  I may be missing something obvious.  Can I suggest altering the API to be something like this:

session.connect(request, response, db=MEMDB(cache.memcache), session_expiry=3600)

Thanks!
Re: How do you change memcache session expiry? Niphlod 9/29/12 5:11 AM
yep, open a bug on http://code.google.com/p/web2py/issues/list
Re: How do you change memcache session expiry? Jose C 9/29/12 5:22 AM
Ah, I understand what you're saying.  I can't seem to find a way to do this either - if it is obvious it is escaping me too.  Perhaps consider opening an issue as a feature request or even possibly a bug - it is a common enough thing to want to do with sessions.

edit: I see Niphlod pipped me to the post on that suggestion.
Re: How do you change memcache session expiry? Massimo Di Pierro 9/29/12 7:50 AM
I would prefer the syntax:

session.connect(request, response, db=MEMDB(cache.memcache, session_expiry=3600))
Re: How do you change memcache session expiry? Massimo Di Pierro 9/29/12 7:51 AM
It also looks to me memdb should not be implemented as it is. It should be implemented as a plugin_adapter for DAL.
Re: How do you change memcache session expiry? Robert Clark 9/29/12 3:54 PM
Thanks everyone, have added an issue
Re: How do you change memcache session expiry? Paolo Valleri 1/18/13 1:03 AM
Hi all, I have just tried the solution proposed on the issue but it doen't work.
This is a really annoying bug not only because I have to login several times in a day but also because I can lose my edits if I don't save my changes in time (i.e, editing a wiki page).
It might be better to remove from the book the advise of storing the session in memcached unless we find a solution

Paolo