summaryrefslogtreecommitdiff
path: root/bin/tools/misc.py
diff options
context:
space:
mode:
Diffstat (limited to 'bin/tools/misc.py')
-rw-r--r--bin/tools/misc.py26
1 files changed, 23 insertions, 3 deletions
diff --git a/bin/tools/misc.py b/bin/tools/misc.py
index 993f4eb03f8..6241365f8d0 100644
--- a/bin/tools/misc.py
+++ b/bin/tools/misc.py
@@ -536,14 +536,34 @@ class cache(object):
Use it as a decorator of the function you plan to cache
Timeout: 0 = no timeout, otherwise in seconds
"""
-
- def __init__(self, timeout=10000, skiparg=2, multi=None):
+
+ __caches = []
+
+ def __init__(self, timeout=None, skiparg=2, multi=None):
assert skiparg >= 2 # at least self and cr
- self.timeout = timeout
+ if timeout is None:
+ self.timeout = config['cache_timeout']
+ else:
+ self.timeout = timeout
self.skiparg = skiparg
self.multi = multi
self.lasttime = time.time()
self.cache = {}
+
+ cache.__caches.append(self)
+
+ @classmethod
+ def clean_cache_for_db(cls, dbname):
+ def get_dbname_from_key(key):
+ for e in key:
+ if e[0] == 'dbname':
+ return e[1]
+ return None
+
+ for cache in cls.__caches:
+ keys_to_del = [key for key in cache.cache if get_dbname_from_key(key) == dbname]
+ for key in keys_to_del:
+ del cache.cache[key]
def __call__(self, fn):
arg_names = inspect.getargspec(fn)[0][self.skiparg:]