Skip to content

Commit fc7df7a

Browse files
committed
added a custom client for PyMemcache library
1 parent f597099 commit fc7df7a

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

ellar/cache/backends/pymem_cache.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,42 @@
1313
from .base import BasePylibMemcachedCache
1414

1515

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+
1648
class PyMemcacheCacheBackend(BasePylibMemcachedCache):
1749
"""An implementation of a cache binding using pymemcache."""
1850

19-
MEMCACHE_CLIENT = HashClient
51+
MEMCACHE_CLIENT = Client
2052

2153
def __init__(self, servers: t.List[str], options: t.Dict = None, **kwargs: t.Any):
2254

0 commit comments

Comments
 (0)