Show More
@@ -20,7 +20,6 b'' | |||
|
20 | 20 | |
|
21 | 21 | import logging |
|
22 | 22 | from dogpile.cache import register_backend |
|
23 | from dogpile.cache import make_region | |
|
24 | 23 | |
|
25 | 24 | register_backend( |
|
26 | 25 | "dogpile.cache.rc.memory_lru", "rhodecode.lib.rc_cache.backends", |
@@ -40,7 +39,7 b' log = logging.getLogger(__name__)' | |||
|
40 | 39 | from . import region_meta |
|
41 | 40 | from .utils import ( |
|
42 | 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 | 45 | def configure_dogpile_cache(settings): |
@@ -19,7 +19,10 b'' | |||
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | import os |
|
21 | 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 | 27 | from rhodecode.lib.utils import safe_str, sha1 |
|
25 | 28 | from . import region_meta |
@@ -27,6 +30,82 b' from . import region_meta' | |||
|
27 | 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 | 109 | def get_default_cache_settings(settings, prefixes=None): |
|
31 | 110 | prefixes = prefixes or [] |
|
32 | 111 | cache_settings = {} |
General Comments 0
You need to be logged in to leave comments.
Login now