generic_cache
¶
General-purpose generic cache with TTL and disk persistence.
GenericCacheService
¶
Generic in-memory cache service with TTL support and automatic cleanup.
Initialize generic cache service.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
AppConfig
|
Typed application configuration |
required |
logger
|
Logger | None
|
Optional logger instance |
None
|
Source code in src/services/cache/generic_cache.py
initialize
async
¶
Initialize generic cache service and start cleanup task.
Source code in src/services/cache/generic_cache.py
get
¶
Get value from generic cache.
Accessing an entry updates its LRU position (moves to end).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_data
|
CacheableKey
|
Cache key data |
required |
Returns:
| Type | Description |
|---|---|
CacheableValue | None
|
Cached value if found and valid, None otherwise |
Source code in src/services/cache/generic_cache.py
set
¶
Store value in generic cache with LRU eviction.
If the cache is at capacity and the key is new, the least recently used entry is evicted before adding the new entry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_data
|
CacheableKey
|
Cache key data |
required |
value
|
CacheableValue
|
Value to cache |
required |
ttl
|
int | None
|
Time to live in seconds (uses default if not specified) |
None
|
Source code in src/services/cache/generic_cache.py
invalidate
¶
Invalidate specific cache entry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_data
|
CacheableKey
|
Cache key data to invalidate |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if entry was found and removed, False otherwise |
Source code in src/services/cache/generic_cache.py
invalidate_all
¶
cleanup_expired
¶
Remove expired entries from cache.
Returns:
| Type | Description |
|---|---|
int
|
Number of entries removed |
Source code in src/services/cache/generic_cache.py
enforce_size_limits
¶
Enforce cache size limits by removing LRU (least recently used) entries.
Uses OrderedDict iteration order where first = oldest (LRU).
Returns:
| Type | Description |
|---|---|
int
|
Number of entries removed |
Source code in src/services/cache/generic_cache.py
stop_cleanup_task
async
¶
Stop the periodic cleanup task.
Source code in src/services/cache/generic_cache.py
__aenter__
async
¶
__aexit__
async
¶
Async context manager exit - ensures cleanup task is stopped.
get_stats
¶
Get generic cache statistics.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing cache statistics |
Source code in src/services/cache/generic_cache.py
save_to_disk
async
¶
Persist cache contents to disk.