[Zope] Method cache (Memoize) for Zope

Max M maxm at mxm.dk
Sun Oct 9 12:34:07 EDT 2005


Has anybody written a Memoize-like decorator for Zope?

I imagine that it could be a nice feature to have a standard decorator 
for Zope that would cache results in a _v_ volatile variable.

I usually do it a bit like this::

     def _cache_it(self, meth, *args, **kwargs):
         cache_name = '_v_%s' % meth.__name__
         if not hasattr(self, cache_name):
             result = meth(*args, **kwargs)
             setattr(self, cache_name, result)
         return getattr(self, cache_name)


     def _some_function(self):
         # do expensive calculations and return result
         return 42


     def some_function(self, *args, **kwargs):
         "Returns cached values"
         return self._cache_it(self._some_function, *args, **kwargs)

But I guess that there must be someone who has allready written a more 
generic way to do it via generators?


-- 

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science



More information about the Zope mailing list