cache
mlflow_secrets_auth.cache
¶
TTL cache implementation for secrets.
Provides a lightweight, thread-safe cache with monotonic-clock-based TTLs and a
simple decorator (cached_fetch
) to memoize zero-argument callables.
Design goals
- Monotonic time to avoid issues when the wall clock changes.
- Thread safety via
RLock
. - No caching of failures: exceptions from the wrapped callable return
None
and are not stored. - Global cache instance for convenience, with helpers to clear and inspect size.
TTLCache()
¶
Thread-safe TTL cache (monotonic-clock based).
Initialize an empty TTL cache with thread safety.
Source code in src/mlflow_secrets_auth/cache.py
clear()
¶
delete(key)
¶
Remove a key from the cache.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
Cache key to remove. |
required |
get(key)
¶
Get a value from the cache if present and not expired.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
Cache key. |
required |
Returns:
Type | Description |
---|---|
Any | None
|
The cached value if present and valid, otherwise None. |
Source code in src/mlflow_secrets_auth/cache.py
invalidate_prefix(prefix)
¶
Remove all keys starting with a prefix.
Useful for provider-wide invalidation using e.g. f"{provider_name}:"
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefix
|
str
|
Prefix to match. |
required |
Source code in src/mlflow_secrets_auth/cache.py
set(key, value, ttl_seconds)
¶
Set a value in the cache with a TTL.
Non-positive or sub-minimum TTLs are treated as "no caching" (the key is removed). TTLs larger than MAX_TTL_SECONDS are capped.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
Cache key. |
required |
value
|
Any
|
Value to store. |
required |
ttl_seconds
|
float
|
Time-to-live in seconds. |
required |
Source code in src/mlflow_secrets_auth/cache.py
size()
¶
Return the current cache size, pruning expired entries first.
Returns:
Type | Description |
---|---|
int
|
Number of live (non-expired) entries. |
Source code in src/mlflow_secrets_auth/cache.py
cached_fetch(cache_key, ttl_seconds=DEFAULT_TTL_SECONDS)
¶
Cache a zero-argument function's result with a TTL.
Exceptions raised by the wrapped function are swallowed and result in None
,
which is not cached. Successful non-None results are cached.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cache_key
|
str
|
Unique cache key for the function result. |
required |
ttl_seconds
|
int
|
Time-to-live for the cached value. |
DEFAULT_TTL_SECONDS
|
Returns:
Type | Description |
---|---|
Callable[[Callable[[], T]], Callable[[], T | None]]
|
A decorator that wraps a |
Source code in src/mlflow_secrets_auth/cache.py
clear_cache()
¶
delete_cache_key(key)
¶
Remove a single cache entry by key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
Cache key to remove. |
required |