[issue18577] lru_cache enhancement: lru_timestamp helper function
Peter Santoro
report at bugs.python.org
Sun Jul 28 14:32:51 CEST 2013
New submission from Peter Santoro:
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))
----------
components: Library (Lib)
files: lru.py
messages: 193820
nosy: peter at psantoro.net
priority: normal
severity: normal
status: open
title: lru_cache enhancement: lru_timestamp helper function
type: enhancement
versions: Python 3.3
Added file: http://bugs.python.org/file31063/lru.py
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18577>
_______________________________________
More information about the Python-bugs-list
mailing list