functools.lru_cache with TTL support and async compatibility.
Project description
ttlru
functools.lru_cache with TTL support and async compatibility — a single decorator for both sync and async functions.
Install
pip install ttlru
Usage
from ttlru import ttl_cache
# Works for both sync and async — TTL=-1 means never expire (default)
@ttl_cache(ttlru_ttl=60, ttlru_maxsize=256)
def fetch_user(user_id: int):
...
@ttl_cache(ttlru_ttl=30)
async def fetch_remote(url: str):
...
# No parens — uses defaults (no expiry, maxsize=128)
@ttl_cache
def compute(x):
...
# Cache management (mirrors functools.lru_cache)
fetch_user.cache_clear()
fetch_user.cache_info() # CacheInfo(hits=…, misses=…, maxsize=…, currsize=…)
Parameters
All decorator keyword arguments use the ttlru_ prefix so they never clash
with parameters of the decorated function (e.g. a function that has its own
ttl or config parameter).
| Parameter | Default | Description |
|---|---|---|
ttlru_ttl |
-1 |
Seconds before entry expires. -1 = never expire. |
ttlru_maxsize |
128 |
Max entries. None = unlimited, 0 = no caching. |
ttlru_typed |
False |
Cache f(3) and f(3.0) separately when True. |
ttlru_config |
global | CacheConfig instance for enabled flag and telemetry. |
Telemetry
from ttlru import set_telemetry_provider, MetricsTelemetryProvider
set_telemetry_provider(MetricsTelemetryProvider(my_otel_counter))
Built-in providers: LoggingTelemetryProvider (default, DEBUG level), MetricsTelemetryProvider, NoOpTelemetryProvider.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ttlru-0.1.0.tar.gz.
File metadata
- Download URL: ttlru-0.1.0.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.3.3 CPython/3.12.13 Darwin/25.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
db5af2b6583ace267aa799c3c1abb2b5f4a44ba3189fc587b26cba52270b80d6
|
|
| MD5 |
31177327543334ece47b2110a6776a28
|
|
| BLAKE2b-256 |
526df83f1cdf4faa416bea483be6ab81c7330004e2445eba06f7375a399322dc
|
File details
Details for the file ttlru-0.1.0-py3-none-any.whl.
File metadata
- Download URL: ttlru-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.3.3 CPython/3.12.13 Darwin/25.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1308381e603f1c90e73c1c1fa5847fa73304074e812e3e33dcd02817109bb146
|
|
| MD5 |
9abce36fa7e59b80d49d7ff651d54895
|
|
| BLAKE2b-256 |
f2c315366ff6ff9c97ffa2f1109340f61b663396fa09df2f66d8cb165c2b7410
|