mirror of
https://github.com/wassname/catalyst.git
synced 2026-06-28 02:44:59 +08:00
b766ce6ebd
Implements a `CachedObject` utility class for wrapping cached results with an expiration date.
60 lines
1.3 KiB
Python
60 lines
1.3 KiB
Python
"""
|
|
Cached object with an expiration date.
|
|
"""
|
|
from collections import namedtuple
|
|
|
|
|
|
class Expired(Exception):
|
|
pass
|
|
|
|
|
|
class CachedObject(namedtuple("_CachedObject", "value expires")):
|
|
"""
|
|
A simple struct for maintaining a cached object with an expiration date.
|
|
|
|
Parameters
|
|
----------
|
|
value : object
|
|
The object to cache.
|
|
expires : datetime-like
|
|
Expiration date of `value`. The cache is considered invalid for dates
|
|
**strictly greater** than `expires`.
|
|
|
|
Methods
|
|
-------
|
|
get(self, dt)
|
|
Get the cached object.
|
|
|
|
Usage
|
|
-----
|
|
>>> from pandas import Timestamp, Timedelta
|
|
>>> expires = Timestamp('2014', tz='UTC')
|
|
>>> obj = CachedObject(1, expires)
|
|
>>> obj.unwrap(expires - Timedelta('1 minute'))
|
|
1
|
|
>>> obj.unwrap(expires)
|
|
1
|
|
>>> obj.unwrap(expires + Timedelta('1 minute'))
|
|
Traceback (most recent call last):
|
|
...
|
|
Expired: 2014-01-01 00:00:00+00:00
|
|
"""
|
|
|
|
def unwrap(self, dt):
|
|
"""
|
|
Get the cached value.
|
|
|
|
Returns
|
|
-------
|
|
value : object
|
|
The cached value.
|
|
|
|
Raises
|
|
------
|
|
Expired
|
|
Raised when `dt` is greater than self.expires.
|
|
"""
|
|
if dt > self.expires:
|
|
raise Expired(self.expires)
|
|
return self.value
|