clabe.cache ¶
CachedSettings ¶
Bases: BaseModel, Generic[T]
Manages a cache of values with a configurable history limit.
When a new value is added and the cache is full, the oldest value is removed.
Attributes:
| Name | Type | Description |
|---|---|---|
values |
list[T]
|
List of cached values, newest first |
max_history |
int
|
Maximum number of items to retain in cache |
Example
add ¶
Add a new value to the cache.
If the value already exists, it's moved to the front. If the cache is full, the oldest value is removed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
T
|
The value to add to the cache |
required |
Source code in src/clabe/cache.py
get_latest ¶
Get the most recently added value.
Returns:
| Type | Description |
|---|---|
T | None
|
The latest value, or None if cache is empty |
CacheData ¶
Bases: BaseModel
Pydantic model for cache serialization.
CacheManager ¶
CacheManager(
cache_path: Path | str | None = None,
sync_strategy: SyncStrategy = AUTO,
)
Thread-safe singleton cache manager with multiple named caches.
Uses Pydantic for proper serialization/deserialization with automatic disk synchronization support. All operations are thread-safe.
Example
Get singleton instance with manual sync (default)¶
manager = CacheManager.get_instance() manager.add_to_cache("subjects", "mouse_001") manager.save() # Explicitly save
Get instance with auto sync - saves after every change¶
manager = CacheManager.get_instance(sync_strategy=SyncStrategy.AUTO) manager.add_to_cache("subjects", "mouse_002") # Automatically saved
Custom path¶
manager = CacheManager.get_instance(cache_path="custom/cache.json")
Initialize a CacheManager instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_path
|
Path | str | None
|
Path to cache file. If None, uses default location. |
None
|
sync_strategy
|
SyncStrategy
|
Strategy for syncing to disk (MANUAL or AUTO) |
AUTO
|
Source code in src/clabe/cache.py
get_instance
classmethod
¶
get_instance(
cache_path: Path | str | None = None,
sync_strategy: SyncStrategy = AUTO,
reset: bool = False,
) -> CacheManager
Get the singleton instance of CacheManager (thread-safe).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_path
|
Path | str | None
|
Path to cache file. If None, uses default location. |
None
|
sync_strategy
|
SyncStrategy
|
Strategy for syncing to disk (MANUAL or AUTO) |
AUTO
|
reset
|
bool
|
If True, reset the singleton and create a new instance |
False
|
Returns:
| Type | Description |
|---|---|
CacheManager
|
The singleton CacheManager instance |
Source code in src/clabe/cache.py
register_cache ¶
Register a new cache with a specific history limit (thread-safe).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Unique name for the cache |
required |
max_history
|
int
|
Maximum number of items to retain |
_DEFAULT_MAX_HISTORY
|
Source code in src/clabe/cache.py
add_to_cache ¶
Add a value to a named cache (thread-safe).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the cache |
required |
value
|
Any
|
Value to add |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If cache name is not registered |
Source code in src/clabe/cache.py
get_cache ¶
Get all values from a named cache (thread-safe).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the cache |
required |
Returns:
| Type | Description |
|---|---|
list[Any]
|
List of cached values, newest first |
Raises:
| Type | Description |
|---|---|
KeyError
|
If cache name is not registered |
Source code in src/clabe/cache.py
try_get_cache ¶
Attempt to get all values from a named cache, returning None if not found.
get_latest ¶
Get the most recent value from a named cache (thread-safe).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the cache |
required |
Returns:
| Type | Description |
|---|---|
Any | None
|
The latest value, or None if cache is empty |
Raises:
| Type | Description |
|---|---|
KeyError
|
If cache name is not registered |
Source code in src/clabe/cache.py
clear_cache ¶
clear_cache(name: str) -> None
Clear all values from a named cache (thread-safe).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the cache |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If cache name is not registered |
Source code in src/clabe/cache.py
clear_all_caches ¶
save ¶
Save all caches to disk using Pydantic serialization (thread-safe).
This method is called automatically if sync_strategy is AUTO, or can be called manually for MANUAL strategy.