-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpymem_cache.py
More file actions
71 lines (57 loc) · 2.2 KB
/
Copy pathpymem_cache.py
File metadata and controls
71 lines (57 loc) · 2.2 KB
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
"""
PyMemcacheCacheBackend inspired by Django PyMemcacheCache
"""
import typing as t
try:
from pymemcache import HashClient
from pymemcache.serde import pickle_serde
except ImportError as e: # pragma: no cover
raise RuntimeError(
"To use `PyMemcacheCacheBackend`, you have to install 'pymemcache' package e.g. `pip install pymemcache`"
) from e
from .base import BasePylibMemcachedCache
class Client(HashClient):
"""
Pymemcache client.
Customize pymemcache behavior as python-memcached (default backend)'s one.
"""
def __init__(self, servers: t.List[str], *args: t.Any, **kwargs: t.Any) -> None:
super(Client, self).__init__(
self._split_host_and_port(servers), *args, **kwargs
)
def _split_host_and_port(self, servers: t.List[t.Any]) -> t.List[t.Tuple[str, int]]:
"""Convert python-memcached based server strings to pymemcache's one.
- python-memcached: ['127.0.0.1:11211', ...] or ['127.0.0.1', ...]
- pymemcache: [('127.0.0.1', 11211), ...]
"""
_host_and_port_list = []
for server in servers:
connection_info = server.split(":")
if len(connection_info) == 1:
_host_and_port_list.append((connection_info[0], 11211))
elif len(connection_info) == 2:
_host_and_port_list.append(
(connection_info[0], int(connection_info[1]))
)
return _host_and_port_list
def disconnect_all(self) -> None:
for client in self.clients.values():
client.close()
class PyMemcacheCacheBackend(BasePylibMemcachedCache):
"""
An implementation of a cache binding using pymemcache.
"""
MEMCACHE_CLIENT = Client
def __init__(
self, servers: t.List[str], options: t.Optional[t.Dict] = None, **kwargs: t.Any
):
_default_options = options or {}
_options = {
"allow_unicode_keys": True,
"default_noreply": False,
"serde": pickle_serde,
**_default_options,
}
super().__init__(servers, options=_options, **kwargs)
def close(self, **kwargs: t.Any) -> None:
self._cache_client.disconnect_all()