Andriy Kornatskyy avatar Andriy Kornatskyy committed 012e223

Added various partial specializations to cache patterns.

Comments (0)

Files changed (2)

src/wheezy/caching/patterns.py

     return result
 
 
+def partial_get_or_add(cache, time=0, namespace=None,
+                       timeout=10, key_prefix='one_pass:'):
+    """ Specializes `get_or_add` cache pattern to::
+
+            get_or_add(key, create_factory, dependency_factory=None)
+    """
+    def get_or_add_wrapper(key, create_factory, dependency_factory=None):
+        return get_or_add(
+            key, create_factory, dependency_factory,
+            time, namespace, cache)
+    return get_or_add_wrapper
+
+
 def get_or_set(key, create_factory, dependency_factory=None,
                time=0, namespace=None, cache=None):
     """ Cache Pattern: get an item by *key* from *cache* and
     return result
 
 
+def partial_get_or_set(cache, time=0, namespace=None,
+                       timeout=10, key_prefix='one_pass:'):
+    """ Specializes `get_or_set` cache pattern to::
+
+            get_or_set(key, create_factory, dependency_factory=None)
+    """
+    def get_or_set_wrapper(key, create_factory, dependency_factory=None):
+        return get_or_set(
+            key, create_factory, dependency_factory,
+            time, namespace, cache)
+    return get_or_set_wrapper
+
+
 def one_pass_create(key, create_factory, dependency_factory=None,
                     time=0, namespace=None, cache=None,
                     timeout=10, key_prefix='one_pass:'):
     return result
 
 
+def partial_one_pass_create(cache, time=0, namespace=None,
+                            timeout=10, key_prefix='one_pass:'):
+    """ Specializes `one_pass_create` cache pattern to::
+
+            one_pass_create(key, create_factory, dependency_factory=None)
+    """
+    def one_pass_create_wrapper(key, create_factory, dependency_factory=None):
+        return one_pass_create(
+            key, create_factory, dependency_factory,
+            time, namespace, cache,
+            timeout, key_prefix)
+    return one_pass_create_wrapper
+
+
 def get_or_create(key, create_factory, dependency_factory=None,
                   time=0, namespace=None, cache=None,
                   timeout=10, key_prefix='one_pass:'):
                            time, namespace, cache, timeout, key_prefix)
 
 
+def partial_get_or_create(cache, time=0, namespace=None,
+                          timeout=10, key_prefix='one_pass:'):
+    """ Specializes `get_or_create` cache pattern to::
+
+            get_or_create(key, create_factory, dependency_factory=None)
+    """
+    def get_or_create_wrapper(key, create_factory, dependency_factory=None):
+        result = cache.get(key, namespace)
+        if result is not None:
+            return result
+        return one_pass_create(
+            key, create_factory, dependency_factory,
+            time, namespace, cache,
+            timeout, key_prefix)
+    return get_or_create_wrapper
+
+
 class OnePass(object):
     """ A solution to `Thundering Head` problem.
 

src/wheezy/caching/tests/test_patterns.py

         mock_dependency_factory.return_value.add.assert_called_once_with(
             'key', 'ns')
 
+    @patch('wheezy.caching.patterns.get_or_add')
+    def test_partial(self, mock_get_or_add):
+        """ Ensure call defaults.
+        """
+        from wheezy.caching.patterns import partial_get_or_add
+        cached = partial_get_or_add(
+            'cache', time='time', namespace='namespace')
+
+        cached('key', 'create_factory', 'dependency_factory')
+        mock_get_or_add.assert_called_once_with(
+            'key', 'create_factory', 'dependency_factory',
+            'time', 'namespace', 'cache')
+
 
 class GetOrSetTestCase(unittest.TestCase):
 
         mock_dependency_factory.return_value.add.assert_called_once_with(
             'key', 'ns')
 
+    @patch('wheezy.caching.patterns.get_or_set')
+    def test_partial(self, mock_get_or_set):
+        """ Ensure call defaults.
+        """
+        from wheezy.caching.patterns import partial_get_or_set
+        cached = partial_get_or_set(
+            'cache', time='time', namespace='namespace')
+
+        cached('key', 'create_factory', 'dependency_factory')
+        mock_get_or_set.assert_called_once_with(
+            'key', 'create_factory', 'dependency_factory',
+            'time', 'namespace', 'cache')
+
 
 class OnePassCreateTestCase(unittest.TestCase):
 
 
         assert 'x' == self.one_pass_create()
 
+    @patch('wheezy.caching.patterns.one_pass_create')
+    def test_partial(self, mock_one_pass_create):
+        """ Ensure call defaults.
+        """
+        from wheezy.caching.patterns import partial_one_pass_create
+        cached = partial_one_pass_create(
+            'cache', time='time', namespace='namespace',
+            timeout='timeout', key_prefix='key_prefix')
+
+        cached('key', 'create_factory', 'dependency_factory')
+        mock_one_pass_create.assert_called_once_with(
+            'key', 'create_factory', 'dependency_factory',
+            'time', 'namespace', 'cache',
+            'timeout', 'key_prefix')
+
 
 class GetOrCreateTestCase(unittest.TestCase):
 
         self.mock_create_factory.return_value = 'x'
         assert 'x' == self.get_or_create()
         self.mock_cache.get.assert_called_once_with('key', 'ns')
+
+    def test_partial_found(self):
+        """ An item found in cache.
+        """
+        from wheezy.caching.patterns import partial_get_or_create
+        self.mock_cache.get.return_value = 'x'
+        cached = partial_get_or_create(
+            self.mock_cache, time='time', namespace='namespace',
+            timeout='timeout', key_prefix='key_prefix')
+
+        assert 'x' == cached('key', None)
+
+    @patch('wheezy.caching.patterns.one_pass_create')
+    def test_partial(self, mock_one_pass_create):
+        """ Ensure call defaults.
+        """
+        from wheezy.caching.patterns import partial_get_or_create
+        self.mock_cache.get.return_value = None
+        cached = partial_get_or_create(
+            self.mock_cache, time='time', namespace='namespace',
+            timeout='timeout', key_prefix='key_prefix')
+
+        cached('key', 'create_factory', 'dependency_factory')
+        mock_one_pass_create.assert_called_once_with(
+            'key', 'create_factory', 'dependency_factory',
+            'time', 'namespace', self.mock_cache,
+            'timeout', 'key_prefix')
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.