Commit a9d02568 authored by Jérome Perrin's avatar Jérome Perrin

clearCache now supports method_id argument to clear the cache of this CachingMethod only

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@6882 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 8b208fad
......@@ -147,8 +147,19 @@ class CachingMethod:
allow_class(CachingMethod)
def clearCache():
def clearCache(method_id=None):
"""Clear the cache.
If method_id is specified, it clears the cache only for this method,
otherwise, it clears the whole cache."""
if method_id is None:
CachingMethod.cached_object_dict.clear()
else:
caching_method_keys = CachingMethod.cached_object_dict.keys()
for key in caching_method_keys :
# CachingMethod dict contains a string representation of a list
# of tuples keys.
if method_id in key :
del CachingMethod.cached_object_dict[key]
# TransactionCache is a cache per transaction. The purpose of this cache is
# to accelerate some heavy read-only operations. Note that this must not be
......
......@@ -200,8 +200,35 @@ class TestERP5Type(ERP5TypeTestCase):
self.assertEquals(organisation.corporate_name,'Nexedi')
self.assertEquals(organisation.default_telephone.corporate_name,'Toto')
def test_06_CachingMethod(self):
"""Tests Caching methods."""
cached_var1 = cached_var1_orig = 'cached_var1'
cached_var2 = cached_var2_orig = 'cached_var2'
def _cache1():
return cached_var1
def _cache2():
return cached_var2
from Products.ERP5Type.Cache import CachingMethod, clearCache
cache1 = CachingMethod(_cache1, id='_cache1')
cache2 = CachingMethod(_cache2, id='_cache2')
self.assertEquals(cache1(), cached_var1)
self.assertEquals(cache2(), cached_var2)
cached_var1 = 'cached_var1 (modified)'
cached_var2 = 'cached_var2 (modified)'
self.assertEquals(cache1(), cached_var1_orig)
# clearCache with a method argument only clear this cache
clearCache(method_id = '_cache1')
self.assertEquals(cache1(), cached_var1)
self.assertEquals(cache2(), cached_var2_orig)
# clearCache with no arguments clear all caches
clearCache()
self.assertEquals(cache2(), cached_var2)
if __name__ == '__main__':
framework()
......
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