|
1
|
|
|
"""uWSGI cache backend""" |
|
2
|
|
|
__version__ = "1.0.1" |
|
3
|
|
|
|
|
4
|
|
|
try: |
|
5
|
|
|
from django.utils.encoding import force_bytes as stringify |
|
6
|
|
|
except ImportError: |
|
7
|
|
|
from django.utils.encoding import smart_str as stringify |
|
8
|
|
|
from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError, DEFAULT_TIMEOUT |
|
9
|
|
|
from django.conf import settings |
|
10
|
|
|
|
|
11
|
|
|
try: |
|
12
|
|
|
import cPickle as pickle |
|
13
|
|
|
except ImportError: |
|
14
|
|
|
import pickle |
|
15
|
|
|
|
|
16
|
|
|
try: |
|
17
|
|
|
import uwsgi |
|
18
|
|
|
except ImportError: |
|
19
|
|
|
if getattr(settings, "UWSGI_CACHE_FALLBACK", True): |
|
20
|
|
|
uwsgi = None |
|
21
|
|
|
else: |
|
22
|
|
|
raise InvalidCacheBackendError( |
|
23
|
|
|
"You're not running under uWSGI ! " |
|
24
|
|
|
"Set UWSGI_CACHE_FALLBACK=True in settings if you want to fallback " |
|
25
|
|
|
"to LocMemCache." |
|
26
|
|
|
) |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
if uwsgi: |
|
30
|
|
|
class UWSGICache(BaseCache): |
|
31
|
|
|
def __init__(self, server, params): |
|
32
|
|
|
BaseCache.__init__(self, params) |
|
33
|
|
|
self._cache = uwsgi |
|
34
|
|
|
self._server = server |
|
35
|
|
|
|
|
36
|
|
|
def exists(self, key): |
|
37
|
|
|
return self._cache.cache_exists(stringify(key), self._server) |
|
38
|
|
|
|
|
39
|
|
|
def add(self, key, value, timeout=True, version=None): |
|
40
|
|
|
full_key = self.make_key(key, version=version) |
|
41
|
|
|
if self.exists(full_key): |
|
42
|
|
|
return False |
|
43
|
|
|
self._set(full_key, value, timeout) |
|
44
|
|
|
return True |
|
45
|
|
|
|
|
46
|
|
|
def get(self, key, default=None, version=None): |
|
47
|
|
|
full_key = self.make_key(key, version=version) |
|
48
|
|
|
val = self._cache.cache_get(stringify(full_key), self._server) |
|
49
|
|
|
if val is None: |
|
50
|
|
|
return default |
|
51
|
|
|
val = stringify(val) |
|
52
|
|
|
return pickle.loads(val) |
|
53
|
|
|
|
|
54
|
|
|
def _set(self, full_key, value, timeout): |
|
55
|
|
|
if timeout is True or timeout == DEFAULT_TIMEOUT: |
|
56
|
|
|
timeout = self.default_timeout |
|
57
|
|
|
|
|
58
|
|
|
if timeout is None or timeout is False: |
|
59
|
|
|
# Django 1.6+: Explicitly passing in timeout=None will set a non-expiring timeout. |
|
60
|
|
|
uwsgi_timeout = 0 |
|
61
|
|
|
elif timeout == 0: |
|
62
|
|
|
# Django 1.6+: Passing in timeout=0 will set-and-expire-immediately the value. |
|
63
|
|
|
uwsgi_timeout = -1 |
|
64
|
|
|
else: |
|
65
|
|
|
uwsgi_timeout = timeout |
|
66
|
|
|
self._cache.cache_update(stringify(full_key), pickle.dumps(value), uwsgi_timeout, self._server) |
|
67
|
|
|
|
|
68
|
|
|
def set(self, key, value, timeout=True, version=None): |
|
69
|
|
|
full_key = self.make_key(key, version=version) |
|
70
|
|
|
self._set(full_key, value, timeout) |
|
71
|
|
|
|
|
72
|
|
|
def delete(self, key, version=None): |
|
73
|
|
|
full_key = self.make_key(key, version=version) |
|
74
|
|
|
self._cache.cache_del(stringify(full_key), self._server) |
|
75
|
|
|
|
|
76
|
|
|
def close(self, **kwargs): |
|
77
|
|
|
pass |
|
78
|
|
|
|
|
79
|
|
|
def clear(self): |
|
80
|
|
|
self._cache.cache_clear(self._server) |
|
81
|
|
|
else: |
|
82
|
|
|
from django.core.cache.backends.locmem import LocMemCache as UWSGICache # flake8: noqa |
|
83
|
|
|
|