Skip to content

Commit 439b467

Browse files
committed
Fix ignore pymemcache's get_many returns False value
In some case, pymemcache returns {'key': False} if client does not found. It raises exception at Django-Cachalot. Django-Cachalot's author said get_many should not contain a key that's not in the cache, as stated in Django. So we try to imprements get_many method and ignore if False were included.
1 parent 39f09c4 commit 439b467

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

django_elastipymemcache/client.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@
22

33

44
class Client(HashClient):
5-
pass
5+
def get_many(self, keys, gets=False, *args, **kwargs):
6+
# pymemcache's HashClient may returns {'key': False}
7+
end = super(Client, self).get_many(keys, gets, args, kwargs)
8+
9+
return {key: end[key] for key in end if end[key]}
10+
11+
get_multi = get_many

tests/test_backend.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,49 @@ def test_invalidate_cache(get_cluster_info):
9999
pass
100100
eq_(backend._cache.get.call_count, 2)
101101
eq_(get_cluster_info.call_count, 3)
102+
103+
104+
@patch('django.conf.settings', global_settings)
105+
@patch('django_elastipymemcache.memcached.get_cluster_info')
106+
def test_client_get_many(get_cluster_info):
107+
from django_elastipymemcache.memcached import ElastiPyMemCache
108+
109+
servers = [('h1', 0), ('h2', 0)]
110+
get_cluster_info.return_value = {
111+
'nodes': servers
112+
}
113+
114+
backend = ElastiPyMemCache('h:0', {})
115+
ret = backend.get_many(['key1'])
116+
eq_(ret, {})
117+
118+
# When server does not found...
119+
with patch('pymemcache.client.hash.HashClient._get_client') as p:
120+
p.return_value = None
121+
ret = backend.get_many(['key2'])
122+
eq_(ret, {})
123+
124+
with patch('django_elastipymemcache.client.Client.get_many'):
125+
with patch('pymemcache.client.hash.HashClient._safely_run_func') as p2:
126+
p2.return_value = {
127+
':1:key3': 1509111630.048594
128+
}
129+
130+
ret = backend.get_many(['key3'])
131+
eq_(ret, {'key3': 1509111630.048594})
132+
133+
# If False value is included, ignore it.
134+
with patch('pymemcache.client.hash.HashClient.get_many') as p:
135+
p.return_value = {
136+
':1:key1': 1509111630.048594,
137+
':1:key2': False,
138+
':1:key3': 1509111630.058594,
139+
}
140+
ret = backend.get_many(['key1', 'key2', 'key3'])
141+
eq_(
142+
ret,
143+
{
144+
'key1': 1509111630.048594,
145+
'key3': 1509111630.058594
146+
},
147+
)

0 commit comments

Comments
 (0)