/
cache.py
80 lines (65 loc) · 3.17 KB
/
cache.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
from __future__ import (
absolute_import,
unicode_literals,
)
from typing import Any
from django.core.cache.backends.locmem import LocMemCache
from django.core.cache.backends.memcached import (
MemcachedCache,
PyLibMCCache,
)
__all__ = (
'PySOAMemcachedCache',
'PySOAProcessScopedMemoryCache',
'PySOAPyLibMCCache',
'PySOARequestScopedMemoryCache',
)
class PySOARequestScopedMemoryCache(LocMemCache):
"""
If you want a request-scoped, in-memory cache that clears at the end of each job request, we recommend you use this
Django cache engine. You are free to use your own implementation, of course. You can also use this in combination
with other cache engines (from this module or others) in a multi-cache Django configuration.
"""
def close(self, **_kwargs): # type: (**Any) -> None
"""
Clear the cache completely at the end of each request.
"""
self.clear()
class PySOAProcessScopedMemoryCache(LocMemCache):
"""
If you want a server process-scoped, in-memory cache that lasts for the entire server process, we recommend you use
this Django cache engine. You are free to use your own implementation, of course. You can also use this in
combination with other cache engines (from this module or others) in a multi-cache Django configuration.
"""
def close(self, **_kwargs): # type: (**Any) -> None
"""
There is no reason to ever clear the cache.
"""
class PySOAMemcachedCache(MemcachedCache):
"""
If you want to use Memcached with the `python-memcached` library, we recommend you use this Django cache engine,
which does not close the socket connections to Memcached unless the server is shutting down. You are free to use
your own implementation, of course. You can also use this in combination with other cache engines (from this module
or others) in a multi-cache Django configuration.
"""
def close(self, for_shutdown=False, **_kwargs): # type: (bool, **Any) -> None
"""
Only call super().close() if the server is shutting down (not between requests).
:param for_shutdown: If `True`, the socket connections to Memcached are closed (defaults to `False`).
"""
if for_shutdown:
super(PySOAMemcachedCache, self).close()
class PySOAPyLibMCCache(PyLibMCCache):
"""
If you want to use Memcached with the `pylibmc` library, we recommend you use this Django cache engine, which does
not close the socket connections to Memcached unless the server is shutting down and the super class supports
closing the connections. You are free to use your own implementation, of course. You can also use this in
combination with other cache engines (from this module or others) in a multi-cache Django configuration.
"""
def close(self, for_shutdown=False, **_kwargs): # type: (bool, **Any) -> None
"""
Only call super().close() if the server is shutting down (not between requests).
:param for_shutdown: If `True`, the socket connections to Memcached are closed (defaults to `False`).
"""
if for_shutdown:
super(PySOAPyLibMCCache, self).close()