Commits

Mike Bayer committed 8c0998f

- changelog for #20
- use py31 compatible callable()
- use a ternary, as expiration_time will often be "0" for dynamic

Comments (0)

Files changed (4)

docs/build/changelog.rst

 
     .. change::
         :tags: feature
+        :tickets: 20
+
+      The ``expiration_time`` argument passed to
+      :meth:`.CacheRegion.cache_on_arguments`
+      may be a callable, to return a dynamic
+      timeout value.  Courtesy David Beitey.
+
+    .. change::
+        :tags: feature
         :tickets: 26
 
       Added support for simple augmentation of existing

dogpile/cache/compat.py

 
 
 py3k = sys.version_info >= (3, 0)
+py32 = sys.version_info >= (3, 2)
 jython = sys.platform.startswith('java')
 
 
     text_type = str
     string_type = str
 
+    if py32:
+        callable = callable
+    else:
+        def callable(fn):
+            return hasattr(fn, '__call__')
+
     import configparser
     import io
 else:
     import ConfigParser as configparser
     import StringIO as io
 
+    callable = callable
+
 if py3k or jython:
     import pickle
 else:

dogpile/cache/region.py

     memoized_property, coerce_string_conf
 from .api import NO_VALUE, CachedValue
 from .proxy import ProxyBackend
+from . import compat
 import time
 from functools import wraps
 import threading
          being declared.
         :param expiration_time: if not None, will override the normal
          expiration time.
-        
+
          May be specified as a callable, taking no arguments, that
          returns a value to be used as the ``expiration_time``. This callable
          will be called whenever the decorated function itself is called, in
          result.  Example use cases include "cache the result until the
          end of the day, week or time period" and "cache until a certain date
          or time passes".
+
+         .. versionchanged:: 0.4.4
+            ``expiration_time`` may be passed as a callable to
+            :meth:`.CacheRegion.cache_on_arguments`.
+
         :param should_cache_fn: passed to :meth:`.CacheRegion.get_or_create`.
 
           .. versionadded:: 0.4.3
 
         """
-        expiration_time_is_callable = callable(expiration_time)
+        expiration_time_is_callable = compat.callable(expiration_time)
         def decorator(fn):
             key_generator = self.function_key_generator(namespace, fn)
             @wraps(fn)
                 @wraps(fn)
                 def creator():
                     return fn(*arg, **kw)
-                timeout = expiration_time_is_callable and \
-                        expiration_time() or expiration_time
+                timeout = expiration_time() if expiration_time_is_callable \
+                            else expiration_time
                 return self.get_or_create(key, creator, timeout,
                                           should_cache_fn)
 

tests/cache/test_decorator.py

         time.sleep(.3)
         eq_(go(1, 2), (3, 1, 2))
 
+    def test_decorator_expire_callable_zero(self):
+        go = self._fixture(expiration_time=lambda: 0)
+        eq_(go(1, 2), (1, 1, 2))
+        eq_(go(1, 2), (2, 1, 2))
+        eq_(go(1, 2), (3, 1, 2))
+
     def test_explicit_expire(self):
         go = self._fixture(expiration_time=1)
         eq_(go(1, 2), (1, 1, 2))