[Zope] Python, persistent objects and caching

Brian Lloyd Brian@digicool.com
Thu, 7 Sep 2000 09:39:35 -0400


> I'm developing a Python-based product that uses persistent objects in
> ZODB to store its configuration. No surprises there, but my product is
> also using volatile attributes to do a bit of caching of its own. The
> thing is, when ZODB takes a sweep every 60 seconds to flush it's own
> cache it will quite often flush my product as well, essentially
> rendering my 15 minute caching ineffective.
> 
> If there a way I can prevent this from happening?
> 
> References:
> http://www.zope.org/Members/MikeP/volatile
> http://www.zope.org/Members/Zen/tips/VolatileAttributes
> 
> Soren Roug

One approach would be to cache your data in the module's 
namespace. For ex:

mycache={}

class MyClassThatKeepsGoingAway:
  ...
  def get_data(self, id):
    if mycache.has_key(id):
      return mycache[id]
    else:
      ...

Note that my example is a little contrived, since using a module 
variable means that you will have to implement this in a way that 
is safe for a multi-threaded environment. You will also want to 
make sure that your module-level cache doesn't grow forever and 
take up a lot of memory.

Hope this helps!

Brian Lloyd (brian@digicool.com)