Commit 7bf89f3f authored by Sean Bleier's avatar Sean Bleier

Merge pull request #103 from paksu/fix_get_many_with_empty_list

get_many and set_many should work the same way as in other cache backends
parents 7b837f80 a9cf398b
...@@ -19,3 +19,4 @@ ...@@ -19,3 +19,4 @@
| Michael Lindemuth / mlindemu <https://github.com/mlindemu> | Michael Lindemuth / mlindemu <https://github.com/mlindemu>
| John Furr / gnulnx <https://github.com/gnulnx> | John Furr / gnulnx <https://github.com/gnulnx>
| Christian Pedersen / chripede <https://github.com/chripede> | Christian Pedersen / chripede <https://github.com/chripede>
| Joona Pääkkönen / paksu <https://github.com/paksu>
...@@ -302,12 +302,14 @@ class BaseRedisCache(BaseCache): ...@@ -302,12 +302,14 @@ class BaseRedisCache(BaseCache):
recovered_data = {} recovered_data = {}
map_keys = dict(zip(versioned_keys, original_keys)) map_keys = dict(zip(versioned_keys, original_keys))
results = client.mget(versioned_keys) # Only try to mget if we actually received any keys to get
if map_keys:
results = client.mget(versioned_keys)
for key, value in zip(versioned_keys, results): for key, value in zip(versioned_keys, results):
if value is None: if value is None:
continue continue
recovered_data[map_keys[key]] = self.get_value(value) recovered_data[map_keys[key]] = self.get_value(value)
return recovered_data return recovered_data
...@@ -316,6 +318,9 @@ class BaseRedisCache(BaseCache): ...@@ -316,6 +318,9 @@ class BaseRedisCache(BaseCache):
raise NotImplementedError raise NotImplementedError
def _set_many(self, client, data): def _set_many(self, client, data):
# Only call mset if there actually is some data to save
if not data:
return True
return client.mset(data) return client.mset(data)
def set_many(self, data, timeout=DEFAULT_TIMEOUT, version=None): def set_many(self, data, timeout=DEFAULT_TIMEOUT, version=None):
......
...@@ -176,6 +176,9 @@ class BaseRedisTestCase(SetupMixin): ...@@ -176,6 +176,9 @@ class BaseRedisTestCase(SetupMixin):
self.assertEqual(self.cache.get_many(['a', 'c', 'd']), {'a': 'a', 'c': 'c', 'd': 'd'}) self.assertEqual(self.cache.get_many(['a', 'c', 'd']), {'a': 'a', 'c': 'c', 'd': 'd'})
self.assertEqual(self.cache.get_many(['a', 'b', 'e']), {'a': 'a', 'b': 'b'}) self.assertEqual(self.cache.get_many(['a', 'b', 'e']), {'a': 'a', 'b': 'b'})
def test_get_many_works_with_empty_keys_array(self):
self.assertEqual(self.cache.get_many([]), {})
def test_get_many_with_manual_integer_insertion(self): def test_get_many_with_manual_integer_insertion(self):
keys = ['a', 'b', 'c', 'd'] keys = ['a', 'b', 'c', 'd']
for i, key in enumerate(keys): for i, key in enumerate(keys):
...@@ -342,6 +345,11 @@ class BaseRedisTestCase(SetupMixin): ...@@ -342,6 +345,11 @@ class BaseRedisTestCase(SetupMixin):
self.assertEqual(self.cache.get("key1"), "spam") self.assertEqual(self.cache.get("key1"), "spam")
self.assertEqual(self.cache.get("key2"), "eggs") self.assertEqual(self.cache.get("key2"), "eggs")
def test_set_many_works_with_empty_dict(self):
# This test passes if no exception is raised
self.cache.set_many({})
self.cache.set_many({}, version=2)
def test_set_many_expiration(self): def test_set_many_expiration(self):
# set_many takes a second ``timeout`` parameter # set_many takes a second ``timeout`` parameter
self.cache.set_many({"key1": "spam", "key2": "eggs"}, 1) self.cache.set_many({"key1": "spam", "key2": "eggs"}, 1)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment