Cache - how to prevent session object to be cached?

9 views
Skip to first unread message

David Zejda

unread,
Mar 28, 2010, 6:33:00 AM3/28/10
to web2py-users
Please, look at the trivial controller:

class Blah:
def sessiontime(self):
return session.ctime

def cache_test():
import time
session.ctime = time.ctime()
b = cache.ram('blah',Blah,50)
return dict(cached=b.sessiontime(), current=session.ctime)

Because the time is not stored in the cached object, I would expect
equal 'cached' and 'current' value on every run.
But it seems that the session object is being stored together with
Blah somehow, so the values differ, starting with the second run for
the time for which the object is being held in cache:

cached : Sun Mar 28 12:22:43 2010
current : Sun Mar 28 12:23:10 2010

Is this behaviour intentional? I would expect to get fresh session
instead.

Regards :)
David

mdipierro

unread,
Mar 28, 2010, 10:39:12 AM3/28/10
to web2py-users
No. and frankly I do not understand why it behaves it this way. You
are doing something I never thought of: caching a constructor instead
of a function. I will check this.

Massimo

David Zejda

unread,
Mar 28, 2010, 4:20:31 PM3/28/10
to web2py-users
Hi Massimo,

i touched it to cache function instead of constructor:

class Blah:
def sessiontime(self):
return session.ctime

def cache_test():
import time
session.ctime = time.ctime()

def blah():
return Blah()
b = cache.ram('blah',blah,30)
return dict(cached=b.sessiontime(), current=session.ctime)

and result is the same.

David

David Zejda

unread,
Mar 30, 2010, 3:22:26 AM3/30/10
to web2py-users
I tried to move the part which asks session for the ctime value into a
model:

def sestime():
return session.ctime

In controller there is:

class Blah:
def sessiontime(self):
return sestime()

def cache_test():
import time
session.ctime = time.ctime()
def blah():
return Blah()
b = cache.ram('blah',blah,30)
return dict(cached=b.sessiontime(), current=session.ctime)

And still the same, the cached and current differ. The old session is
wired into the cache somehow, hmmm..

The examples are silly, of course, just to show the problem. I wish to
use the cache to store complex objects, constructed on results of
multiple queries. The objects have various methods operating on their
properties in context of session. Once I cache the objects (to avoid
tons of queries), my program breaks :(

David

David Zejda

unread,
Mar 31, 2010, 1:57:07 PM3/31/10
to web2py-users
I played with the cache more. If I pass current session to the object
loaded from cache, it works OK, I mean the times returned are equal:

class Blah:
def sessiontime(self, session):
globals()['session'] = session
return session.ctime

def cache_test():
import time
session.ctime = time.ctime()
def blah():
return Blah()
b = cache.ram('blah',blah,30)

return dict(cached=b.sessiontime(session), current=session.ctime)

Of course, this is not a solution, just a dirty hack.

Moreover the problem is not with session object only. It seems that
whole globals at the time of storage are being attached to the object,
including request, result etc. E.g.:

class Blah2:
def req_function(self):
return request.function

def cache_test_2():
b = cache.ram('blah',Blah2,30)
return dict(function=b.req_function())

def cache_test_other():
b = cache.ram('blah',Blah2,30)
return dict(function=b.req_function())

I would anticipate that it will return the name of request function
from which it is being called,
but blah pulled from cache prints name of function from the time of
construction :-(

David

David Zejda

unread,
Apr 3, 2010, 6:59:14 PM4/3/10
to web2py-users
Hmm.. I had to cut my objects into parts, those which hold data are
being cached while those with logic are not... kind of hack, but I'm
no more directly affected by the bug.

D.

mdipierro

unread,
Apr 3, 2010, 7:07:55 PM4/3/10
to web2py-users
The fact is I am not convinced it is web2py bug. It is a weird
behavior but I think it is more of a python issue than a web2py issue.
Reply all
Reply to author
Forward
0 new messages