Show More
@@ -23,20 +23,20 b' import errno' | |||
|
23 | 23 | import logging |
|
24 | 24 | |
|
25 | 25 | import msgpack |
|
26 | import redis | |
|
26 | 27 | import gevent |
|
27 | import redis | |
|
28 | 28 | |
|
29 | 29 | from dogpile.cache.api import CachedValue |
|
30 | 30 | from dogpile.cache.backends import memory as memory_backend |
|
31 | 31 | from dogpile.cache.backends import file as file_backend |
|
32 | 32 | from dogpile.cache.backends import redis as redis_backend |
|
33 |
from dogpile.cache.backends.file import NO_VALUE, |
|
|
33 | from dogpile.cache.backends.file import NO_VALUE, FileLock | |
|
34 | 34 | from dogpile.cache.util import memoized_property |
|
35 | 35 | |
|
36 | 36 | from pyramid.settings import asbool |
|
37 | 37 | |
|
38 | 38 | from rhodecode.lib.memory_lru_dict import LRUDict, LRUDictDebug |
|
39 |
from rhodecode.lib.utils import safe_str |
|
|
39 | from rhodecode.lib.utils import safe_str | |
|
40 | 40 | |
|
41 | 41 | |
|
42 | 42 | _default_max_size = 1024 |
@@ -74,7 +74,7 b' class PickleSerializer(object):' | |||
|
74 | 74 | |
|
75 | 75 | def _dumps(self, value, safe=False): |
|
76 | 76 | try: |
|
77 |
return |
|
|
77 | return pickle.dumps(value) | |
|
78 | 78 | except Exception: |
|
79 | 79 | if safe: |
|
80 | 80 | return NO_VALUE |
@@ -83,7 +83,7 b' class PickleSerializer(object):' | |||
|
83 | 83 | |
|
84 | 84 | def _loads(self, value, safe=True): |
|
85 | 85 | try: |
|
86 |
return |
|
|
86 | return pickle.loads(value) | |
|
87 | 87 | except Exception: |
|
88 | 88 | if safe: |
|
89 | 89 | return NO_VALUE |
@@ -187,7 +187,7 b' class FileNamespaceBackend(PickleSeriali' | |||
|
187 | 187 | |
|
188 | 188 | with self._dbm_file(True) as dbm: |
|
189 | 189 | try: |
|
190 | return filter(cond, dbm.keys()) | |
|
190 | return list(filter(cond, list(dbm.keys()))) | |
|
191 | 191 | except Exception: |
|
192 | 192 | log.error('Failed to fetch DBM keys from DB: %s', self.get_store()) |
|
193 | 193 | raise |
@@ -300,7 +300,7 b' class BaseRedisBackend(redis_backend.Red' | |||
|
300 | 300 | |
|
301 | 301 | def get_mutex(self, key): |
|
302 | 302 | if self.distributed_lock: |
|
303 |
lock_key = |
|
|
303 | lock_key = '_lock_{0}'.format(safe_str(key)) | |
|
304 | 304 | return get_mutex_lock(self.client, lock_key, self._lock_timeout, |
|
305 | 305 | auto_renewal=self._lock_auto_renewal) |
|
306 | 306 | else: |
@@ -21,13 +21,13 b' import os' | |||
|
21 | 21 | import time |
|
22 | 22 | import logging |
|
23 | 23 | import functools |
|
24 | import decorator | |
|
24 | 25 | import threading |
|
25 | 26 | |
|
26 | 27 | from dogpile.cache import CacheRegion |
|
27 | from dogpile.cache.util import compat | |
|
28 | 28 | |
|
29 | 29 | import rhodecode |
|
30 |
from rhodecode.lib.utils import safe_ |
|
|
30 | from rhodecode.lib.utils import safe_bytes, sha1 | |
|
31 | 31 | from rhodecode.lib.utils2 import safe_unicode, str2bool |
|
32 | 32 | from rhodecode.model.db import Session, CacheKey, IntegrityError |
|
33 | 33 | |
@@ -50,7 +50,7 b' class RhodeCodeCacheRegion(CacheRegion):' | |||
|
50 | 50 | self, namespace=None, |
|
51 | 51 | expiration_time=None, |
|
52 | 52 | should_cache_fn=None, |
|
53 |
to_str= |
|
|
53 | to_str=str, | |
|
54 | 54 | function_key_generator=None, |
|
55 | 55 | condition=True): |
|
56 | 56 | """ |
@@ -58,74 +58,19 b' class RhodeCodeCacheRegion(CacheRegion):' | |||
|
58 | 58 | condition isn't meet. This works a bit different than should_cache_fn |
|
59 | 59 | And it's faster in cases we don't ever want to compute cached values |
|
60 | 60 | """ |
|
61 |
expiration_time_is_callable = |
|
|
61 | expiration_time_is_callable = callable(expiration_time) | |
|
62 | 62 | |
|
63 | 63 | if function_key_generator is None: |
|
64 | 64 | function_key_generator = self.function_key_generator |
|
65 | 65 | |
|
66 | # workaround for py2 and cython problems, this block should be removed | |
|
67 | # once we've migrated to py3 | |
|
68 | if 'cython' == 'cython': | |
|
69 | def decorator(fn): | |
|
70 | if to_str is compat.string_type: | |
|
71 | # backwards compatible | |
|
72 | key_generator = function_key_generator(namespace, fn) | |
|
73 | else: | |
|
74 | key_generator = function_key_generator(namespace, fn, to_str=to_str) | |
|
75 | ||
|
76 | @functools.wraps(fn) | |
|
77 | def decorate(*arg, **kw): | |
|
78 | key = key_generator(*arg, **kw) | |
|
79 | ||
|
80 | @functools.wraps(fn) | |
|
81 | def creator(): | |
|
82 | return fn(*arg, **kw) | |
|
83 | ||
|
84 | if not condition: | |
|
85 | return creator() | |
|
86 | ||
|
87 | timeout = expiration_time() if expiration_time_is_callable \ | |
|
88 | else expiration_time | |
|
89 | ||
|
90 | return self.get_or_create(key, creator, timeout, should_cache_fn) | |
|
91 | ||
|
92 | def invalidate(*arg, **kw): | |
|
93 | key = key_generator(*arg, **kw) | |
|
94 | self.delete(key) | |
|
95 | ||
|
96 | def set_(value, *arg, **kw): | |
|
97 | key = key_generator(*arg, **kw) | |
|
98 | self.set(key, value) | |
|
99 | ||
|
100 | def get(*arg, **kw): | |
|
101 | key = key_generator(*arg, **kw) | |
|
102 | return self.get(key) | |
|
103 | ||
|
104 | def refresh(*arg, **kw): | |
|
105 | key = key_generator(*arg, **kw) | |
|
106 | value = fn(*arg, **kw) | |
|
107 | self.set(key, value) | |
|
108 | return value | |
|
109 | ||
|
110 | decorate.set = set_ | |
|
111 | decorate.invalidate = invalidate | |
|
112 | decorate.refresh = refresh | |
|
113 | decorate.get = get | |
|
114 | decorate.original = fn | |
|
115 | decorate.key_generator = key_generator | |
|
116 | decorate.__wrapped__ = fn | |
|
117 | ||
|
118 | return decorate | |
|
119 | return decorator | |
|
120 | ||
|
121 | 66 | def get_or_create_for_user_func(key_generator, user_func, *arg, **kw): |
|
122 | 67 | |
|
123 | 68 | if not condition: |
|
124 |
log.debug('Calling un-cached method:%s', user_func. |
|
|
69 | log.debug('Calling un-cached method:%s', user_func.__name__) | |
|
125 | 70 | start = time.time() |
|
126 | 71 | result = user_func(*arg, **kw) |
|
127 | 72 | total = time.time() - start |
|
128 |
log.debug('un-cached method:%s took %.4fs', user_func. |
|
|
73 | log.debug('un-cached method:%s took %.4fs', user_func.__name__, total) | |
|
129 | 74 | return result |
|
130 | 75 | |
|
131 | 76 | key = key_generator(*arg, **kw) |
@@ -133,11 +78,11 b' class RhodeCodeCacheRegion(CacheRegion):' | |||
|
133 | 78 | timeout = expiration_time() if expiration_time_is_callable \ |
|
134 | 79 | else expiration_time |
|
135 | 80 | |
|
136 |
log.debug('Calling cached method:`%s`', user_func. |
|
|
81 | log.debug('Calling cached method:`%s`', user_func.__name__) | |
|
137 | 82 | return self.get_or_create(key, user_func, timeout, should_cache_fn, (arg, kw)) |
|
138 | 83 | |
|
139 | 84 | def cache_decorator(user_func): |
|
140 |
if to_str is |
|
|
85 | if to_str is str: | |
|
141 | 86 | # backwards compatible |
|
142 | 87 | key_generator = function_key_generator(namespace, user_func) |
|
143 | 88 | else: |
@@ -200,7 +145,7 b' def compute_key_from_params(*args):' | |||
|
200 | 145 | """ |
|
201 | 146 | Helper to compute key from given params to be used in cache manager |
|
202 | 147 | """ |
|
203 |
return sha1("_".join(map( |
|
|
148 | return sha1(safe_bytes("_".join(map(str, args)))) | |
|
204 | 149 | |
|
205 | 150 | |
|
206 | 151 | def backend_key_generator(backend): |
@@ -232,7 +177,7 b' def get_or_create_region(region_name, re' | |||
|
232 | 177 | if not region_obj: |
|
233 | 178 | raise EnvironmentError( |
|
234 | 179 | 'Region `{}` not in configured: {}.'.format( |
|
235 | region_name, region_meta.dogpile_cache_regions.keys())) | |
|
180 | region_name, list(region_meta.dogpile_cache_regions.keys()))) | |
|
236 | 181 | |
|
237 | 182 | region_uid_name = '{}:{}'.format(region_name, region_namespace) |
|
238 | 183 | if isinstance(region_obj.actual_backend, FileNamespaceBackend): |
@@ -24,8 +24,7 b' for Python. Build with server client arc' | |||
|
24 | 24 | """ |
|
25 | 25 | import atexit |
|
26 | 26 | import logging |
|
27 | import urlparse | |
|
28 | from cStringIO import StringIO | |
|
27 | from io import StringIO | |
|
29 | 28 | |
|
30 | 29 | import rhodecode |
|
31 | 30 | from rhodecode.lib.vcs.conf import settings |
@@ -33,10 +32,6 b' from rhodecode.lib.vcs.backends import g' | |||
|
33 | 32 | from rhodecode.lib.vcs.exceptions import ( |
|
34 | 33 | VCSError, RepositoryError, CommitError, VCSCommunicationError) |
|
35 | 34 | |
|
36 | VERSION = (0, 5, 0, 'dev') | |
|
37 | ||
|
38 | __version__ = '.'.join((str(each) for each in VERSION[:4])) | |
|
39 | ||
|
40 | 35 | __all__ = [ |
|
41 | 36 | 'get_version', 'get_vcs_instance', 'get_backend', |
|
42 | 37 | 'VCSError', 'RepositoryError', 'CommitError', 'VCSCommunicationError' |
General Comments 0
You need to be logged in to leave comments.
Login now