From a9d025687266123331f4c0c756dbc88d5fa11684 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com> Date: Mon, 24 Apr 2006 12:04:01 +0000 Subject: [PATCH] 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 --- product/ERP5Type/Cache.py | 15 +++++++++++-- product/ERP5Type/tests/testERP5Type.py | 31 ++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/product/ERP5Type/Cache.py b/product/ERP5Type/Cache.py index af5423c7a8..9e3652e035 100644 --- a/product/ERP5Type/Cache.py +++ b/product/ERP5Type/Cache.py @@ -147,8 +147,19 @@ class CachingMethod: allow_class(CachingMethod) -def clearCache(): - CachingMethod.cached_object_dict.clear() +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 diff --git a/product/ERP5Type/tests/testERP5Type.py b/product/ERP5Type/tests/testERP5Type.py index 9cee177793..73a3981f59 100644 --- a/product/ERP5Type/tests/testERP5Type.py +++ b/product/ERP5Type/tests/testERP5Type.py @@ -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() -- 2.30.9