Discussion:
[Erp5-dev] caching per object
Bartek Gorny
2006-09-14 19:04:29 UTC
Permalink
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?

Bartek
--
"Software is largely a service industry operating under the persistent
but unfounded delusion that it is a manufacturing industry."
Eric S.Raymond, "The Magic Cauldron"
Bartek Gorny
2006-09-14 21:28:52 UTC
Permalink
Post by Bartek Gorny
Hello
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.
Ah, got it - that's why I thought it caches per type - in my case the
wrapped method got an argument which was different for every portal
type.

BTW, is this cache system-wide, or bound to a site or user session?
So, to make it simple, you will probably achive better performance with
I think you're right. Thanks for explanation!

Bartek
whatever = getattr(self, '_v_whatever', _MARKER)
self._v_whatever = whatever = self.computeWhatever()
return whatever
Jerome
_______________________________________________
Erp5-dev mailing list
Erp5-dev at erp5.org
http://erp5.org/mailman/listinfo/erp5-dev
--
"Software is largely a service industry operating under the persistent
but unfounded delusion that it is a manufacturing industry."
Eric S.Raymond, "The Magic Cauldron"
Jérôme Perrin
2006-09-15 07:12:37 UTC
Permalink
Post by Bartek Gorny
Post by Bartek Gorny
Hello
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.
Ah, got it - that's why I thought it caches per type - in my case the
wrapped method got an argument which was different for every portal
type.
BTW, is this cache system-wide, or bound to a site or user session?
System-wide, but you can for example pass the user name as a parameter to make
it per user.
--
J?rome
Jerome Perrin
2006-09-14 22:59:00 UTC
Permalink
Post by Bartek Gorny
Hello
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
Loading...