Issue #50 new

cache_on_arguments' expiration_time doesn't accept timedelta

Antoine Bertin
created an issue

This is feature is already working for the expiration_time in region.configure. I think this should be extended to all expiration_time arguments to make this argument consistent across dogpile.cache.

Comments (3)

  1. Mike Bayer repo owner

    well a good start is, start with one point of handling expiration_time, then apply it consistently, here's part of it

    diff --git a/dogpile/cache/region.py b/dogpile/cache/region.py
    index 7085289..c731e40 100644
    --- a/dogpile/cache/region.py
    +++ b/dogpile/cache/region.py
    @@ -236,25 +236,30 @@ class CacheRegion(object):
             else:
                 self.backend = backend_cls(arguments or {})
    
    -        if not expiration_time or isinstance(expiration_time, Number):
    -            self.expiration_time = expiration_time
    -        elif isinstance(expiration_time, datetime.timedelta):
    -            self.expiration_time = int(compat.timedelta_total_seconds(expiration_time))
    -        else:
    -            raise exception.ValidationError(
    -                'expiration_time is not a number or timedelta.')
    +        self.expiration_time = self._parse_expiration_time(expiration_time)
    
             if self.key_mangler is None:
                 self.key_mangler = self.backend.key_mangler
    
             self._lock_registry = NameRegistry(self._create_mutex)
    
    -        if getattr(wrap,'__iter__', False):
    +        if getattr(wrap, '__iter__', False):
                 for wrapper in reversed(wrap):
                     self.wrap(wrapper)
    
             return self
    
    +    def _parse_expiration_time(self, expiration_time, default=None):
    +        if expiration_time is None:
    +            return default
    +        elif isinstance(expiration_time, Number):
    +            return expiration_time
    +        elif isinstance(expiration_time, datetime.timedelta):
    +            return int(compat.timedelta_total_seconds(expiration_time))
    +        else:
    +            raise exception.ValidationError(
    +                'expiration_time is not a number or timedelta.')
    +
         def wrap(self, proxy):
             ''' Takes a ProxyBackend instance or class and wraps the
             attached backend. '''
    @@ -451,8 +456,8 @@ class CacheRegion(object):
             if ignore_expiration:
                 return lambda value: value
             else:
    -            if expiration_time is None:
    -                expiration_time = self.expiration_time
    +            expiration_time = self._parse_expiration_time(
    +                                expiration_time, self.expiration_time)
    
                 current_time = time.time()
    
  2. Log in to comment