Andriy Kornatskyy avatar Andriy Kornatskyy committed c3f37d7

Added get_keys and get_multi_keys to cache dependency.

Comments (0)

Files changed (2)

src/wheezy/caching/dependency.py

             key_prefix and map(lambda k: key_prefix + k, keys) or keys))
         return self.cache.add_multi(mapping, self.time, '', self.namespace)
 
+    def get_keys(self, master_key):
+        """ Returns all keys wired by *master_key* cache dependency.
+        """
+        n = self.cache.get(master_key, self.namespace)
+        if n is None:
+            return []
+        keys = [master_key + str(i) for i in xrange(1, n + 1)]
+        keys.extend(itervalues(self.cache.get_multi(
+            keys, '', self.namespace)))
+        keys.append(master_key)
+        return keys
+
+    def get_multi_keys(self, master_keys):
+        """ Returns all keys wired by *master_keys* cache dependencies.
+        """
+        numbers = self.cache.get_multi(master_keys, '', self.namespace)
+        if not numbers:
+            return []
+        keys = [master_key + str(i) for master_key, n in numbers.items()
+                for i in xrange(1, n + 1)]
+        keys.extend(itervalues(self.cache.get_multi(
+            keys, '', self.namespace)))
+        keys.extend(master_keys)
+        return keys
+
     def delete(self, master_key):
         """ Delete all items wired by *master_key* cache dependency.
         """
-        cache = self.cache
-        n = cache.get(master_key, self.namespace)
-        if n is None:
+        keys = self.get_keys(master_key)
+        if not keys:
             return True
-        keys = [master_key + str(i) for i in xrange(1, n + 1)]
-        keys.extend(itervalues(cache.get_multi(keys, '', self.namespace)))
-        keys.append(master_key)
-        return cache.delete_multi(keys, 0, '', self.namespace)
+        return self.cache.delete_multi(keys, 0, '', self.namespace)
 
     def delete_multi(self, master_keys):
         """ Delete all items wired by *master_keys* cache dependencies.
         """
-        cache = self.cache
-        numbers = cache.get_multi(master_keys, '', self.namespace)
-        if not numbers:
+        keys = self.get_multi_keys(master_keys)
+        if not keys:
             return True
-        keys = [master_key + str(i) for master_key, n in numbers.items()
-                for i in xrange(1, n + 1)]
-        keys.extend(itervalues(cache.get_multi(keys, '', self.namespace)))
-        keys.extend(master_keys)
-        return cache.delete_multi(keys, 0, '', self.namespace)
+        return self.cache.delete_multi(keys, 0, '', self.namespace)

src/wheezy/caching/tests/test_dependency.py

         self.mock_cache.add_multi.assert_called_once_with(
             {'key1': 'prefix-k'}, 10, '', 'ns')
 
+    def test_get_keys(self):
+        """ Ensure related keys are returned.
+        """
+        self.mock_cache.get.return_value = None
+        assert [] == self.d.get_keys('key')
+        self.mock_cache.get.assert_called_once_with(
+            'key', 'ns')
+        assert not self.mock_cache.get_multi.called
+
+        def side_effect(*args):
+            assert ['key1', 'key2'] == args[0]
+            return {'key1': 'k1', 'key2': 'k2'}
+        self.mock_cache.reset_mock()
+        self.mock_cache.get.return_value = 2
+        self.mock_cache.get_multi.side_effect = side_effect
+        keys = self.d.get_keys('key')
+        assert ['k1', 'k2', 'key', 'key1', 'key2'] == sorted(keys)
+        self.mock_cache.get.assert_called_once_with(
+            'key', 'ns')
+        self.mock_cache.get_multi.assert_called_once_with(
+            ANY, '', 'ns')
+
+    def test_get_multi(self):
+        """ Ensure related keys are returned for multi
+            dependencies.
+        """
+        self.mock_cache.get_multi.return_value = None
+        assert [] == self.d.get_multi_keys(['ka', 'kb'])
+        self.mock_cache.get_multi.assert_called_once_with(
+            ['ka', 'kb'], '', 'ns')
+
+        calls = [
+            {'ka': 2, 'kb': 1},
+            {'ka1': 'k1', 'ka2': 'k2', 'kb1': 'k3'}
+        ]
+
+        def side_effect(*args):
+            result = calls[0]
+            del calls[0]
+            return result
+        self.mock_cache.reset_mock()
+        self.mock_cache.get_multi.side_effect = side_effect
+
+        keys = self.d.get_multi_keys(['ka', 'kb', 'kc'])
+        assert ['k1', 'k2', 'k3', 'ka', 'ka1', 'ka2', 'kb', 'kb1', 'kc'
+                ] == sorted(keys)
+        assert 2 == self.mock_cache.get_multi.call_count
+
     def test_delete(self):
         """ Ensure related keys are invalidated.
         """
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.