Allow expiration_time to be a callable
My current use-case is to cache a set of values for a relative amount of time -- in one case, until the end of the current day, in another, until the end of the given week, and finally, until a certain date/time.
Currently, dogpile.cache accepts an expiration_time as an integer, which represents a fixed number seconds from now (eg in 24 hours time), and not a relative or otherwise dynamic value. In order to obtain a relative
expiration_time for use, you could use
get_or_create directly and calculate the necessary value when run, but for the
cache_on_arguments decorator, this isn't possible given its nature as a decorator.
So, my suggestion is to allow
expiration_time to be specified as callable that returns a integer and call this whenever expiration_time is used (eg in the CacheRegion.get and get_or_create functions). Thus, the expiration time is dynamic and since a function, the resulting relative time could be based upon anything (not just relative times as I mention above).
class CacheRegion(object): ... def get_or_create(self, key, creator, expiration_time=None): if hasattr(expiration_time, '__call__'): expiration_time = expiration_time() ... def seconds_til_tomorrow(): #or something else like a database call or whatever tomorrow = date.today() + timedelta(days=1) til_tomorrow = datetime.combine(tomorrow, time(0)) - datetime.now() return math.ceil(til_tomorrow.total_seconds()) region = make_region().configure('memory', expiration_time=seconds_til_tomorrow) config.rest_of_day.get_or_create('1', lambda: datetime.date.today()) #Caches value of today's date til tomorrow