If you need to speed up your Python application, you should take a
look at plone.memoize, in particular its new cache decorator that’s
really easy to use:
>>> from plone.memoize.ram import cache
>>> def cache_key(fun, first, second):
... return (first, second)
>>> @cache(cache_key)
... def pow(first, second):
... print 'Someone or something called me'
... return first ** second
>>> pow(3, 2)
Someone or something called me
9
>>> pow(3, 2)
9
>>> pow(3, 3)
Somone or something called me
27
You can see that the cache key function determines which input
values result in the same output and thus can be cached. Cache key
functions receive exactly the same arguments as the functions that
they cache. Thus, key functions for methods can also use self for
determining a cache key. A cache key function may raise the
DontCache exception to signal that no caching should take place.
The cache storage backend can be freely chosen using the second
argument to the cache decorator:
>>> @cache(cache_key, cache_storage)
where cache_storage is a function that again takes all arguments of
the original function and returns a dict-like object for use as a
cache storage. plone.memoize has built-in support for memcached
and zope.app.cache as storages.
See the doctests for volatile.py for more examples.
For now, you’ll need to use the SVN version. A release containing
the cache decorator will follow soon.
Daniel Nouri: Need for speed -> plone.memoize
Originally from Planet Plone
http://plonewars.com/2007/06/10/daniel-nouri-need-for-speed-plonememoize/