Commits

Andriy Kornatskyy  committed 50671bf

Refactored Cached to use dependency_key_factory in cache patterns.

  • Participants
  • Parent commits cdec846

Comments (0)

Files changed (2)

File src/wheezy/caching/patterns.py

         self.key_prefix = key_prefix
         self.dependency = CacheDependency(cache, time, namespace)
 
-    def set(self, key, value, dependency=None):
+    def set(self, key, value, dependency_key=None):
         """ Sets a key's value, regardless of previous contents
-            in cache. If *dependency* is specified the *key* is added.
+            in cache.
         """
         succeed = self.cache.set(key, value, self.time, self.namespace)
-        if dependency:
-            dependency.add(key, self.namespace)
+        if dependency_key:
+            self.dependency.add(dependency_key, key)
         return succeed
 
     def set_multi(self, mapping, key_prefix=''):
         return self.cache.set_multi(mapping, self.time, key_prefix,
                                     self.namespace)
 
-    def add(self, key, value, dependency=None):
+    def add(self, key, value, dependency_key=None):
         """ Sets a key's value, if and only if the item is not
-            already. If *dependency* is specified the *key* is added.
+            already.
         """
         succeed = self.cache.add(key, value, self.time, self.namespace)
-        if succeed and dependency:
-            dependency.add(key, self.namespace)
+        if succeed and dependency_key:
+            self.dependency.add(dependency_key, key)
         return succeed
 
     def add_multi(self, mapping, key_prefix=''):
         """
         return self.cache.decr(key, delta, self.namespace, initial_value)
 
-    def get_or_add(self, key, create_factory, dependency_factory):
+    def get_or_add(self, key, create_factory, dependency_key_factory):
         """ Cache Pattern: get an item by *key* from *cache* and
             if it is not available use *create_factory* to aquire one.
             If result is not `None` use cache `add` operation to store
-            result and if operation succeed use *dependency_factory*
-            to get an instance of `CacheDependency` to add *key* to it.
+            result and if operation succeed use *dependency_key_factory*
+            to get an instance of `dependency_key` to link with *key*.
         """
         result = self.cache.get(key, self.namespace)
         if result is not None:
         result = create_factory()
         if result is not None:
             succeed = self.cache.add(key, result, self.time, self.namespace)
-            if succeed and dependency_factory is not None:
-                dependency = dependency_factory()
-                dependency.add(key, self.namespace)
+            if succeed and dependency_key_factory is not None:
+                self.dependency.add(dependency_key_factory(), key)
         return result
 
     def wraps_get_or_add(self, wrapped):
             return result
         return get_or_add_wrapper
 
-    def get_or_set(self, key, create_factory, dependency_factory=None):
+    def get_or_set(self, key, create_factory, dependency_key_factory=None):
         """ Cache Pattern: get an item by *key* from *cache* and
             if it is not available use *create_factory* to aquire one.
             If result is not `None` use cache `set` operation to store
-            result and use *dependency_factory* to get an instance of
-            `CacheDependency` to add *key* to it.
+            result and use *dependency_key_factory* to get an instance
+            of `dependency_key` to link with *key*.
         """
         result = self.cache.get(key, self.namespace)
         if result is not None:
         result = create_factory()
         if result is not None:
             self.cache.set(key, result, self.time, self.namespace)
-            if dependency_factory is not None:
-                dependency = dependency_factory()
-                dependency.add(key, self.namespace)
+            if dependency_key_factory is not None:
+                self.dependency.add(dependency_key_factory(), key)
         return result
 
     def __call__(self, wrapped):
             return result
         return get_or_set_wrapper
 
-    def one_pass_create(self, key, create_factory, dependency_factory=None):
+    def one_pass_create(self, key, create_factory,
+                        dependency_key_factory=None):
         """ Cache Pattern: try enter one pass: (1) if entered
             use *create_factory* to get a value if result is not `None`
             use cache `set` operation to store result and use
