[Erp5-dev] caching per object
Jerome Perrin
jerome at nexedi.com
Fri Sep 15 00:59:00 CEST 2006
On Thu, Sep 14, 2006 at 09:04:29PM +0200, Bartek Gorny wrote:
> Hello
>
> I tried to use caching methods, but it seems that a simple statement like:
>
> def getWhatever(self):
> def cached_getWhatever():
> return something
> cached_getWhatever=CachingMethod(cached_getWhatever, id='an_id')
> return cached_getWhatever()
>
> does caching per portal type, which is very useful, but I need to do
> caching per object - is there any way to do this?
>
This is actually does "global" caching. Cache keys are parameters you pass
when calling the (wrapped) method.
To have a per portal type cache, you need:
def getWhatever(self):
def cached_getWhatever(portal_type=None):
return something
cached_getWhatever=CachingMethod(cached_getWhatever, id='an_id')
return cached_getWhatever(portal_type=self.getPortalType())
Note that portal_type parameter can be ignored by the 'real' method.
In the same way, you can imagine having per object caching passing
object uid:
def getWhatever(self):
def cached_getWhatever(uid=None):
return something
cached_getWhatever=CachingMethod(cached_getWhatever, id='an_id')
return cached_getWhatever(uid=self.getUid())
But I'm not sure it's a good idea, because if you have too much cache
keys, the whole caching mechanism will become inneficiant.
So, to make it simple, you will probably achive better performance with
a volatile attribute:
def getWhatever(self):
whatever = getattr(self, '_v_whatever', _MARKER)
if whatever is _MARKER:
self._v_whatever = whatever = self.computeWhatever()
return whatever
Jerome
More information about the Erp5-dev
mailing list