Andriy Kornatskyy avatar Andriy Kornatskyy committed 4d6d656

Refactored cache dependency by moving cache instance to constructor.

Comments (0)

Files changed (1)

src/wheezy/caching/dependency.py

         simplifing code necessary to manage dependencies in cache.
     """
 
-    __slots__ = ['master_key', 'time']
+    __slots__ = ('cache', 'master_key', 'time')
 
-    def __init__(self, master_key, time=0):
+    def __init__(self, cache, master_key, time=0):
+        """
+           *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.
+        """
+        self.cache = cache
         self.master_key = master_key
         self.time = time
 
-    def next_key(self, cache, namespace=None):
+    def next_key(self, namespace=None):
         """ Returns the next unique key for dependency.
 
             >>> from wheezy.caching.memory import MemoryCache
-            >>> d = CacheDependency('key')
             >>> c = MemoryCache()
-            >>> d.next_key(c)
+            >>> d = CacheDependency(c, 'key')
+            >>> d.next_key()
             'key1'
-            >>> d.next_key(c)
+            >>> d.next_key()
             'key2'
         """
-        return self.master_key + str(cache.incr(
+        return self.master_key + str(self.cache.incr(
             self.master_key, 1, namespace, 0))
 
-    def next_keys(self, cache, n, namespace=None):
+    def next_keys(self, n, namespace=None):
         """ Returns ``n`` number of dependency keys.
 
             >>> from wheezy.caching.memory import MemoryCache
-            >>> d = CacheDependency('key')
             >>> c = MemoryCache()
-            >>> d.next_keys(c, 1)
+            >>> d = CacheDependency(c, 'key')
+            >>> d.next_keys(1)
             ['key1']
-            >>> d.next_keys(c, 3)
+            >>> d.next_keys(3)
             ['key2', 'key3', 'key4']
         """
-        last_id = cache.incr(self.master_key, n, namespace, 0)
+        last_id = self.cache.incr(self.master_key, n, namespace, 0)
         return [self.master_key + str(i)
                 for i in xrange(last_id - n + 1, last_id + 1)]
 
-    def add(self, cache, key, namespace=None):
+    def add(self, key, namespace=None):
         """ Adds a given key to dependency.
 
             >>> from wheezy.caching.memory import MemoryCache
-            >>> d = CacheDependency('key')
             >>> c = MemoryCache()
-            >>> d.add(c, 'key-x')
+            >>> d = CacheDependency(c, 'key')
+            >>> d.add('key-x')
             True
             >>> c.get('key1')
             'key-x'
         """
-        return cache.add(
-            self.next_key(cache, namespace),
-            key, self.time, namespace)
+        return self.cache.add(self.next_key(namespace),
+                              key, self.time, namespace)
 
-    def add_multi(self, cache, keys, key_prefix='', namespace=None):
+    def add_multi(self, keys, key_prefix='', namespace=None):
         """ Adds several keys to dependency.
 
             >>> from wheezy.caching.memory import MemoryCache
-            >>> d = CacheDependency('key')
             >>> c = MemoryCache()
-            >>> d.add_multi(c, ('key-x', 'key-y'))
+            >>> d = CacheDependency(c, 'key')
+            >>> d.add_multi(('key-x', 'key-y'))
             []
             >>> c.get('key1')
             'key-x'
 
             With ``key_prefix``
 
-            >>> d.add_multi(c, ('a', 'b'), key_prefix='key-')
+            >>> d.add_multi(('a', 'b'), key_prefix='key-')
             []
             >>> c.get('key3')
             'key-a'
             >>> c.get('key4')
             'key-b'
         """
-        mapping = dict(zip(
-            self.next_keys(cache, len(keys), namespace),
-            map(lambda k: key_prefix + k, keys)))
-        return cache.add_multi(mapping, self.time, '', namespace)
+        mapping = dict(zip(self.next_keys(len(keys), namespace),
+                           map(lambda k: key_prefix + k, keys)))
+        return self.cache.add_multi(mapping, self.time, '', namespace)
 
-    def delete(self, cache, namespace=None):
+    def delete(self, namespace=None):
         """ Delete all items wired by this cache dependency.
 
             >>> from wheezy.caching.memory import MemoryCache
-            >>> d = CacheDependency('key')
             >>> c = MemoryCache()
+            >>> d = CacheDependency(c, 'key')
 
             If there are no dependent items, delete succeed.
 
             >>> mapping = {'key-x': 1, 'key-y': 2}
             >>> c.set_multi(mapping, 100)
             []
-            >>> d.add_multi(c, mapping.keys())
+            >>> d.add_multi(mapping.keys())
             []
             >>> len(c.items)
             5
-            >>> d.delete(c)
+            >>> d.delete()
             True
             >>> len(c.items)
             0
         """
+        cache = self.cache
         n = cache.get(self.master_key, 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.extend(itervalues(cache.get_multi(keys, '', namespace)))
         keys.append(self.master_key)
         return cache.delete_multi(keys, 0, '', namespace)
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.