Does web2py support memcached as a session store ?

164 views
Skip to first unread message

luckyboy

unread,
Mar 3, 2009, 10:10:10 PM3/3/09
to web2py Web Framework
Was never able to find a clear answer.. And if not, are there plans in
the (soon) coming releases for this ?

mdipierro

unread,
Mar 3, 2009, 10:16:58 PM3/3/09
to web2py Web Framework
No yet but very soon. Perhaps tonight?

Massimo

luckyboy

unread,
Mar 4, 2009, 1:31:43 AM3/4/09
to web2py Web Framework
Cool, can't wait..

mdipierro

unread,
Mar 4, 2009, 1:47:31 AM3/4/09
to web2py Web Framework
for now you should be able to do in like this:

from gluon.contrib.memcache import MemcacheClient
cache.memcache=MemcacheClient([.....]) ### [...] is a list of memcache
IPs
session=cache.memcache(response.session_id,lambda:{},1e8)
def custom_commit(response,session):
cache.memcache(response.session_id,lambda:session,-1)
SQLDB.close_all_instances(SQLDB.commit)
response._custom_commit=lambda response=response,session=session:
custom_commit(response,session)
session.forget()

Tiy can try with cache.ram or cache.disk first

Robin B

unread,
Mar 4, 2009, 9:51:10 AM3/4/09
to web2py Web Framework
I have been using MEMDB to store sessions in memcache since August
2008:

http://groups.google.com/group/web2py/browse_thread/thread/cd89f11de4d23b53/f67faac66a695ca2?hl=en&lnk=gst&q=memdb#f67faac66a695ca2

the code is under contrib:

from gluon.contrib.memdb import *
from google.appengine.api.memcache import Client
session.connect(request,response,db=MEMDB(Client()))

Robin

Markus Gritsch

unread,
Mar 4, 2009, 10:35:00 AM3/4/09
to web...@googlegroups.com
On Wed, Mar 4, 2009 at 3:51 PM, Robin B <rob...@gmail.com> wrote:
>
> I have been using MEMDB to store sessions in memcache since August
> 2008:
> http://groups.google.com/group/web2py/browse_thread/thread/cd89f11de4d23b53/f67faac66a695ca2?hl=en&lnk=gst&q=memdb#f67faac66a695ca2
>
> the code is under contrib:
>
> from gluon.contrib.memdb import *
> from google.appengine.api.memcache import Client
> session.connect(request,response,db=MEMDB(Client()))

Wow! I didn't know about this. This should probably be in den
scaffolding db.py file.

One question: When does this cache expire?

BTW, I tried it like you suggested:

try:
from gluon.contrib.gql import *
except:
db=SQLDB('sqlite://storage.db')
else:
db=GQLDB()


from gluon.contrib.memdb import *
from google.appengine.api.memcache import Client
session.connect(request, response, db=MEMDB(Client()))

db.define_table('key',
SQLField('now', 'datetime'),
SQLField('line', 'text'))

which fails because importing * from gluon.contrib.memdb overwrites
SQLField already defined in gluon.contrib.gql. I changed it to

from gluon.contrib.memdb import MEMDB

and it seems to work fine.

Kind regards,
Markus

Markus Gritsch

unread,
Mar 4, 2009, 3:28:07 PM3/4/09
to web...@googlegroups.com
On Wed, Mar 4, 2009 at 7:47 AM, mdipierro <mdip...@cs.depaul.edu> wrote:
>
> for now you should be able to do in like this:
>
> from gluon.contrib.memcache import MemcacheClient
> cache.memcache=MemcacheClient([.....]) ### [...] is a list of memcache
> IPs
> session=cache.memcache(response.session_id,lambda:{},1e8)
> def custom_commit(response,session):
>     cache.memcache(response.session_id,lambda:session,-1)
>     SQLDB.close_all_instances(SQLDB.commit)
> response._custom_commit=lambda response=response,session=session:
> custom_commit(response,session)
> session.forget()
>
> Tiy can try with cache.ram or cache.disk first

Can the session be stored in cache.ram without gluon.contrib.memcache?

Kind regards,
Markus

Robin B

unread,
Mar 4, 2009, 6:23:45 PM3/4/09
to web2py Web Framework
No one has published a 'RAMDB' (a DAL driver for RAM). It could
easily be implemented as a global dict, and it could be useful for say
caching, but I would not put sessions into ram for production. If you
have more than 1 application server/process, you will not be able to
share sessions between them. RAMDB would however be very useful for
development. :)

Robin

On Mar 4, 2:28 pm, Markus Gritsch <m.grit...@gmail.com> wrote:

mdipierro

unread,
Mar 4, 2009, 10:36:24 PM3/4/09
to web2py Web Framework
no but we have