-            *dependency_factory* to get an instance of `CacheDependency`
-            to add *key* to it; (2) if not entered `wait` until one
-            pass is available and it is not timed out get an item by *key*
-            from *cache*.
+            *dependency_key_factory* to get an instance of `dependency_key`
+            to link with *key*; (2) if not entered `wait` until one pass is
+            available and it is not timed out get an item by *key* from
+            *cache*.
         """
         result = None
         one_pass = OnePass(self.cache, self.key_prefix + key,
                 result = create_factory()
                 if result is not None:
                     self.cache.set(key, result, self.time, self.namespace)
-                    if dependency_factory is not None:
-                        dependency = dependency_factory()
-                        dependency.add(key, self.namespace)
+                    if dependency_key_factory is not None:
+                        self.dependency.add(dependency_key_factory(), key)
             elif one_pass.wait():
                 result = self.cache.get(key, self.namespace)
         finally:
             one_pass.__exit__(None, None, None)
         return result
 
-    def get_or_create(self, key, create_factory, dependency_factory=None):
+    def get_or_create(self, key, create_factory, dependency_key_factory=None):
         """ Cache Pattern: get an item by *key* from *cache* and
             if it is not available see `one_pass_create`.
         """
         result = self.cache.get(key, self.namespace)
         if result is not None:
             return result
-        return self.one_pass_create(key, create_factory, dependency_factory)
+        return self.one_pass_create(key, create_factory,
+                                    dependency_key_factory)
 
     def wraps_get_or_create(self, wrapped):
         """ Returns specialized decorator for `get_or_create` cache

File src/wheezy/caching/tests/test_patterns.py

         self.mock_cache = Mock()
         self.mock_dependency = Mock()
         self.cached = Cached(self.mock_cache, time=10, namespace='ns')
+        self.cached.dependency = self.mock_dependency
 
     def test_set(self):
         """ Ensure set operation is passed to cache.
         """ Ensure set operation is passed to cache and
             key added to dependency.
         """
-        self.cached.set('key', 'value', self.mock_dependency)
+        self.cached.set('key', 'value', 'master_key')
         self.mock_cache.set.assert_called_once_with('key', 'value', 10, 'ns')
-        self.mock_dependency.add.assert_called_once_with('key', 'ns')
+        self.mock_dependency.add.assert_called_once_with('master_key', 'key')
 
     def test_set_multi(self):
         """ Ensure set_multi operation is passed to cache.
         """ Ensure add operation is passed to cache and
             key added to dependency.
         """
-        self.cached.add('key', 'value', self.mock_dependency)
+        self.cached.add('key', 'value', 'master_key')
         self.mock_cache.add.assert_called_once_with('key', 'value', 10, 'ns')
-        self.mock_dependency.add.assert_called_once_with('key', 'ns')
+        self.mock_dependency.add.assert_called_once_with('master_key', 'key')
 
     def test_add_multi(self):
         """ Ensure add_multi operation is passed to cache.
     def test_dependency(self):
         """ Ensure returned CacheDependency is properly initialized.
         """
-        d = self.cached.dependency
-        assert self.cached.cache == d.cache
-        assert self.cached.time == d.time
-        assert self.cached.namespace == d.namespace
+        from wheezy.caching.patterns import Cached
+        cached = Cached(self.mock_cache, time=10, namespace='ns')
+        d = cached.dependency
+        assert cached.cache == d.cache
+        assert cached.time == d.time
+        assert cached.namespace == d.namespace
 
 
 class OnePassTestCase(unittest.TestCase):
     def setUp(self):
         self.mock_cache = Mock()
         self.mock_create_factory = Mock()
+        self.mock_dependency = Mock()
 
-    def get_or_add(self, dependency_factory=None):
+    def get_or_add(self, dependency_key_factory=None):
         from wheezy.caching.patterns import Cached
         cached = Cached(self.mock_cache, time=10, namespace='ns')
+        cached.dependency = self.mock_dependency
         return cached.get_or_add('key', self.mock_create_factory,
-                                 dependency_factory)
+                                 dependency_key_factory)
 
     def test_found(self):
         """ An item found in cache.
         self.mock_cache.get.return_value = None
         self.mock_create_factory.return_value = 'x'
         self.mock_cache.add.return_value = True
