Caching
provide.foundation.utils.caching
¶
Caching utilities for Foundation.
Provides efficient caching mechanisms for frequently accessed data with configurable size limits and optional TTL support.
Classes¶
LRUCache
¶
Thread-safe LRU cache with configurable size.
This is a simple LRU cache that maintains insertion order and evicts least recently used items when the cache is full.
Initialize LRU cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
maxsize
|
int
|
Maximum number of items to cache |
128
|
Source code in provide/foundation/utils/caching.py
Functions¶
clear
¶
get
¶
Get value from cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
Cache key |
required |
default
|
Any
|
Default value if key not found |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Cached value or default |
Source code in provide/foundation/utils/caching.py
set
¶
Set value in cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
Cache key |
required |
value
|
Any
|
Value to cache |
required |
Source code in provide/foundation/utils/caching.py
stats
¶
Get cache statistics.
Returns:
| Type | Description |
|---|---|
dict[str, int | float]
|
Dictionary with hits, misses, size, maxsize, and hit_rate |
Source code in provide/foundation/utils/caching.py
Functions¶
cached
¶
Decorator to cache function results with LRU eviction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
maxsize
|
int
|
Maximum number of cached results |
128
|
enabled
|
bool | None
|
Whether caching is enabled (defaults to FOUNDATION_CACHE_ENABLED) |
None
|
Returns:
| Type | Description |
|---|---|
Callable
|
Decorated function with caching |
Example
@cached(maxsize=100) ... def expensive_operation(x: int) -> int: ... return x * x
Source code in provide/foundation/utils/caching.py
clear_all_caches
¶
Clear all registered caches.
Useful for testing and cache invalidation.