cache_manager¶
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
cache = CachedSettingsstr cache.add("first") cache.add("second") cache.get_all() ['second', 'first']
add ¶
add(value: T) -> None
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_manager.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
get_all ¶
get_all() -> list[T]
Get all cached values.
Returns:
| Type | Description |
|---|---|
list[T]
|
List of all cached values, newest first |
Source code in src/clabe/cache_manager.py
63 64 65 66 67 68 69 70 | |
get_latest ¶
get_latest() -> T | None
Get the most recently added value.
Returns:
| Type | Description |
|---|---|
T | None
|
The latest value, or None if cache is empty |
Source code in src/clabe/cache_manager.py
72 73 74 75 76 77 78 79 | |
clear ¶
clear() -> None
Clear all values from the cache.
Source code in src/clabe/cache_manager.py
81 82 83 | |
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_manager.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
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_manager.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
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_manager.py
184 185 186 187 188 189 190 191 192 193 194 195 | |
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_manager.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
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_manager.py
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |
try_get_cache ¶
Attempt to get all values from a named cache, returning None if not found.
Source code in src/clabe/cache_manager.py
243 244 245 246 247 248 | |
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_manager.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | |
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_manager.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
clear_all_caches ¶
clear_all_caches() -> None
Clear all caches (thread-safe).
Source code in src/clabe/cache_manager.py
283 284 285 286 287 | |
save ¶
save() -> None
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.
Source code in src/clabe/cache_manager.py
289 290 291 292 293 294 295 296 297 | |