TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/alex-ber/AlexBerUtils/llms.txt
Use this file to discover all available pages before exploring further.
cache module provides AsyncCache — a thread-safe, async-native cache — and the @async_cache decorator that wraps any coroutine function with automatic caching.
Installation
async_cache decorator
The simplest way to add caching is with the @async_cache decorator.
fetch_user(42) twice within 60 seconds returns the cached result on the second call.
Accessing the cache instance
The decorator attaches the underlyingAsyncCache instance to the wrapped function:
Caching class methods
The decorator detects bound methods viainspect and keys the cache per instance using id(self), so two instances never share cached results:
Maximum number of entries to hold in the cache. When the cache is full, the
least-recently-used (LRU) or least-frequently-used (LFU) entry is evicted. Required when using
AsyncCache directly; optional in @async_cache.Eviction policy. Accepts
"LRU" (Least Recently Used) or "LFU" (Least
Frequently Used). LFU keeps the entries called most often; LRU keeps the
entries called most recently.Time-to-Live in seconds. After this many seconds an entry is treated as
expired and the next access triggers a fresh call.
None disables expiry.AsyncCache class
Use AsyncCache directly when you need fine-grained control — for example, to share one cache across multiple functions or to call clear() programmatically.
Constructor parameters
Maximum number of entries the cache will hold before evicting.
Eviction policy:
"LRU" or "LFU".Entry lifetime in seconds.
None means entries never expire.Methods
| Method | Description |
|---|---|
await cache[key] | Return cached value. Raises KeyError on miss or expiry. |
await cache.__setitem__(key, value) | Store a value. Sets expiry if TTL is configured. |
await cache.update_profiling(exec_time_ns) | Record an execution time sample (nanoseconds). |
await cache.get_stats() | Return a dict of hit/miss/timing statistics (see below). |
await cache.clear() | Evict all entries and reset all statistics. |
Statistics returned by get_stats()
| Key | Type | Description |
|---|---|---|
hits | int | Number of successful cache lookups. |
misses | int | Number of cache misses. |
hit_miss_ratio | str | hits / (hits + misses), formatted to 4 decimal places. |
avg_time | str | Average execution time across all recorded calls. |
max_time | str | Longest recorded execution time. |
min_time | str | Shortest recorded execution time. |
total_calls | int | Number of calls passed to update_profiling. |
current_size | int | Number of entries currently in the cache. |
max_size | int | Configured maxsize. |
ttl_sec | str | Configured TTL (or "None"). |
Eviction policies
LRU — Least Recently Used
Evicts the entry that was accessed least recently. Good when access
patterns are temporal — recently requested data is likely to be requested
again soon.
LFU — Least Frequently Used
Evicts the entry with the lowest access count. Good when some data is
accessed far more often than the rest and should be protected from eviction.
Cache key internals
The cache automatically converts unhashable argument values (lists, dicts, custom objects) into stable cache keys using two helpers frommains.py.
make_hashable(obj)
Recursively converts an object to a hashable type:
- Mappings →
frozensetof(key, value)pairs - Iterables (excluding
str/bytes) →tuple - Natively hashable objects → returned as-is
- Everything else → wrapped in
HashableWrapper
HashableWrapper
A fallback wrapper for objects that are inherently non-hashable. It derives its hash from str(obj) and compares by identity of the wrapped value.
is_iterable(value) and is_mapping(value)
Two guard utilities used by make_hashable internally:
is_iterable explicitly excludes str and bytes so that string arguments
are treated as atomic cache key components rather than sequences of
characters.