10

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.

5
  • Do you have a example of what a expiry hash table is? Have your implemented this in another language? Nov 10, 2018 at 22:34
  • Nope...I was just wondering what a feasible logic might be
    – kir bak
    Nov 10, 2018 at 22:55
  • What happens when they "expire"?
    – gilch
    Nov 10, 2018 at 23:07
  • 1
    You may want to check out cachetools which has a TTL cache that seems to do what your describing.
    – Michael
    Nov 11, 2018 at 0:57
  • 1
    Possible duplicate of Python in-memory cache with time to live
    – Louis
    Aug 30, 2019 at 15:43

1 Answer 1

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)
1
  • 1
    can 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, 2022 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.