Gilles  committed 32d628d

Updated per pull request #13 comments

Use NO_VALUE to not cache result instead of cache_none=False

  • Participants
  • Parent commits f6c8b44
  • Branches no_cache_none

Comments (0)

Files changed (2)

File dogpile/cache/

         return value.payload
-    def get_or_create(self, key, creator, expiration_time=None, cache_none=True):
+    def get_or_create(self, key, creator, expiration_time=None):
         """Return a cached value based on the given key.
         If the value does not exist or is considered to be expired
         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
         :param expiration_time: optional expiration time which will overide
          the expiration time already configured on this :class:`.CacheRegion`
          if not None.   To set no expiration, use the value -1.
-        :param cache_none: If True (the default) also cache when the
-         decorated function returns None.
         See also:
         def gen_value():
             value = self._value(creator())
-            if cache_none or value.payload:
+            if value.payload is not NO_VALUE:
                 self.backend.set(key, value)
             return value.payload, value.metadata["ct"]
-    def cache_on_arguments(self, namespace=None, expiration_time=None, cache_none=True):
+    def cache_on_arguments(self, namespace=None, expiration_time=None):
         """A function decorator that will cache the return
         value of the function using a key derived from the
         function itself and its arguments.
          being declared.
         :param expiration_time: if not None, will override the normal
          expiration time.
-        :param cache_none: If True (the default) also cache when the
-         decorated function returns None.
         def decorator(fn):
             key_generator = self.function_key_generator(namespace, fn)
                 def creator():
                     return fn(*arg, **kw)
-                return self.get_or_create(key, creator, expiration_time, cache_none)
+                return self.get_or_create(key, creator, expiration_time)
             def invalidate(*arg, **kw):
                 key = key_generator(*arg, **kw)

File tests/cache/

         eq_(reg.get_or_create("some key", creator), 
                     "some value 2")
-    def test_not_cache_none(self):
+    def test_not_cache_NO_VALUE(self):
         reg = self._region()
         def creator():
-            return None
+            return NO_VALUE
         reg.get_or_create("some key", creator)
-        value = reg.backend._cache["some key"]
-        eq_(value.payload, None)
-        reg = self._region()
-        reg.get_or_create("some key", creator, cache_none=False)
         self.assertNotIn("some key", reg.backend._cache)
 class CacheDecoratorTest(TestCase):