Commits

Mike Bayer  committed 44a16a1 Merge

Merged in ijl/dogpile.cache (pull request #20)

CacheRegion.configure() accepts a datetime.timedelta object for 'expiration_time', and rejects non-number, non-timedelta objects

  • Participants
  • Parent commits 9155443, 84aae5e

Comments (0)

Files changed (2)

File dogpile/cache/region.py

 from .api import NO_VALUE, CachedValue
 from .proxy import ProxyBackend
 from . import compat
-import time
+import time, datetime
+from numbers import Number
 from functools import wraps
 import threading
 
             )
         else:
             self.backend = backend_cls(arguments or {})
-        self.expiration_time = expiration_time
+
+        if not expiration_time or isinstance(expiration_time, Number):
+            self.expiration_time = expiration_time 
+        elif isinstance(expiration_time, datetime.timedelta):
+            self.expiration_time = int(expiration_time.total_seconds())
+        else:
+            raise Exception('expiration_time is not a number or timedelta.')
+
         if self.key_mangler is None:
             self.key_mangler = self.backend.key_mangler
 

File tests/cache/test_region.py

 from dogpile.cache import make_region, register_backend, CacheRegion, util
 from dogpile.cache.proxy import ProxyBackend
 from . import eq_, is_, assert_raises_message, io, configparser
-import time
+import time, datetime
 import itertools
 from collections import defaultdict
 import operator
         eq_(my_region.backend.arguments, {'url': '127.0.0.1',
                             'dogpile_lockfile':False, 'xyz':None})
 
+    def test_datetime_expiration_time(self):
+        my_region = make_region()
+        my_region.configure(
+            backend='mock', 
+            expiration_time=datetime.timedelta(days=1, hours=8)
+        )
+        eq_(my_region.expiration_time, 32*60*60)
+
+    def test_reject_invalid_expiration_time(self):
+        my_region = make_region()
+        
+        assert_raises_message(
+            Exception,
+            "expiration_time is not a number or timedelta.",
+            my_region.configure, 'mock', 'one hour'
+        )
+
     def test_key_mangler_argument(self):
         reg = self._region(init_args={"key_mangler":key_mangler})
         assert reg.key_mangler is key_mangler