Documentation 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.
AsyncCache
An asynchronous, thread-safe cache supporting LFU (Least Frequently Used) and LRU (Least Recently Used) eviction policies with an optional per-entry Time-to-Live. All item access methods are coroutines and internally guard state with anRLock, making the cache safe for use across concurrent async tasks.
Maximum number of entries the cache will hold before evicting.
Time-to-Live in seconds. When set, expired entries are treated as cache misses. Pass
None to disable expiry.Eviction policy. Accepted values:
"LFU" or "LRU".ValueError— ifpolicyis not"LFU"or"LRU".
AsyncCache.__getitem__
Retrieves a cached value by key. Non-hashable keys are automatically wrapped in aHashableWrapper.
Cache key. Need not be natively hashable.
Any).
Raises:
KeyError— if the key is not present or has expired.
AsyncCache.__setitem__
Stores a value under the given key. If TTL is configured, the expiry timestamp is recorded in nanoseconds. Non-hashable keys are automatically wrapped.Cache key.
Value to cache.
None
AsyncCache.update_profiling
Records one function execution time for statistical purposes. Called automatically by theasync_cache decorator after each cache miss.
Execution time of the wrapped function in nanoseconds.
None
AsyncCache.get_stats
Returns a snapshot of cache performance statistics. Returns:dict with the following string keys:
| Key | Type | Description |
|---|---|---|
hits | int | Number of cache hits |
misses | int | Number of cache misses |
hit_miss_ratio | str | Ratio formatted to 4 decimal places |
max_time | str | Longest observed execution, formatted as "X.XXXX s" |
min_time | str | Shortest observed execution, formatted as "X.XXXX s" |
avg_time | str | Average execution time, formatted as "X.XXXX s" |
total_calls | int | Total number of decorated function calls |
current_size | int | Current number of entries in the cache |
max_size | int | Maximum capacity (maxsize) |
ttl_sec | str | TTL as "X.XXXX" or "None" |
AsyncCache.clear
Evicts all entries and resets all statistics counters to zero. Returns:None
async_cache
Decorator that applies anAsyncCache to an async function. On each call, a cache key is derived from the arguments and the result is returned from cache when available, skipping function execution.
Maximum cache size. When omitted,
AsyncCache uses its own default.Time-to-Live in seconds. Forwarded to
AsyncCache.Eviction policy:
"LFU" or "LRU". Forwarded to AsyncCache.cache_instance attribute (an AsyncCache) for direct inspection or clearing.
Cache key strategy:
- No positional args → key based on
frozenset(kwargs.items()). - First arg looks like a class instance → key uses
id(args[0])plus remaining args (bound method pattern). - Otherwise → key uses all positional args and kwargs.
make_hashable.
Internal cache backends
These classes are internal implementation details and are not part of the public API. They are documented here for completeness._LRUCache
A simple LRU (Least Recently Used) eviction cache backed by adict and a deque. O(n) removal on access due to deque.remove.
| Method | Description |
|---|---|
__getitem__(key) | Returns value and promotes key to MRU position. Raises KeyError if absent. |
__setitem__(key, value) | Inserts or updates. Evicts oldest entry when at capacity. |
__len__() | Current number of cached entries. |
__contains__(key) | Membership test. |
clear() | Empties cache and order tracking. |
_LFUCache
A simple LFU (Least Frequently Used) eviction cache backed by adict for values, a defaultdict for frequency counts, and a defaultdict(deque) for frequency buckets.
| Method | Description |
|---|---|
__getitem__(key) | Returns value and increments frequency. Raises KeyError if absent. |
__setitem__(key, value) | Inserts or updates. Evicts the least-frequently-used entry when at capacity. |
__len__() | Current number of cached entries. |
__contains__(key) | Membership test. |
clear() | Empties all internal structures and resets min_freq. |
