Skip to main content

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.

Release files for ttlru 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ttlru 0.1.0
File Size Uploaded
ttlru-0.1.0.tar.gz 6.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ttlru 0.1.0
File Interpreter ABI Platform
ttlru-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.6 kB

Release files / ttlru-0.1.0.tar.gz

Download URL ttlru-0.1.0.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
db5af2b6583ace267aa799c3c1abb2b5f4a44ba3189fc587b26cba52270b80d6
BLAKE2b-256 checksum
How to use checksums
526df83f1cdf4faa416bea483be6ab81c7330004e2445eba06f7375a399322dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.3 CPython/3.12.13 Darwin/25.4.0

Release files / ttlru-0.1.0-py3-none-any.whl

Download URL ttlru-0.1.0-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1308381e603f1c90e73c1c1fa5847fa73304074e812e3e33dcd02817109bb146
BLAKE2b-256 checksum
How to use checksums
f2c315366ff6ff9c97ffa2f1109340f61b663396fa09df2f66d8cb165c2b7410
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.3 CPython/3.12.13 Darwin/25.4.0

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page