-        mock_dependency_factory = Mock()
+        mock_dependency_key_factory = Mock()
+        mock_dependency_key_factory.return_value = 'master_key'
 
-        assert 'x' == self.get_or_add(mock_dependency_factory)
+        assert 'x' == self.get_or_add(mock_dependency_key_factory)
         self.mock_cache.get.assert_called_once_with('key', 'ns')
         self.mock_cache.add.assert_called_once_with('key', 'x', 10, 'ns')
-        mock_dependency_factory.return_value.add.assert_called_once_with(
-            'key', 'ns')
+        self.mock_dependency.add.assert_called_once_with('master_key', 'key')
 
 
 class WrapsGetOrAddTestCase(GetOrAddTestCase):
     def setUp(self):
         self.mock_cache = Mock()
         self.mock_create_factory = Mock()
+        self.mock_dependency = Mock()
 
-    def get_or_set(self, dependency_factory=None):
+    def get_or_set(self, dependency_key_factory=None):
         from wheezy.caching.patterns import Cached
         cached = Cached(self.mock_cache, time=10, namespace='ns')
+        cached.dependency = self.mock_dependency
         return cached.get_or_set('key', self.mock_create_factory,
-                                 dependency_factory)
+                                 dependency_key_factory)
 
     def test_found(self):
         """ An item found in cache.
         self.mock_cache.get.return_value = None
         self.mock_create_factory.return_value = 'x'
         self.mock_cache.set.return_value = True
-        mock_dependency_factory = Mock()
+        mock_dependency_key_factory = Mock()
+        mock_dependency_key_factory.return_value = 'master_key'
 
-        assert 'x' == self.get_or_set(mock_dependency_factory)
+        assert 'x' == self.get_or_set(mock_dependency_key_factory)
         self.mock_cache.get.assert_called_once_with('key', 'ns')
         self.mock_cache.set.assert_called_once_with('key', 'x', 10, 'ns')
-        mock_dependency_factory.return_value.add.assert_called_once_with(
-            'key', 'ns')
+        self.mock_dependency.add.assert_called_once_with('master_key', 'key')
 
 
 class WrapsGetOrSetTestCase(GetOrSetTestCase):
     def setUp(self):
         self.mock_cache = Mock()
         self.mock_create_factory = Mock()
+        self.mock_dependency = Mock()
 
-    def one_pass_create(self, dependency_factory=None):
+    def one_pass_create(self, dependency_key_factory=None):
         from wheezy.caching.patterns import Cached
         cached = Cached(self.mock_cache, time=10, namespace='ns', timeout=5)
+        cached.dependency = self.mock_dependency
         return cached.one_pass_create('key', self.mock_create_factory,
-                                      dependency_factory)
+                                      dependency_key_factory)
 
     def test_create_none(self):
         """ One pass has been entered and create factory returns None.
         """
         self.mock_cache.add.return_value = True
         self.mock_create_factory.return_value = 'x'
-        mock_dependency_factory = Mock()
-        assert 'x' == self.one_pass_create(mock_dependency_factory)
+        mock_dependency_key_factory = Mock()
+        mock_dependency_key_factory.return_value = 'master_key'
+        assert 'x' == self.one_pass_create(mock_dependency_key_factory)
 
         self.mock_cache.add.assert_called_once_with(
             'one_pass:key', ANY, 5, 'ns')
         self.mock_create_factory.assert_called_once_with()
         self.mock_cache.set.assert_called_once_with('key', 'x', 10, 'ns')
         self.mock_cache.delete.assert_called_once_with('one_pass:key', 'ns')
-        mock_dependency_factory.return_value.add.assert_called_once_with(
-            'key', 'ns')
+        self.mock_dependency.add.assert_called_once_with('master_key', 'key')
 
     @patch('wheezy.caching.patterns.OnePass')
     def test_wait_timedout(self, mock_cls_one_pass):