Commit 9bc0ed81 authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki

* add 'cache_id_func' argument in CachingMethod.__init__ to specify how to...

* add 'cache_id_func' argument in CachingMethod.__init__ to specify how to generate cache_id, that is especially useful when you want to use for instance methods where args contain 'self'.
* use 'cache_id_func' in ERP5TypeInformation._getRawActionInformationList().


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@29691 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 21950585
...@@ -157,7 +157,8 @@ class CachingMethod: ...@@ -157,7 +157,8 @@ class CachingMethod:
factories = {} factories = {}
def __init__(self, callable_object, id, cache_duration=180, def __init__(self, callable_object, id, cache_duration=180,
cache_factory=DEFAULT_CACHE_FACTORY): cache_factory=DEFAULT_CACHE_FACTORY,
cache_id_func=None):
"""Wrap a callable object in a caching method. """Wrap a callable object in a caching method.
callable_object must be callable. callable_object must be callable.
...@@ -175,6 +176,7 @@ class CachingMethod: ...@@ -175,6 +176,7 @@ class CachingMethod:
self.callable_object = callable_object self.callable_object = callable_object
self.cache_duration = cache_duration self.cache_duration = cache_duration
self.cache_factory = cache_factory self.cache_factory = cache_factory
self.cache_id_func = cache_id_func
def __call__(self, *args, **kwd): def __call__(self, *args, **kwd):
"""Call the method or return cached value using appropriate cache plugin """ """Call the method or return cached value using appropriate cache plugin """
...@@ -221,6 +223,9 @@ class CachingMethod: ...@@ -221,6 +223,9 @@ class CachingMethod:
## generate cache id out of arguments passed. ## generate cache id out of arguments passed.
## depending on arguments we may have different ## depending on arguments we may have different
## cache_id for same method_id ## cache_id for same method_id
cache_id_func = self.cache_id_func
if cache_id_func is not None:
return cache_id_func(method_id, *args, **kwd)
return str((method_id, args, kwd)) return str((method_id, args, kwd))
allow_class(CachingMethod) allow_class(CachingMethod)
......
...@@ -530,7 +530,8 @@ class ERP5TypeInformation(XMLObject, ...@@ -530,7 +530,8 @@ class ERP5TypeInformation(XMLObject,
_getRawActionInformationList = CachingMethod( _getRawActionInformationList = CachingMethod(
_getRawActionInformationList, _getRawActionInformationList,
id='_getRawActionInformationList', id='_getRawActionInformationList',
cache_factory='erp5_content_long') cache_factory='erp5_content_long',
cache_id_func=lambda method_id, *args, **kwd:str(method_id))
security.declarePrivate('getRawActionInformationList') security.declarePrivate('getRawActionInformationList')
def getRawActionInformationList(self): def getRawActionInformationList(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment