How do you change memcache session expiry?

1,036 views
Skip to first unread message

Robert Clark

unread,
Sep 27, 2012, 6:27:35 PM9/27/12
to web...@googlegroups.com
(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.

Jose C

unread,
Sep 28, 2012, 8:27:55 AM9/28/12
to web...@googlegroups.com
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?

Robert Clark

unread,
Sep 28, 2012, 11:24:07 PM9/28/12
to web...@googlegroups.com
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!

Niphlod

unread,
Sep 29, 2012, 8:11:10 AM9/29/12
to web...@googlegroups.com

Jose C

unread,
Sep 29, 2012, 8:23:47 AM9/29/12
to
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.

Massimo Di Pierro

unread,
Sep 29, 2012, 10:50:37 AM9/29/12
to web...@googlegroups.com
I would prefer the syntax:

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

Massimo Di Pierro

unread,
Sep 29, 2012, 10:51:10 AM9/29/12
to web...@googlegroups.com
It also looks to me memdb should not be implemented as it is. It should be implemented as a plugin_adapter for DAL.

Robert Clark

unread,
Sep 29, 2012, 6:54:03 PM9/29/12
to web...@googlegroups.com
Thanks everyone, have added an issue

Paolo valleri

unread,
Jan 18, 2013, 4:03:01 AM1/18/13
to web...@googlegroups.com
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

Joe Barnhart

unread,
Aug 7, 2018, 1:50:56 PM8/7/18
to web2py-users
Yes, I agree.  This needs to be REMOVED FROM THE BOOK.  I've just wasted most of a day before realizing memcache is useless for sessions.

-- Joe
Reply all
Reply to author
Forward
0 new messages