Commits

Andriy Kornatskyy committed cdec846

Refactored CacheDependency to be specialization use case.

Comments (0)

Files changed (4)

src/wheezy/caching/dependency.py

         simplifing code necessary to manage dependencies in cache.
     """
 
-    __slots__ = ('cache', 'master_key', 'time', 'namespace')
-
-    def __init__(self, cache, master_key, time=0, namespace=None):
+    def __init__(self, cache, time=0, namespace=None):
         """
            *cache* - a cache instance to be used to track dependencies.
-           *master_key* - a key used to track a number of issued dependencies.
            *time* - a time in seconds to keep dependent keys.
            *namespace* - a default namespace.
         """
         self.cache = cache
-        self.master_key = master_key
         self.time = time
         self.namespace = namespace
 
-    def next_key(self, namespace=None):
+    def next_key(self, master_key):
         """ Returns the next unique key for dependency.
+
+           *master_key* - a key used to track a number of issued dependencies.
         """
-        return self.master_key + str(self.cache.incr(
-            self.master_key, 1, namespace or self.namespace, 0))
+        return master_key + str(self.cache.incr(
+            master_key, 1, self.namespace, 0))
 
-    def next_keys(self, n, namespace=None):
-        """ Returns ``n`` number of dependency keys.
+    def next_keys(self, master_key, n):
+        """ Returns *n* number of dependency keys.
+
+           *master_key* - a key used to track a number of issued dependencies.
         """
