Author benhoyt
Recipients benhoyt,, rhettinger
Date 2014-09-03.16:05:41
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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).

Date User Action Args
2014-09-03 16:05:42benhoytsetrecipients: + benhoyt, rhettinger,
2014-09-03 16:05:42benhoytsetmessageid: <>
2014-09-03 16:05:42benhoytlinkissue18577 messages
2014-09-03 16:05:41benhoytcreate