Commits

Mike Bayer committed e0120b3 Merge

Merged in gillesdevaux/dogpile.cache/no_cache_none (pull request #13)

Don't cache NO_VALUE

  • Participants
  • Parent commits c067ccd, 32d628d

Comments (0)

Files changed (2)

dogpile/cache/region.py

         creation function may or may not be used to recreate the value
         and persist the newly generated value in the cache.
 
+        If the creation function returns :const:`NO_VALUE`, nothing is cached.
+        Note that if the returns `None`, `None` will be cached.
+
         Whether or not the function is used depends on if the
         *dogpile lock* can be acquired or not.  If it can't, it means
         a different thread or process is already running a creation
 
         def gen_value():
             value = self._value(creator())
-            self.backend.set(key, value)
+            if value.payload is not NO_VALUE:
+                self.backend.set(key, value)
             return value.payload, value.metadata["ct"]
 
         if expiration_time is None:

tests/cache/test_region.py

         eq_(reg.get_or_create("some key", creator), 
                     "some value 2")
 
+    def test_not_cache_NO_VALUE(self):
+        reg = self._region()
+        def creator():
+            return NO_VALUE
+        reg.get_or_create("some key", creator)
+        self.assertNotIn("some key", reg.backend._cache)
+
 class CacheDecoratorTest(TestCase):
     def _region(self, init_args={}, config_args={}, backend="mock"):
         reg = CacheRegion(**init_args)