Mike Bayer avatar Mike Bayer committed de67bd7

- [bug] Fixed bug whereby region.get() didn't
work if the value wasn't present. #8
- 100% coverage for region.py (even though coverage alone wasn't enough for #8)

Comments (0)

Files changed (3)

 0.3.0
 =====
-- get() method now checks expiration time 
+- [feature] get() method now checks expiration time 
   by default.   Use ignore_expiration=True
   to bypass this.
 
-- Added new invalidate() method.  Sets the current
+- [feature] Added new invalidate() method.  Sets the current
   timestamp as a minimum value that all retrieved
   values must be created after.  Is honored by the
   get_or_create() and get() methods. #7
 
+- [bug] Fixed bug whereby region.get() didn't 
+  work if the value wasn't present.  #8
+
 0.2.4
 =====
 - Fixed py3k issue with config string coerce,

dogpile/cache/region.py

         if "backend" in self.__dict__:
             raise Exception(
                     "This region is already "
-                    "configured with the %s backend" 
+                    "configured with backend: %s" 
                     % self.backend)
         backend_cls = _backend_loader.load(backend)
         if _config_argument_dict:
         if self.key_mangler:
             key = self.key_mangler(key)
         value = self.backend.get(key)
-
-        if not ignore_expiration:
+        if value is NO_VALUE:
+            return value
+        elif not ignore_expiration:
             if expiration_time is None:
                 expiration_time = self.expiration_time
             if expiration_time is not None and \

tests/cache/test_region.py

         reg.delete("some key")
         eq_(reg.get("some key"), NO_VALUE)
 
+    def test_dupe_config(self):
+        reg = CacheRegion()
+        reg.configure("mock")
+        assert_raises_message(
+            Exception,
+            "This region is already configured",
+            reg.configure, "mock"
+        )
+
     def test_no_config(self):
         reg = CacheRegion()
         assert_raises_message(
     def test_set_get_nothing(self):
         reg = self._region()
         eq_(reg.get("some key"), NO_VALUE)
+        eq_(reg.get("some key", expiration_time=10), NO_VALUE)
+        reg.invalidate()
+        eq_(reg.get("some key"), NO_VALUE)
 
     def test_creator(self):
         reg = self._region()
         reg.invalidate()
         eq_(reg.get_or_create("some key", creator), 
                     "some value 2")
+
+class CacheDecoratorTest(TestCase):
+    def _region(self, init_args={}, config_args={}, backend="mock"):
+        reg = CacheRegion(**init_args)
+        reg.configure(backend, **config_args)
+        return reg
+
+    def test_cache_arg(self):
+        reg = self._region()
+
+        counter = itertools.count(1)
+
+        @reg.cache_on_arguments()
+        def generate(x, y):
+            return next(counter) + x + y
+
+        eq_(generate(1, 2), 4)
+        eq_(generate(2, 1), 5)
+        eq_(generate(1, 2), 4)
+        generate.invalidate(1, 2)
+        eq_(generate(1, 2), 6)
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.