mailgun / expiringdict

Dictionary with auto-expiring values for caching purposes.
Apache License 2.0
344 stars 76 forks source link

Add an option to reset remove timer on access #47

Open ghost opened 3 years ago

ghost commented 3 years ago

For exemple:

from expiringdict import ExpiringDict
import time
cache = ExpiringDict(max_len=10, max_age_seconds=10, refresh_on_access=True)
cache["foo"] = "bar"
time.sleep(7)
print(cache["foo"]) #Value is accessed, timer is reset for this value
time.sleep(7)
print(cache["foo"]) #Value is still there

Alternatively, a function to reset the timer

from expiringdict import ExpiringDict, ResetTimer
import time
cache = ExpiringDict(max_len=10, max_age_seconds=10)
cache["foo"] = "bar"
time.sleep(7)
ResetTimer(cache, "foo") #Timer is reset for this value
time.sleep(7)
print(cache["foo"]) #Value is still there
ghost commented 3 years ago

Actually this code works:

cache["test"] = "desuwa"
time.sleep(7)
cache["test"] = cache["test"]
time.sleep(7)
print(cache['test'])

So the second example is already possible

It would be nice to be able to do this on access automatically though, like in the first example