Show More
@@ -1079,7 +1079,7 b' class AuthUser(object):' | |||
|
1079 | 1079 | 'with caching: %s[%ss]' % (user, scope, cache_on, cache_seconds)) |
|
1080 | 1080 | start = time.time() |
|
1081 | 1081 | compute = caches.conditional_cache( |
|
1082 | 'short_term', 'cache_desc', | |
|
1082 | 'short_term', 'cache_desc.{}'.format(user_id), | |
|
1083 | 1083 | condition=cache_on, func=_cached_perms_data) |
|
1084 | 1084 | result = compute(user_id, scope, user_is_admin, |
|
1085 | 1085 | user_inherit_default_permissions, explicit, algo, |
@@ -1154,7 +1154,7 b' class AuthUser(object):' | |||
|
1154 | 1154 | RepoList(qry, perm_set=perm_def)] |
|
1155 | 1155 | |
|
1156 | 1156 | compute = caches.conditional_cache( |
|
1157 | 'long_term', 'repo_acl_ids', | |
|
1157 | 'long_term', 'repo_acl_ids.{}'.format(self.user_id), | |
|
1158 | 1158 | condition=cache, func=_cached_repo_acl) |
|
1159 | 1159 | return compute(self.user_id, perms, name_filter) |
|
1160 | 1160 | |
@@ -1180,7 +1180,7 b' class AuthUser(object):' | |||
|
1180 | 1180 | RepoGroupList(qry, perm_set=perm_def)] |
|
1181 | 1181 | |
|
1182 | 1182 | compute = caches.conditional_cache( |
|
1183 | 'long_term', 'repo_group_acl_ids', | |
|
1183 | 'long_term', 'repo_group_acl_ids.{}'.format(self.user_id), | |
|
1184 | 1184 | condition=cache, func=_cached_repo_group_acl) |
|
1185 | 1185 | return compute(self.user_id, perms, name_filter) |
|
1186 | 1186 | |
@@ -1206,7 +1206,7 b' class AuthUser(object):' | |||
|
1206 | 1206 | UserGroupList(qry, perm_set=perm_def)] |
|
1207 | 1207 | |
|
1208 | 1208 | compute = caches.conditional_cache( |
|
1209 | 'long_term', 'user_group_acl_ids', | |
|
1209 | 'long_term', 'user_group_acl_ids.{}'.format(self.user_id), | |
|
1210 | 1210 | condition=cache, func=_cached_user_group_acl) |
|
1211 | 1211 | return compute(self.user_id, perms, name_filter) |
|
1212 | 1212 |
@@ -17,7 +17,7 b'' | |||
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | ||
|
20 | import functools | |
|
21 | 21 | |
|
22 | 22 | import beaker |
|
23 | 23 | import logging |
@@ -148,7 +148,7 b' def get_repo_namespace_key(prefix, repo_' | |||
|
148 | 148 | return '{0}_{1}'.format(prefix, compute_key_from_params(repo_name)) |
|
149 | 149 | |
|
150 | 150 | |
|
151 |
def conditional_cache(region, |
|
|
151 | def conditional_cache(region, cache_namespace, condition, func): | |
|
152 | 152 | """ |
|
153 | 153 | Conditional caching function use like:: |
|
154 | 154 | def _c(arg): |
@@ -161,7 +161,7 b' def conditional_cache(region, prefix, co' | |||
|
161 | 161 | return compute(arg) |
|
162 | 162 | |
|
163 | 163 | :param region: name of cache region |
|
164 | :param prefix: cache region prefix | |
|
164 | :param cache_namespace: cache namespace | |
|
165 | 165 | :param condition: condition for cache to be triggered, and |
|
166 | 166 | return data cached |
|
167 | 167 | :param func: wrapped heavy function to compute |
@@ -171,8 +171,34 b' def conditional_cache(region, prefix, co' | |||
|
171 | 171 | if condition: |
|
172 | 172 | log.debug('conditional_cache: True, wrapping call of ' |
|
173 | 173 | 'func: %s into %s region cache', region, func) |
|
174 | cached_region = _cache_decorate((prefix,), None, None, region) | |
|
174 | ||
|
175 | def _cache_wrap(region_name, cache_namespace): | |
|
176 | """Return a caching wrapper""" | |
|
177 | ||
|
178 | def decorate(func): | |
|
179 | @functools.wraps(func) | |
|
180 | def cached(*args, **kwargs): | |
|
181 | if kwargs: | |
|
182 | raise AttributeError( | |
|
183 | 'Usage of kwargs is not allowed. ' | |
|
184 | 'Use only positional arguments in wrapped function') | |
|
185 | manager = get_cache_manager(region_name, cache_namespace) | |
|
186 | cache_key = compute_key_from_params(*args) | |
|
187 | ||
|
188 | def go(): | |
|
189 | return func(*args, **kwargs) | |
|
190 | ||
|
191 | # save org function name | |
|
192 | go.__name__ = '_cached_%s' % (func.__name__,) | |
|
193 | ||
|
194 | return manager.get(cache_key, createfunc=go) | |
|
195 | return cached | |
|
196 | ||
|
197 | return decorate | |
|
198 | ||
|
199 | cached_region = _cache_wrap(region, cache_namespace) | |
|
175 | 200 | wrapped = cached_region(func) |
|
201 | ||
|
176 | 202 | return wrapped |
|
177 | 203 | |
|
178 | 204 |
General Comments 0
You need to be logged in to leave comments.
Login now