##// END OF EJS Templates
cache: updated cache decorators based on latest code from dogpile
marcink -
r751:fb55dc24 default
parent child Browse files
Show More
@@ -18,6 +18,7 b''
18 import os
18 import os
19 import logging
19 import logging
20 import functools
20 import functools
21 from decorator import decorate
21
22
22 from dogpile.cache import CacheRegion
23 from dogpile.cache import CacheRegion
23 from dogpile.cache.util import compat
24 from dogpile.cache.util import compat
@@ -47,30 +48,35 b' class RhodeCodeCacheRegion(CacheRegion):'
47 if function_key_generator is None:
48 if function_key_generator is None:
48 function_key_generator = self.function_key_generator
49 function_key_generator = self.function_key_generator
49
50
50 def decorator(fn):
51 def get_or_create_for_user_func(key_generator, user_func, *arg, **kw):
51 if to_str is compat.string_type:
52 # backwards compatible
53 key_generator = function_key_generator(namespace, fn)
54 else:
55 key_generator = function_key_generator(namespace, fn, to_str=to_str)
56
57 @functools.wraps(fn)
58 def decorate(*arg, **kw):
59 key = key_generator(*arg, **kw)
60
61 @functools.wraps(fn)
62 def creator():
63 return fn(*arg, **kw)
64
52
65 if not condition:
53 if not condition:
66 log.debug('Calling un-cached func:%s', fn)
54 log.debug('Calling un-cached func:%s', user_func)
67 return creator()
55 return user_func(*arg, **kw)
56
57 key = key_generator(*arg, **kw)
68
58
69 timeout = expiration_time() if expiration_time_is_callable \
59 timeout = expiration_time() if expiration_time_is_callable \
70 else expiration_time
60 else expiration_time
71
61
72 log.debug('Calling cached fn:%s', fn)
62 log.debug('Calling cached fn:%s', user_func)
73 return self.get_or_create(key, creator, timeout, should_cache_fn)
63 return self.get_or_create(key, user_func, timeout, should_cache_fn, (arg, kw))
64
65 def cache_decorator(user_func):
66 if to_str is compat.string_type:
67 # backwards compatible
68 key_generator = function_key_generator(namespace, user_func)
69 else:
70 key_generator = function_key_generator(namespace, user_func, to_str=to_str)
71
72 def refresh(*arg, **kw):
73 """
74 Like invalidate, but regenerates the value instead
75 """
76 key = key_generator(*arg, **kw)
77 value = user_func(*arg, **kw)
78 self.set(key, value)
79 return value
74
80
75 def invalidate(*arg, **kw):
81 def invalidate(*arg, **kw):
76 key = key_generator(*arg, **kw)
82 key = key_generator(*arg, **kw)
@@ -84,23 +90,19 b' class RhodeCodeCacheRegion(CacheRegion):'
84 key = key_generator(*arg, **kw)
90 key = key_generator(*arg, **kw)
85 return self.get(key)
91 return self.get(key)
86
92
87 def refresh(*arg, **kw):
93 user_func.set = set_
88 key = key_generator(*arg, **kw)
94 user_func.invalidate = invalidate
89 value = fn(*arg, **kw)
95 user_func.get = get
90 self.set(key, value)
96 user_func.refresh = refresh
91 return value
97 user_func.key_generator = key_generator
98 user_func.original = user_func
92
99
93 decorate.set = set_
100 # Use `decorate` to preserve the signature of :param:`user_func`.
94 decorate.invalidate = invalidate
95 decorate.refresh = refresh
96 decorate.get = get
97 decorate.original = fn
98 decorate.key_generator = key_generator
99 decorate.__wrapped__ = fn
100
101
101 return decorate
102 return decorate(user_func, functools.partial(
103 get_or_create_for_user_func, key_generator))
102
104
103 return decorator
105 return cache_decorator
104
106
105
107
106 def make_region(*arg, **kw):
108 def make_region(*arg, **kw):
General Comments 0
You need to be logged in to leave comments. Login now