Commit 224f9899 authored by Ivan Tyagov's avatar Ivan Tyagov

Implemented global clearCache() for backwards compatability with old Caching...

Implemented global clearCache() for backwards compatability with old Caching system as well some tests.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@11121 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent d96e0444
...@@ -30,9 +30,9 @@ import string ...@@ -30,9 +30,9 @@ import string
from time import time from time import time
from AccessControl.SecurityInfo import allow_class from AccessControl.SecurityInfo import allow_class
from AccessControl import getSecurityManager from AccessControl import getSecurityManager
#from Products import ERP5Cache
from zLOG import LOG from zLOG import LOG
DEFAULT_CACHE_SCOPE = 'GLOBAL'
is_cache_initialized = 0 is_cache_initialized = 0
def initializePortalCachingProperties(self): def initializePortalCachingProperties(self):
...@@ -84,7 +84,6 @@ class CacheFactory: ...@@ -84,7 +84,6 @@ class CacheFactory:
quick_cached = self.quick_cache.get(cache_id, scope) quick_cached = self.quick_cache.get(cache_id, scope)
if quick_cached is not None: if quick_cached is not None:
#print "HIT RAM", self.quick_cache
return quick_cached.getValue() return quick_cached.getValue()
else: else:
## not in local, check if it's in shared ## not in local, check if it's in shared
...@@ -164,19 +163,18 @@ class CachingMethod: ...@@ -164,19 +163,18 @@ 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
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 """
## CachingMethod is global Zope class and thus we must make sure ## CachingMethod is global Zope class and thus we must make sure
#erp5_site_id = kwd.get('portal_path', ('','erp5'))[1] #erp5_site_id = kwd.get('portal_path', ('','erp5'))[1]
## cache scope is based on user which is a kwd argument ## cache scope is based on user which is a kwd argument
scope = kwd.get('scope', 'GLOBAL') scope = kwd.get('scope', DEFAULT_CACHE_SCOPE)
## generate unique cache id ## generate unique cache id
cache_id = self.generateCacheId(self.id, *args, **kwd) cache_id = self.generateCacheId(self.id, *args, **kwd)
try: try:
## try to get value from cache in a try block ## try to get value from cache in a try block
## which is faster than checking for keys ## which is faster than checking for keys
...@@ -195,17 +193,25 @@ class CachingMethod: ...@@ -195,17 +193,25 @@ class CachingMethod:
def generateCacheId(self, method_id, *args, **kwd): def generateCacheId(self, method_id, *args, **kwd):
""" Generate proper cache id based on *args and **kwd """ """ Generate proper cache id based on *args and **kwd """
cache_id = [method_id] if args==() and kwd == {}:
key_list = kwd.keys() ## we have static method_id without any argument passed
key_list.sort() ## so we return it as it is.
for arg in args: return str(method_id)
cache_id.append((None, arg)) else:
for key in key_list: ## generate cache id out of arguments passed.
cache_id.append((key, str(kwd[key]))) ## depending on arguments we may have different
cache_id = str(cache_id) ## cache_id for same method_id
## because some cache backends don't allow some chars in cached id we make sure to replace them cache_id = [method_id]
cache_id = cache_id.translate(self._cache_id_translate_table) key_list = kwd.keys()
return cache_id key_list.sort()
for arg in args:
cache_id.append((None, arg))
for key in key_list:
cache_id.append((key, str(kwd[key])))
cache_id = str(cache_id)
## because some cache backends don't allow some chars in cached id we make sure to replace them
cache_id = cache_id.translate(self._cache_id_translate_table)
return cache_id
allow_class(CachingMethod) allow_class(CachingMethod)
...@@ -235,10 +241,20 @@ def disableReadOnlyTransactionCache(context): ...@@ -235,10 +241,20 @@ def disableReadOnlyTransactionCache(context):
## TODO: Check if it make sense to keep them any more ## ## TODO: Check if it make sense to keep them any more ##
######################################################## ########################################################
def clearCache(method_id=None): def clearCache(method_id = None, scope = DEFAULT_CACHE_SCOPE):
""" """
Clear the cache. Clear the cache.
If method_id is specified, it clears the cache only for this method, If method_id is specified, it clears the cache only for this method,
otherwise, it clears the whole cache. otherwise, it clears the whole cache. Make sure to specify scope otherwise
default one will be used.
""" """
pass if method_id is not None:
## clear cache factories and plugins for specified method_id
for cf_id, cf_obj in CachingMethod.factories.items():
for cp in cf_obj.getCachePluginList():
cp.delete(method_id, scope)
else:
## clear (flush) whole cache
for cf_obj in CachingMethod.factories.values():
for cp in cf_obj.getCachePluginList():
cp.clearCache()
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