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