|
13 | 13 | from .base import BasePylibMemcachedCache
|
14 | 14 |
|
15 | 15 |
|
| 16 | +class Client(HashClient): |
| 17 | + """pymemcache client. |
| 18 | + Customize pymemcache behavior as python-memcached (default backend)'s one. |
| 19 | + """ |
| 20 | + |
| 21 | + def __init__(self, servers: t.List[str], *args: t.Any, **kwargs: t.Any) -> None: |
| 22 | + |
| 23 | + super(Client, self).__init__( |
| 24 | + self._split_host_and_port(servers), *args, **kwargs |
| 25 | + ) |
| 26 | + |
| 27 | + def _split_host_and_port(self, servers: t.List[t.Any]) -> t.List[t.Tuple[str, int]]: |
| 28 | + """Convert python-memcached based server strings to pymemcache's one. |
| 29 | + - python-memcached: ['127.0.0.1:11211', ...] or ['127.0.0.1', ...] |
| 30 | + - pymemcache: [('127.0.0.1', 11211), ...] |
| 31 | + """ |
| 32 | + _host_and_port_list = [] |
| 33 | + for server in servers: |
| 34 | + connection_info = server.split(":") |
| 35 | + if len(connection_info) == 1: |
| 36 | + _host_and_port_list.append((connection_info[0], 11211)) |
| 37 | + elif len(connection_info) == 2: |
| 38 | + _host_and_port_list.append( |
| 39 | + (connection_info[0], int(connection_info[1])) |
| 40 | + ) |
| 41 | + return _host_and_port_list |
| 42 | + |
| 43 | + def disconnect_all(self) -> None: |
| 44 | + for client in self.clients.values(): |
| 45 | + client.close() |
| 46 | + |
| 47 | + |
16 | 48 | class PyMemcacheCacheBackend(BasePylibMemcachedCache):
|
17 | 49 | """An implementation of a cache binding using pymemcache."""
|
18 | 50 |
|
19 |
| - MEMCACHE_CLIENT = HashClient |
| 51 | + MEMCACHE_CLIENT = Client |
20 | 52 |
|
21 | 53 | def __init__(self, servers: t.List[str], options: t.Dict = None, **kwargs: t.Any):
|
22 | 54 |
|
|
0 commit comments