Commits

Eric Hanchrow committed ead71f3

New "get" attribute, analogous to "invalidate" and "refresh"

Comments (0)

Files changed (2)

dogpile/cache/region.py

         .. versionadded:: 0.5.0 Added ``refresh()`` method to decorated
            function.
 
+        Lastly, a ``get()`` attribute returns either the value cached
+        for the given key, or else the token ``NO_VALUE``.  So for the example above,
+
+            generate_something.get(5, 6)
+
+        would return ``newvalue``, whereas
+
+            generate_something(99, 100)
+
+        would return ``NO_VALUE``.
+
+        .. versionadded:: 0.5.3 Added ``get()`` method to decorated
+        function.
+
         The default key generation will use the name
         of the function, the module name for the function,
         the arguments passed, as well as an optional "namespace"
                 key = key_generator(*arg, **kw)
                 self.set(key, value)
 
+            def get(*arg, **kw):
+                key = key_generator(*arg, **kw)
+                return self.get(key)
+
             def refresh(*arg, **kw):
                 key = key_generator(*arg, **kw)
                 value = fn(*arg, **kw)
             decorate.set = set_
             decorate.invalidate = invalidate
             decorate.refresh = refresh
+            decorate.get = get
 
             return decorate
         return decorator
 
     """
     return CacheRegion(*arg, **kw)
-

tests/cache/test_decorator.py

         go.set(0, 1, 3)
         eq_(go(1, 3), 0)
 
+    def test_explicit_get(self):
+        go = self._fixture(expiration_time=1)
+        eq_(go(1, 2), (1, 1, 2))
+        eq_(go.get(1, 2), (1, 1, 2))
+        eq_(go.get(2, 1), NO_VALUE)
+        eq_(go(2, 1), (2, 2, 1))
+        eq_(go.get(2, 1), (2, 2, 1))
+
     def test_explicit_set_multi(self):
         go = self._multi_fixture(expiration_time=1)
         eq_(go(1, 2), ['1 1', '1 2'])
 
         generate.set({7: 18, 10: 15})
         eq_(generate(2, 7, 10), ['2 5', 18, 15])
-
-