You should never access the cache internals like

   x = cache.ram.storage['myObj'][1]   # DON'T

because it is not thread safe. You should have something like:

def makeMe():
   return cStringIO.StringIO()

def getMe():
   x = cache.ram("myObj", lambda:makeMe(), 5)
   response.headers['Content-Type']='image/png'
   response.stream(x)

You only need to call getMe and if "myObj" does not exist or it older
than 5 secods, it will make and cache a new one. You actually do not
need at all a makeMe() since you can do

def getMe():
   x = cache.ram("myObj", lambda:cStringIO.StringIO(), 5)
   response.headers['Content-Type']='image/png'
   response.stream(x)


Mind that the particular object you are using cString().String() make
be accessed by multiple threads at the same time after you extract it
from cache. This may or may not be OK, depends on the details of the
app.

Massimo


On Mar 25, 1:56 pm, Mark Larsen <larsen...@gmail.com> wrote:
> Web2Pyers,
>
> What the appropriate way to use cache.ram?  Let's say I want to cache an
> object (in my case its a cStringIO).  In my controller function -->
>
> def makeMe():
>   x = cStringIO.StringIO()
>   cache.ram("myObj", lambda: x, 5)
>
> def getMe():
>   x = cache.ram.storage['myObj'][1]
>   response.headers['Content-Type']='image/png'
>   response.stream(x)
>
> While this works, am I missing something?
>
> Also is the cache object unique for each session?  Does Web2py have any
> concept of a shared cache for all sessons?
>
> Thanks,
>
> Mark
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" group.
To post to this group, send email to web2py@googlegroups.com
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to