Message193820
The attached proposed lru_timestamp function provides developers with more control over how often lru_cache entries are refreshed. Doc string follows:
def lru_timestamp(refresh_interval=60):
""" Return a timestamp string for @lru_cache decorated functions.
The returned timestamp is used as the value of an extra parameter
to @lru_cache decorated functions, allowing for more control over
how often cache entries are refreshed. The lru_timestamp function
should be called with the same refresh_interval value for a given
lru_cache decorated function.
Positional arguments:
refresh_interval -- 1-1440 minutes (default 60) as int or float
"""
Rationale:
Some functions have input parameters that rarely change, but yet return different results over time. It would be nice to have a ready-made solution to force lru_cache entries to be refreshed at specified time intervals.
An common example is using a stable userid to read user information from a database. By itself, the lru_cache decorator can be used to cache the user information and prevent unnecessary i/o. However, if a given user's information is updated in the database, but the associated lru_cache entry has not yet been discarded, the application will be using stale data. The lru_timestamp function is a simple, ready-made helper function that gives the developer more control over the age of lru_cache entries in such situations.
Sample usage:
@functools.lru_cache()
def user_info(userid, timestamp):
# expensive database i/o, but value changes over time
# the timestamp parameter is normally not used, it is
# for the benefit of the @lru_cache decorator
pass
# read user info from database, if not in cache or
# older than 120 minutes
info = user_info('johndoe', functools.lru_timestamp(120)) |
|
Date |
User |
Action |
Args |
2013-07-28 12:32:51 | peter@psantoro.net | set | recipients:
+ peter@psantoro.net |
2013-07-28 12:32:51 | peter@psantoro.net | set | messageid: <1375014771.52.0.855210720887.issue18577@psf.upfronthosting.co.za> |
2013-07-28 12:32:51 | peter@psantoro.net | link | issue18577 messages |
2013-07-28 12:32:51 | peter@psantoro.net | create | |
|