Anonymous avatar Anonymous committed 32d628d

Updated per pull request #13 comments

Use NO_VALUE to not cache result instead of cache_none=False

Comments (0)

Files changed (2)

dogpile/cache/region.py

 
         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"]
 
 
         self.backend.delete(key)
 
-    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)
                 @wraps(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)

tests/cache/test_region.py

         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):
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.