Created
July 18, 2022 20:29
-
-
Save kgriffs/ca4359c3b26d7833c56d499ea9164a57 to your computer and use it in GitHub Desktop.
Grant Jenks' LRU Cache with TTL for Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Grant Jenks' LRU Cache with TTL for Python | |
# | |
# https://stackoverflow.com/questions/31771286/python-in-memory-cache-with-time-to-live/71634221#71634221 | |
from functools import lru_cache, wraps | |
from time import monotonic | |
def lru_cache_with_ttl(maxsize=128, typed=False, ttl=60): | |
"""Least-recently used cache with time-to-live (ttl) limit.""" | |
class Result: | |
__slots__ = ('value', 'death') | |
def __init__(self, value, death): | |
self.value = value | |
self.death = death | |
def decorator(func): | |
@lru_cache(maxsize=maxsize, typed=typed) | |
def cached_func(*args, **kwargs): | |
value = func(*args, **kwargs) | |
death = monotonic() + ttl | |
return Result(value, death) | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
result = cached_func(*args, **kwargs) | |
if result.death < monotonic(): | |
result.value = func(*args, **kwargs) | |
result.death = monotonic() + ttl | |
return result.value | |
wrapper.cache_clear = cached_func.cache_clear | |
return wrapper | |
return decorator | |
# Recalculate cached results after 5 seconds. | |
@lru_cache_with_ttl(ttl=5) | |
def expensive_function(a, b): | |
return a + b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credit: Grant Jenks: https://stackoverflow.com/questions/31771286/python-in-memory-cache-with-time-to-live/71634221#71634221