-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathbase.py
495 lines (400 loc) · 15.8 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
from functools import wraps
from django.core.cache.backends.base import (
BaseCache, DEFAULT_TIMEOUT, InvalidCacheBackendError,
)
from django.core.exceptions import ImproperlyConfigured
try:
import redis
except ImportError:
raise InvalidCacheBackendError(
"Redis cache backend requires the 'redis-py' library"
)
from redis.connection import DefaultParser
from redis_cache.constants import KEY_EXPIRED, KEY_NON_VOLATILE
from redis_cache.connection import pool
from redis_cache.utils import get_servers, parse_connection_kwargs, import_class
def get_client(write=False):
def wrapper(method):
@wraps(method)
def wrapped(self, key, *args, **kwargs):
version = kwargs.pop('version', None)
key = self.make_key(key, version=version)
client = self.get_client(key, write=write)
return method(self, client, key, *args, **kwargs)
return wrapped
return wrapper
class BaseRedisCache(BaseCache):
def __init__(self, server, params):
"""
Connect to Redis, and set up cache backend.
"""
super(BaseRedisCache, self).__init__(params)
self.server = server
self.servers = get_servers(server)
self.params = params or {}
self.options = params.get('OPTIONS', {})
self.clients = {}
self.client_list = []
self.db = self.get_db()
self.password = self.get_password()
self.parser_class = self.get_parser_class()
self.pickle_version = self.get_pickle_version()
self.socket_timeout = self.get_socket_timeout()
self.socket_connect_timeout = self.get_socket_connect_timeout()
self.connection_pool_class = self.get_connection_pool_class()
self.connection_pool_class_kwargs = (
self.get_connection_pool_class_kwargs()
)
# Serializer
self.serializer_class = self.get_serializer_class()
self.serializer_class_kwargs = self.get_serializer_class_kwargs()
self.serializer = self.serializer_class(
**self.serializer_class_kwargs
)
# Compressor
self.compressor_class = self.get_compressor_class()
self.compressor_class_kwargs = self.get_compressor_class_kwargs()
self.compressor = self.compressor_class(
**self.compressor_class_kwargs
)
redis_py_version = tuple(int(part) for part in redis.__version__.split('.'))
if redis_py_version < (3, 0, 0):
self.Redis = redis.StrictRedis
else:
self.Redis = redis.Redis
def __getstate__(self):
return {'params': self.params, 'server': self.server}
def __setstate__(self, state):
self.__init__(**state)
def get_db(self):
_db = self.params.get('db', self.options.get('DB', 1))
try:
return int(_db)
except (ValueError, TypeError):
raise ImproperlyConfigured("db value must be an integer")
def get_password(self):
return self.params.get('password', self.options.get('PASSWORD', None))
def get_parser_class(self):
parser_class = self.options.get('PARSER_CLASS', None)
if parser_class is None:
return DefaultParser
return import_class(parser_class)
def get_pickle_version(self):
"""
Get the pickle version from the settings and save it for future use
"""
_pickle_version = self.options.get('PICKLE_VERSION', -1)
try:
return int(_pickle_version)
except (ValueError, TypeError):
raise ImproperlyConfigured(
"pickle version value must be an integer"
)
def get_socket_timeout(self):
return self.options.get('SOCKET_TIMEOUT', None)
def get_socket_connect_timeout(self):
return self.options.get('SOCKET_CONNECT_TIMEOUT', None)
def get_connection_pool_class(self):
pool_class = self.options.get(
'CONNECTION_POOL_CLASS',
'redis.ConnectionPool'
)
return import_class(pool_class)
def get_connection_pool_class_kwargs(self):
return self.options.get('CONNECTION_POOL_CLASS_KWARGS', {})
def get_serializer_class(self):
serializer_class = self.options.get(
'SERIALIZER_CLASS',
'redis_cache.serializers.PickleSerializer'
)
return import_class(serializer_class)
def get_serializer_class_kwargs(self):
kwargs = self.options.get('SERIALIZER_CLASS_KWARGS', {})
serializer_class = self.options.get(
'SERIALIZER_CLASS',
'redis_cache.serializers.PickleSerializer'
)
if serializer_class == 'redis_cache.serializers.PickleSerializer':
kwargs['pickle_version'] = kwargs.get(
'pickle_version',
self.pickle_version
)
return kwargs
def get_compressor_class(self):
compressor_class = self.options.get(
'COMPRESSOR_CLASS',
'redis_cache.compressors.NoopCompressor'
)
return import_class(compressor_class)
def get_compressor_class_kwargs(self):
return self.options.get('COMPRESSOR_CLASS_KWARGS', {})
def get_master_client(self):
"""
Get the write server:port of the master cache
"""
cache = self.options.get('MASTER_CACHE', None)
if cache is None:
return next(iter(self.client_list))
kwargs = parse_connection_kwargs(cache, db=self.db)
return self.clients[(
kwargs['host'],
kwargs['port'],
kwargs['db'],
kwargs['unix_socket_path'],
)]
def create_client(self, server):
kwargs = parse_connection_kwargs(
server,
db=self.db,
password=self.password,
socket_timeout=self.socket_timeout,
socket_connect_timeout=self.socket_connect_timeout,
)
# remove socket-related connection arguments
if kwargs.get('ssl', False):
del kwargs['socket_timeout']
del kwargs['socket_connect_timeout']
del kwargs['unix_socket_path']
client = redis.Redis(**kwargs)
kwargs.update(
parser_class=self.parser_class,
connection_pool_class=self.connection_pool_class,
connection_pool_class_kwargs=self.connection_pool_class_kwargs,
)
connection_pool = pool.get_connection_pool(client, **kwargs)
client.connection_pool = connection_pool
return client
def serialize(self, value):
return self.serializer.serialize(value)
def deserialize(self, value):
return self.serializer.deserialize(value)
def compress(self, value):
return self.compressor.compress(value)
def decompress(self, value):
return self.compressor.decompress(value)
def get_value(self, original):
try:
value = int(original)
except (ValueError, TypeError):
value = self.decompress(original)
value = self.deserialize(value)
return value
def prep_value(self, value):
if isinstance(value, int) and not isinstance(value, bool):
return value
value = self.serialize(value)
return self.compress(value)
def make_keys(self, keys, version=None):
return [self.make_key(key, version=version) for key in keys]
def get_timeout(self, timeout):
if timeout is DEFAULT_TIMEOUT:
timeout = self.default_timeout
if timeout is not None:
timeout = int(timeout)
return timeout
####################
# Django cache api #
####################
@get_client(write=True)
def add(self, client, key, value, timeout=DEFAULT_TIMEOUT):
"""Add a value to the cache, failing if the key already exists.
Returns ``True`` if the object was added, ``False`` if not.
"""
timeout = self.get_timeout(timeout)
return self._set(client, key, self.prep_value(value), timeout, _add_only=True)
def _get(self, client, key, default=None):
value = client.get(key)
if value is None:
return default
value = self.get_value(value)
return value
@get_client()
def get(self, client, key, default=None):
"""Retrieve a value from the cache.
Returns deserialized value if key is found, the default if not.
"""
return self._get(client, key, default)
def _set(self, client, key, value, timeout, _add_only=False):
if timeout is not None and timeout < 0:
return False
elif timeout == 0:
return client.expire(key, 0)
return client.set(key, value, nx=_add_only, ex=timeout)
@get_client(write=True)
def set(self, client, key, value, timeout=DEFAULT_TIMEOUT):
"""Persist a value to the cache, and set an optional expiration time.
"""
timeout = self.get_timeout(timeout)
result = self._set(client, key, self.prep_value(value), timeout, _add_only=False)
return result
@get_client(write=True)
def delete(self, client, key):
"""Remove a key from the cache."""
return client.delete(key)
def _delete_many(self, client, keys):
return client.delete(*keys)
def delete_many(self, keys, version=None):
"""
Remove multiple keys at once.
"""
raise NotImplementedError
def _clear(self, client):
return client.flushdb()
def clear(self, version=None):
"""Flush cache keys.
If version is specified, all keys belonging the version's key
namespace will be deleted. Otherwise, all keys will be deleted.
"""
raise NotImplementedError
def _get_many(self, client, original_keys, versioned_keys):
recovered_data = {}
map_keys = dict(zip(versioned_keys, original_keys))
# Only try to mget if we actually received any keys to get
if map_keys:
results = client.mget(versioned_keys)
for key, value in zip(versioned_keys, results):
if value is None:
continue
recovered_data[map_keys[key]] = self.get_value(value)
return recovered_data
def get_many(self, keys, version=None):
"""Retrieve many keys."""
raise NotImplementedError
def set_many(self, data, timeout=DEFAULT_TIMEOUT, version=None):
"""Set a bunch of values in the cache at once from a dict of key/value
pairs. This is much more efficient than calling set() multiple times.
If timeout is given, that timeout will be used for the key; otherwise
the default cache timeout will be used.
"""
raise NotImplementedError
@get_client(write=True)
def incr(self, client, key, delta=1):
"""Add delta to value in the cache. If the key does not exist, raise a
`ValueError` exception.
"""
exists = client.exists(key)
if not exists:
raise ValueError("Key '%s' not found" % key)
value = client.incr(key, delta)
return value
def _incr_version(self, client, old, new, original, delta, version):
try:
client.rename(old, new)
except redis.ResponseError:
raise ValueError("Key '%s' not found" % original)
return version + delta
def incr_version(self, key, delta=1, version=None):
"""Adds delta to the cache version for the supplied key. Returns the
new version.
"""
@get_client(write=True)
def touch(self, client, key, timeout=DEFAULT_TIMEOUT):
"""Reset the timeout of a key to `timeout` seconds."""
return client.expire(key, timeout)
#####################
# Extra api methods #
#####################
@get_client()
def has_key(self, client, key):
"""Returns True if the key is in the cache and has not expired."""
return client.exists(key)
@get_client()
def ttl(self, client, key):
"""Returns the 'time-to-live' of a key. If the key is not volatile,
i.e. it has not set expiration, then the value returned is None.
Otherwise, the value is the number of seconds remaining. If the key
does not exist, 0 is returned.
"""
ttl = client.ttl(key)
if ttl == KEY_NON_VOLATILE:
return None
elif ttl == KEY_EXPIRED:
return 0
else:
return ttl
def _delete_pattern(self, client, pattern):
keys = list(client.scan_iter(match=pattern))
if keys:
client.delete(*keys)
def delete_pattern(self, pattern, version=None):
raise NotImplementedError
def lock(
self,
key,
timeout=None,
sleep=0.1,
blocking_timeout=None,
thread_local=True):
client = self.get_client(key, write=True)
return client.lock(
key,
timeout=timeout,
sleep=sleep,
blocking_timeout=blocking_timeout,
thread_local=thread_local
)
@get_client(write=True)
def get_or_set(
self,
client,
key,
default,
timeout=DEFAULT_TIMEOUT,
lock_timeout=None,
stale_cache_timeout=None):
"""Get a value from the cache or use ``default`` to set it and return it.
If ``default`` is a callable, call it without arguments and store its return value in the cache instead.
This implementation is slightly more advanced that Django's. It provides thundering herd
protection, which prevents multiple threads/processes from calling the value-generating
function too much.
There are three timeouts you can specify:
``timeout``: Time in seconds that value at ``key`` is considered fresh.
``lock_timeout``: Time in seconds that the lock will stay active and prevent other threads
or processes from acquiring the lock.
``stale_cache_timeout``: Time in seconds that the stale cache will remain after the key has
expired. If ``None`` is specified, the stale value will remain indefinitely.
"""
lock_key = "__lock__" + key
fresh_key = "__fresh__" + key
is_fresh = self._get(client, fresh_key)
value = self._get(client, key)
if is_fresh:
return value
timeout = self.get_timeout(timeout)
lock = self.lock(lock_key, timeout=lock_timeout)
acquired = lock.acquire(blocking=False)
if acquired:
try:
value = default() if callable(default) else default
except Exception:
raise
else:
key_timeout = (
None if stale_cache_timeout is None else timeout + stale_cache_timeout
)
pipeline = client.pipeline()
pipeline.set(key, self.prep_value(value), key_timeout)
pipeline.set(fresh_key, 1, timeout)
pipeline.execute()
finally:
lock.release()
return value
def _reinsert_keys(self, client):
keys = list(client.scan_iter(match='*'))
for key in keys:
timeout = client.ttl(key)
value = self.get_value(client.get(key))
if timeout is None:
client.set(key, self.prep_value(value))
def reinsert_keys(self):
"""
Reinsert cache entries using the current pickle protocol version.
"""
raise NotImplementedError
@get_client(write=True)
def persist(self, client, key):
"""Remove the timeout on a key.
Equivalent to setting a timeout of None in a set command.
Returns True if successful and False if not.
"""
return client.persist(key)