-        last_id = self.cache.incr(self.master_key, n,
-                                  namespace or self.namespace, 0)
-        return [self.master_key + str(i)
+        last_id = self.cache.incr(master_key, n, self.namespace, 0)
+        return [master_key + str(i)
                 for i in xrange(last_id - n + 1, last_id + 1)]
 
-    def add(self, key, namespace=None):
-        """ Adds a given key to dependency.
+    def add(self, master_key, key):
+        """ Adds a given *key* to dependency.
         """
-        namespace = namespace or self.namespace
-        return self.cache.add(self.next_key(namespace),
-                              key, self.time, namespace)
+        return self.cache.add(self.next_key(master_key),
+                              key, self.time, self.namespace)
 
-    def add_multi(self, keys, key_prefix='', namespace=None):
-        """ Adds several keys to dependency.
+    def add_multi(self, master_key, keys, key_prefix=''):
+        """ Adds several *keys* to dependency.
         """
-        namespace = namespace or self.namespace
-        mapping = dict(zip(self.next_keys(
-            len(keys), namespace),
+        mapping = dict(zip(
+            self.next_keys(master_key, len(keys)),
             key_prefix and map(lambda k: key_prefix + k, keys) or keys))
-        return self.cache.add_multi(mapping, self.time, '', namespace)
+        return self.cache.add_multi(mapping, self.time, '', self.namespace)
 
-    def delete(self, namespace=None):
+    def delete(self, master_key):
         """ Delete all items wired by this cache dependency.
         """
-        namespace = namespace or self.namespace
         cache = self.cache
-        n = cache.get(self.master_key, namespace)
+        n = cache.get(master_key, self.namespace)
         if n is None:
             return True
-        keys = [self.master_key + str(i) for i in xrange(1, n + 1)]
-        keys.extend(itervalues(cache.get_multi(keys, '', namespace)))
-        keys.append(self.master_key)
-        return cache.delete_multi(keys, 0, '', namespace)
+        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)

src/wheezy/caching/patterns.py

         self.namespace = namespace
         self.timeout = timeout
         self.key_prefix = key_prefix
+        self.dependency = CacheDependency(cache, time, namespace)
 
     def set(self, key, value, dependency=None):
         """ Sets a key's value, regardless of previous contents
         """
         return self.cache.decr(key, delta, self.namespace, initial_value)
 
-    def dependency(self, master_key):
-        """ Returns an instance of `CacheDependency` initialized by
-            given *master_key*.
-        """
-        return CacheDependency(self.cache, master_key, self.time,
-                               self.namespace)
-
     def get_or_add(self, key, create_factory, dependency_factory):
         """ Cache Pattern: get an item by *key* from *cache* and
             if it is not available use *create_factory* to aquire one.

src/wheezy/caching/tests/test_dependency.py

     def setUp(self):
         from wheezy.caching.dependency import CacheDependency
         self.mock_cache = Mock()
-        self.d = CacheDependency(self.mock_cache, 'key',
-                                 time=10, namespace='default')
+        self.d = CacheDependency(self.mock_cache, time=10, namespace='ns')
 
     def test_next_key(self):
         """ Ensures consistency of key issued.
         """
         self.mock_cache.incr.return_value = 1
-        assert 'key1' == self.d.next_key()
-        self.mock_cache.incr.assert_called_once_with('key', 1, 'default', 0)
+        assert 'key1' == self.d.next_key('key')
+        self.mock_cache.incr.assert_called_once_with('key', 1, 'ns', 0)
 
         self.mock_cache.reset_mock()
         self.mock_cache.incr.return_value = 2
-        assert 'key2' == self.d.next_key('ns')
+        assert 'key2' == self.d.next_key('key')
         self.mock_cache.incr.assert_called_once_with('key', 1, 'ns', 0)
 
     def test_next_keys(self):
         """ Ensures consistency of keys issued.
         """
         self.mock_cache.incr.return_value = 2
-        assert ['key1', 'key2'] == self.d.next_keys(2)
-        self.mock_cache.incr.assert_called_once_with('key', 2, 'default', 0)
+        assert ['key1', 'key2'] == self.d.next_keys('key', 2)
+        self.mock_cache.incr.assert_called_once_with('key', 2, 'ns', 0)
 
         self.mock_cache.reset_mock()
         self.mock_cache.incr.return_value = 4
-        assert ['key3', 'key4'] == self.d.next_keys(2, 'ns')
+        assert ['key3', 'key4'] == self.d.next_keys('key', 2)
         self.mock_cache.incr.assert_called_once_with('key', 2, 'ns', 0)
 
     def test_add(self):
         """
         self.mock_cache.incr.return_value = 1
         self.mock_cache.add.return_value = True
-        assert self.d.add('k')
-        self.mock_cache.add.assert_called_once_with(
-            'key1', 'k', 10, 'default')
-
-        self.mock_cache.reset_mock()
-        self.mock_cache.incr.return_value = 1
-        self.mock_cache.add.return_value = True
-        assert self.d.add('k', 'ns')
+        assert self.d.add('key', 'k')
         self.mock_cache.add.assert_called_once_with(
             'key1', 'k', 10, 'ns')
 
         """
         self.mock_cache.incr.return_value = 1
         self.mock_cache.add.return_value = True
-        assert self.d.add_multi(['k'])
+        assert self.d.add_multi('key', ['k'])
         self.mock_cache.add_multi.assert_called_once_with(
-            {'key1': 'k'}, 10, '', 'default')
+            {'key1': 'k'}, 10, '', 'ns')
 
         self.mock_cache.reset_mock()
         self.mock_cache.incr.return_value = 1
         self.mock_cache.add.return_value = True
-        assert self.d.add_multi(['k'], 'prefix-')
+        assert self.d.add_multi('key', ['k'], 'prefix-')
         self.mock_cache.add_multi.assert_called_once_with(
-            {'key1': 'prefix-k'}, 10, '', 'default')
-
-        self.mock_cache.reset_mock()
-        self.mock_cache.incr.return_value = 1
-        self.mock_cache.add.return_value = True
-        assert self.d.add_multi(['k'], namespace='ns')
-        self.mock_cache.add_multi.assert_called_once_with(
-            {'key1': 'k'}, 10, '', 'ns')
+            {'key1': 'prefix-k'}, 10, '', 'ns')
 
     def test_delete(self):
         """ Ensure related keys are invalidated.
         """
         self.mock_cache.get.return_value = None
-        assert self.d.delete()
+        assert self.d.delete('key')
         self.mock_cache.get.assert_called_once_with(
-            'key', 'default')
+            'key', 'ns')
 
         self.mock_cache.reset_mock()
         self.mock_cache.get.return_value = None
-        assert self.d.delete('ns')
+        assert self.d.delete('key')
         self.mock_cache.get.assert_called_once_with(
             'key', 'ns')
 
         self.mock_cache.reset_mock()
         self.mock_cache.get.return_value = 2
         self.mock_cache.get_multi.side_effect = side_effect
-        assert self.d.delete()
+        assert self.d.delete('key')
         self.mock_cache.get.assert_called_once_with(
-            'key', 'default')
+            'key', 'ns')
         self.mock_cache.get_multi.assert_called_once_with(
-            ANY, '', 'default')
+            ANY, '', 'ns')
         self.mock_cache.delete_multi.assert_called_once_with(
-            ANY, 0, '', 'default')
+            ANY, 0, '', 'ns')
         assert ['k1', 'k2', 'key', 'key1', 'key2'] == sorted(
             self.mock_cache.delete_multi.call_args[0][0])

src/wheezy/caching/tests/test_patterns.py

     def test_dependency(self):
         """ Ensure returned CacheDependency is properly initialized.
         """
-        d = self.cached.dependency('master_key')
+        d = self.cached.dependency
         assert self.cached.cache == d.cache
-        assert 'master_key' == d.master_key
         assert self.cached.time == d.time
         assert self.cached.namespace == d.namespace