I was just wondering how one might efficiently implement a time expiry dictionary in memory in Python such that key-value pairs expire after a specified time interval.
Asked
Active
Viewed 9,950 times
6
-
Do you have a example of what a expiry hash table is? Have your implemented this in another language? – Red Cricket Nov 10 '18 at 22:34
-
Nope...I was just wondering what a feasible logic might be – kir bak Nov 10 '18 at 22:55
-
What happens when they "expire"? – gilch Nov 10 '18 at 23:07
-
1You may want to check out [cachetools](https://github.com/tkem/cachetools) which has a TTL cache that seems to do what your describing. – Michael Nov 11 '18 at 00:57
-
1Possible duplicate of [Python in-memory cache with time to live](https://stackoverflow.com/questions/31771286/python-in-memory-cache-with-time-to-live) – Louis Aug 30 '19 at 15:43
1 Answers
10
The design pattern to typically do this is not via a dictionary, but via a function or method decorator. The dictionary is managed behind the scenes by the cache.
This answer uses the ttl_cache
decorator in cachetools==3.1.0
with Python 3.7. It works a lot like functools.lru_cache
, but with a time to live. As for its implementation logic, consider its source code.
import cachetools.func
@cachetools.func.ttl_cache(maxsize=128, ttl=10 * 60)
def example_function(key):
return get_expensively_computed_value(key)
class ExampleClass:
EXP = 2
@classmethod
@cachetools.func.ttl_cache()
def example_classmethod(cls, i):
return i**cls.EXP
@staticmethod
@cachetools.func.ttl_cache()
def example_staticmethod(i):
return i**3
If however you insist on using a dictionary, cachetools
also has TTLCache
.
import cachetools
ttl_cache = cachetools.TTLCache(maxsize=128, ttl=10 * 60)

Asclepius
- 57,944
- 17
- 167
- 143
-
1can it set the TTL to all keys together at once in the dict, instead of setting by one key. e.g.: all the content of ttl_cache disappear at once after TTL. – DennisLi Mar 30 '22 at 15:27