[issue18577] lru_cache enhancement: lru_timestamp helper function

Ben Hoyt report at bugs.python.org
Wed Sep 3 18:05:42 CEST 2014


Ben Hoyt added the comment:

I really like this idea (and am needing this functionality), but I don't think this API (or implementation) is very nice:

1) It means you have to change your function signature to use the timeout feature.

2) Specifying the interval in minutes seems odd (most similar timeouts in Python are specified in seconds).

I would love to see an optional timeout=seconds keyword arg to the lru_cache() decorator, or some other way to support this.

Raymond, what do you think would be the simplest way to hook this in?

One way I think would be nice (and also support other neat things) is to allow you to specify the dict-like object that's used for the cache (defaults to "dict", of course). So the lru_cache signature would change to:

def lru_cache(maxsize=100, typed=False, cache=None):
    ...

>From looking at the source, cache would need to support these methods: get, clear, __setitem__, __contains__, __len__, __delitem__

Would this just work? Or could there be a race condition if __contains__ (key in cache) returned True but then cache.get(key) returned False a bit later?

In any case, this seems nice and general to me, and would mean you could implement a simple ExpiringDict() and then pass that as cache=ExpiringDict(expiry_time).

Thoughts?

----------
nosy: +benhoyt

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18577>
_______________________________________


More information about the Python-bugs-list mailing list