SQLDB("sqlite:memory:")

mdipierro

unread,
Mar 4, 2009, 10:37:45 PM3/4/09
to web2py Web Framework
ignore this. On a second thought, this works but lives only for one
call, you want something more persistent.
It should be possible to built it with cache and sqlite:memory:

Massimo

Markus Gritsch

unread,
Mar 5, 2009, 2:33:52 AM3/5/09
to web2py
2009/3/5 mdipierro <mdip...@cs.depaul.edu>:

>
> ignore this. On a second thought, this works but lives only for one
> call, you want something more persistent.
> It should be possible to built it with cache and sqlite:memory:

I already tried this using Pythons module caching:

import applications.welcome.modules.ramdb
session.connect(request, response, db=applications.welcome.modules.ramdb.db)

where applications.welcome.modules.ramdb contains

from gluon.sql import SQLDB
db = SQLDB('sqlite:memory:')

Unfortunately this gives a traceback due to SQLites thread
restriction. The exception message reads:

"ProgrammingError: SQLite objects created in a thread can only be used
in that same thread.The object was created in thread id 1332 and this
is thread id 2584"

Kind regards,
Markus

Markus Gritsch

unread,
Mar 5, 2009, 3:48:21 PM3/5/09
to web...@googlegroups.com
On Wed, Mar 4, 2009 at 4:35 PM, Markus Gritsch <m.gr...@gmail.com> wrote:
> On Wed, Mar 4, 2009 at 3:51 PM, Robin B <rob...@gmail.com> wrote:
>>
>> I have been using MEMDB to store sessions in memcache since August
>> 2008:
>> http://groups.google.com/group/web2py/browse_thread/thread/cd89f11de4d23b53/f67faac66a695ca2?hl=en&lnk=gst&q=memdb#f67faac66a695ca2
>>
>> the code is under contrib:
>>
>> from gluon.contrib.memdb import *
>> from google.appengine.api.memcache import Client
>> session.connect(request,response,db=MEMDB(Client()))
>
> Wow!  I didn't know about this.  This should probably be in den
> scaffolding db.py file.

Please find attached db.py which includes the option to store sessions
in Memcache. It also fixes the damage to the comment formatting which
was done by PythonTidy.py.

This should go into the scaffolding app.

Markus

db.py

mdipierro

unread,
Mar 5, 2009, 4:38:43 PM3/5/09
to web2py Web Framework
I am not opposing, just asking... isnt this file already too fat?
Why include memcaching on GAE and not ram caching on non-GAE?

Massimo

On Mar 5, 2:48 pm, Markus Gritsch <m.grit...@gmail.com> wrote:
> On Wed, Mar 4, 2009 at 4:35 PM, Markus Gritsch <m.grit...@gmail.com> wrote:
> > On Wed, Mar 4, 2009 at 3:51 PM, Robin B <robi...@gmail.com> wrote:
>
> >> I have been using MEMDB to store sessions in memcache since August
> >> 2008:
> >>http://groups.google.com/group/web2py/browse_thread/thread/cd89f11de4...
>
> >> the code is under contrib:
>
> >> from gluon.contrib.memdb import *
> >> from google.appengine.api.memcache import Client
> >> session.connect(request,response,db=MEMDB(Client()))
>
> > Wow!  I didn't know about this.  This should probably be in den
> > scaffolding db.py file.
>
> Please find attached db.py which includes the option to store sessions
> in Memcache.  It also fixes the damage to the comment formatting which
> was done by PythonTidy.py.
>
> This should go into the scaffolding app.
>
> Markus
>
>  db.py
> 5KViewDownload

Markus Gritsch

unread,
Mar 6, 2009, 2:33:48 AM3/6/09
to web2py
2009/3/5 mdipierro <mdip...@cs.depaul.edu>:

>
> I am not opposing, just asking... isnt this file already too fat?

I agree it is already quite big. But until the auth stuff is
documented in the regular documentation I was not opposed to your
decision to add all those comments.

> Why include memcaching on GAE and not ram caching on non-GAE?

1. On GAE if one wants to use sessions, something has to be done
expicitely. Either store it in BigTable, or, which I just learned two
days ago, store it in Google Memcache, which is IMO a superior
solution, since it does not lead to piling up all those sessions in
BigTable. And since access to BigTable is under quota, it seems to be
another reason to have sessions stored in Memcache.

2. On non-GAE sessions work out of the box, so it is not mandatory to
tweak session handling. Memcache on non-GAE requires setting up a
Memcached server, so it would not be enough to just uncomment a few
lines in db.py. And caching the sessions in RAM is IMO currently not
possible with web2py. Am I wrong?

Kind regards,
Markus

Reply all
Reply to author
Forward
0 new messages