cachettl

raw JSON →
1.0.4 verified Fri May 01 auth: no python

cachettl is an elegant LRU TTL cache decorator for Python, supporting both synchronous and asyncio functions. Version 1.0.4 (latest) requires Python >=3.7. Provides cache_info(), cache_clear(), and a remainingttl property. Development appears stable with infrequent releases.

pip install cachettl
error ModuleNotFoundError: No module named 'cachettl'
cause Package not installed or installed under a different name.
fix
Run 'pip install cachettl' to install the package.
error TypeError: __init__() got an unexpected keyword argument 'timeout'
cause Using a non-existent keyword argument 'timeout' instead of 'ttl'.
fix
Replace 'timeout' with 'ttl' in the decorator call.
gotcha The decorator name is cachettl (all lowercase), not cache_ttl or CacheTTL. Importing from cachettl import cachettl is required.
fix Use 'from cachettl import cachettl' and decorate with @cachettl(...).
gotcha The decorator does not support keyword-only arguments like 'cache' or 'timeout'. Only positional parameters: maxsize, ttl, and maxage.
fix Use @cachettl(maxsize=128, ttl=60) only. Do not pass other unexpected kwargs.

Basic usage of cachettl decorator with maxsize and ttl.

from cachettl import cachettl
import time

@cachettl(maxsize=128, ttl=60)
def expensive_function(n):
    return n * n

print(expensive_function(5))  # Computes and caches
print(expensive_function(5))  # Returns cached result
print(expensive_function.cache_info())  # e.g., hits=1, misses=1
print(expensive_function.remainingttl())  # seconds left
time.sleep(61)
print(expensive_function(5))  # Recomputes after TTL expiry