##// END OF EJS Templates
caches: use different namespaces for conditional_cache wrapper....
marcink -
r2814:45a75c50 default
parent child Browse files
Show More
@@ -1079,7 +1079,7 b' class AuthUser(object):'
1079 'with caching: %s[%ss]' % (user, scope, cache_on, cache_seconds))
1079 'with caching: %s[%ss]' % (user, scope, cache_on, cache_seconds))
1080 start = time.time()
1080 start = time.time()
1081 compute = caches.conditional_cache(
1081 compute = caches.conditional_cache(
1082 'short_term', 'cache_desc',
1082 'short_term', 'cache_desc.{}'.format(user_id),
1083 condition=cache_on, func=_cached_perms_data)
1083 condition=cache_on, func=_cached_perms_data)
1084 result = compute(user_id, scope, user_is_admin,
1084 result = compute(user_id, scope, user_is_admin,
1085 user_inherit_default_permissions, explicit, algo,
1085 user_inherit_default_permissions, explicit, algo,
@@ -1154,7 +1154,7 b' class AuthUser(object):'
1154 RepoList(qry, perm_set=perm_def)]
1154 RepoList(qry, perm_set=perm_def)]
1155
1155
1156 compute = caches.conditional_cache(
1156 compute = caches.conditional_cache(
1157 'long_term', 'repo_acl_ids',
1157 'long_term', 'repo_acl_ids.{}'.format(self.user_id),
1158 condition=cache, func=_cached_repo_acl)
1158 condition=cache, func=_cached_repo_acl)
1159 return compute(self.user_id, perms, name_filter)
1159 return compute(self.user_id, perms, name_filter)
1160
1160
@@ -1180,7 +1180,7 b' class AuthUser(object):'
1180 RepoGroupList(qry, perm_set=perm_def)]
1180 RepoGroupList(qry, perm_set=perm_def)]
1181
1181
1182 compute = caches.conditional_cache(
1182 compute = caches.conditional_cache(
1183 'long_term', 'repo_group_acl_ids',
1183 'long_term', 'repo_group_acl_ids.{}'.format(self.user_id),
1184 condition=cache, func=_cached_repo_group_acl)
1184 condition=cache, func=_cached_repo_group_acl)
1185 return compute(self.user_id, perms, name_filter)
1185 return compute(self.user_id, perms, name_filter)
1186
1186
@@ -1206,7 +1206,7 b' class AuthUser(object):'
1206 UserGroupList(qry, perm_set=perm_def)]
1206 UserGroupList(qry, perm_set=perm_def)]
1207
1207
1208 compute = caches.conditional_cache(
1208 compute = caches.conditional_cache(
1209 'long_term', 'user_group_acl_ids',
1209 'long_term', 'user_group_acl_ids.{}'.format(self.user_id),
1210 condition=cache, func=_cached_user_group_acl)
1210 condition=cache, func=_cached_user_group_acl)
1211 return compute(self.user_id, perms, name_filter)
1211 return compute(self.user_id, perms, name_filter)
1212
1212
@@ -17,7 +17,7 b''
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20 import functools
21
21
22 import beaker
22 import beaker
23 import logging
23 import logging
@@ -148,7 +148,7 b' def get_repo_namespace_key(prefix, repo_'
148 return '{0}_{1}'.format(prefix, compute_key_from_params(repo_name))
148 return '{0}_{1}'.format(prefix, compute_key_from_params(repo_name))
149
149
150
150
151 def conditional_cache(region, prefix, condition, func):
151 def conditional_cache(region, cache_namespace, condition, func):
152 """
152 """
153 Conditional caching function use like::
153 Conditional caching function use like::
154 def _c(arg):
154 def _c(arg):
@@ -161,7 +161,7 b' def conditional_cache(region, prefix, co'
161 return compute(arg)
161 return compute(arg)
162
162
163 :param region: name of cache region
163 :param region: name of cache region
164 :param prefix: cache region prefix
164 :param cache_namespace: cache namespace
165 :param condition: condition for cache to be triggered, and
165 :param condition: condition for cache to be triggered, and
166 return data cached
166 return data cached
167 :param func: wrapped heavy function to compute
167 :param func: wrapped heavy function to compute
@@ -171,8 +171,34 b' def conditional_cache(region, prefix, co'
171 if condition:
171 if condition:
172 log.debug('conditional_cache: True, wrapping call of '
172 log.debug('conditional_cache: True, wrapping call of '
173 'func: %s into %s region cache', region, func)
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 wrapped = cached_region(func)
200 wrapped = cached_region(func)
201
176 return wrapped
202 return wrapped
177
203
178
204
General Comments 0
You need to be logged in to leave comments. Login now