summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Hanchrow <ehanchrow@ine.com>2014-01-06 10:31:25 -0800
committerEric Hanchrow <ehanchrow@ine.com>2014-01-06 10:45:38 -0800
commitead71f38f2048d759202b61b090894ba5b6309f9 (patch)
tree16916692712d0af44d610ad2d9f1377d077a524c
parent10dfea269c6e1d6c58ee1decee603a92f90b2c14 (diff)
downloaddogpile-cache-ead71f38f2048d759202b61b090894ba5b6309f9.tar.gz
New "get" attribute, analogous to "invalidate" and "refresh"
-rw-r--r--dogpile/cache/region.py20
-rw-r--r--tests/cache/test_decorator.py10
2 files changed, 27 insertions, 3 deletions
diff --git a/dogpile/cache/region.py b/dogpile/cache/region.py
index 7085289..04be3ed 100644
--- a/dogpile/cache/region.py
+++ b/dogpile/cache/region.py
@@ -875,6 +875,20 @@ class CacheRegion(object):
.. 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"
@@ -1005,6 +1019,10 @@ class CacheRegion(object):
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)
@@ -1014,6 +1032,7 @@ class CacheRegion(object):
decorate.set = set_
decorate.invalidate = invalidate
decorate.refresh = refresh
+ decorate.get = get
return decorate
return decorator
@@ -1205,4 +1224,3 @@ def make_region(*arg, **kw):
"""
return CacheRegion(*arg, **kw)
-
diff --git a/tests/cache/test_decorator.py b/tests/cache/test_decorator.py
index d2c142c..00f7cbd 100644
--- a/tests/cache/test_decorator.py
+++ b/tests/cache/test_decorator.py
@@ -99,6 +99,14 @@ class DecoratorTest(_GenericBackendFixture, TestCase):
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'])
@@ -385,5 +393,3 @@ class CacheDecoratorTest(_GenericBackendFixture, TestCase):
generate.set({7: 18, 10: 15})
eq_(generate(2, 7, 10), ['2 5', 18, 15])
-
-