##// END OF EJS Templates
caches: introduce new conditional cache function.
marcink -
r2891:63610fd1 default
parent child Browse files
Show More
@@ -20,7 +20,6 b''
20
20
21 import logging
21 import logging
22 from dogpile.cache import register_backend
22 from dogpile.cache import register_backend
23 from dogpile.cache import make_region
24
23
25 register_backend(
24 register_backend(
26 "dogpile.cache.rc.memory_lru", "rhodecode.lib.rc_cache.backends",
25 "dogpile.cache.rc.memory_lru", "rhodecode.lib.rc_cache.backends",
@@ -40,7 +39,7 b' log = logging.getLogger(__name__)'
40 from . import region_meta
39 from . import region_meta
41 from .utils import (
40 from .utils import (
42 get_default_cache_settings, key_generator, get_or_create_region,
41 get_default_cache_settings, key_generator, get_or_create_region,
43 clear_cache_namespace)
42 clear_cache_namespace, make_region)
44
43
45
44
46 def configure_dogpile_cache(settings):
45 def configure_dogpile_cache(settings):
@@ -19,7 +19,10 b''
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 import os
20 import os
21 import logging
21 import logging
22 from dogpile.cache import make_region
22 import functools
23
24 from dogpile.cache import CacheRegion
25 from dogpile.cache.util import compat
23
26
24 from rhodecode.lib.utils import safe_str, sha1
27 from rhodecode.lib.utils import safe_str, sha1
25 from . import region_meta
28 from . import region_meta
@@ -27,6 +30,82 b' from . import region_meta'
27 log = logging.getLogger(__name__)
30 log = logging.getLogger(__name__)
28
31
29
32
33 class RhodeCodeCacheRegion(CacheRegion):
34
35 def conditional_cache_on_arguments(
36 self, namespace=None,
37 expiration_time=None,
38 should_cache_fn=None,
39 to_str=compat.string_type,
40 function_key_generator=None,
41 condition=True):
42 """
43 Custom conditional decorator, that will not touch any dogpile internals if
44 condition isn't meet. This works a bit different than should_cache_fn
45 And it's faster in cases we don't ever want to compute cached values
46 """
47 expiration_time_is_callable = compat.callable(expiration_time)
48
49 if function_key_generator is None:
50 function_key_generator = self.function_key_generator
51
52 def decorator(fn):
53 if to_str is compat.string_type:
54 # backwards compatible
55 key_generator = function_key_generator(namespace, fn)
56 else:
57 key_generator = function_key_generator(namespace, fn, to_str=to_str)
58
59 @functools.wraps(fn)
60 def decorate(*arg, **kw):
61 key = key_generator(*arg, **kw)
62
63 @functools.wraps(fn)
64 def creator():
65 return fn(*arg, **kw)
66
67 if not condition:
68 return creator()
69
70 timeout = expiration_time() if expiration_time_is_callable \
71 else expiration_time
72
73 return self.get_or_create(key, creator, timeout, should_cache_fn)
74
75 def invalidate(*arg, **kw):
76 key = key_generator(*arg, **kw)
77 self.delete(key)
78
79 def set_(value, *arg, **kw):
80 key = key_generator(*arg, **kw)
81 self.set(key, value)
82
83 def get(*arg, **kw):
84 key = key_generator(*arg, **kw)
85 return self.get(key)
86
87 def refresh(*arg, **kw):
88 key = key_generator(*arg, **kw)
89 value = fn(*arg, **kw)
90 self.set(key, value)
91 return value
92
93 decorate.set = set_
94 decorate.invalidate = invalidate
95 decorate.refresh = refresh
96 decorate.get = get
97 decorate.original = fn
98 decorate.key_generator = key_generator
99
100 return decorate
101
102 return decorator
103
104
105 def make_region(*arg, **kw):
106 return RhodeCodeCacheRegion(*arg, **kw)
107
108
30 def get_default_cache_settings(settings, prefixes=None):
109 def get_default_cache_settings(settings, prefixes=None):
31 prefixes = prefixes or []
110 prefixes = prefixes or []
32 cache_settings = {}
111 cache_settings = {}
@@ -120,7 +120,6 b' def get_repo_group_slug(request):'
120 # pyramid
120 # pyramid
121 _group = request.matchdict.get('repo_group_name')
121 _group = request.matchdict.get('repo_group_name')
122
122
123
124 if _group:
123 if _group:
125 _group = _group.rstrip('/')
124 _group = _group.rstrip('/')
126 return _group
125 return _group
General Comments 0
You need to be logged in to leave comments. Login now