Cache Layer¶
The Cache Layer provides high-performance in-memory caching implementations optimized for different data types.
Cache Implementations¶
MemoryCache¶
MemoryCache ¶
In-memory LRU cache implementation.
Provides LRU eviction policy, TTL support, max size enforcement, and basic thread-safe operations.
Attributes:
| Name | Type | Description |
|---|---|---|
max_size | int | Maximum number of entries in cache |
default_ttl | int | Default TTL in seconds (0 = no expiration) |
_cache | OrderedDict | Ordered dictionary storing cached values |
_expiry | dict[str, Optional[datetime]] | Expiry timestamps for cache entries |
Methods:
| Name | Description |
|---|---|
get | Get value from cache |
set | Set value in cache |
delete | Delete entry from cache |
clear | Clear all cache entries |
exists | Check if key exists in cache |
size | Get current cache size |
get_stats | Get cache statistics |
Initialize memory cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_size | int | Maximum number of cache entries. | 1000 |
default_ttl | int | Default TTL in seconds (0 = no expiration). | 0 |
Source code in src/infrastructure/cache/memory_cache.py
Functions¶
get ¶
Get value from cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
Returns:
| Type | Description |
|---|---|
Optional[Any] | Cached value or None if not found/expired |
Source code in src/infrastructure/cache/memory_cache.py
set ¶
Set value in cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
value | Any | Value to cache | required |
ttl | Optional[int] | TTL in seconds (if None, uses default_ttl) | None |
Source code in src/infrastructure/cache/memory_cache.py
delete ¶
Delete entry from cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
Returns:
| Type | Description |
|---|---|
bool | True if deleted, False if key not found |
Source code in src/infrastructure/cache/memory_cache.py
clear ¶
Clear all cache entries.
Source code in src/infrastructure/cache/memory_cache.py
exists ¶
Check if key exists in cache (and not expired).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
Returns:
| Type | Description |
|---|---|
bool | True if exists and not expired |
Source code in src/infrastructure/cache/memory_cache.py
size ¶
Get current cache size.
Returns:
| Type | Description |
|---|---|
int | Number of entries in cache |
get_stats ¶
Get cache statistics.
Returns:
| Type | Description |
|---|---|
dict | Statistics including size, max_size, usage percentage |
Source code in src/infrastructure/cache/memory_cache.py
DataFrameCache¶
DataFrameCache ¶
DataFrameCache(max_size: int = 50, default_ttl: int = 3600, compress_threshold: int = 1024 * 1024, compression_level: int = 6)
Bases: MemoryCache
Specialized cache for pandas DataFrames.
Provides compression (gzip + pickle), DataFrame-specific hash generation, memory usage tracking, and automatic compression for large DataFrames.
Attributes:
| Name | Type | Description |
|---|---|---|
compress_threshold | int | DataFrame size (bytes) above which compression is used |
compression_level | int | Gzip compression level (1-9) |
Initialize DataFrame cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_size | int | Maximum number of DataFrames to cache. | 50 |
default_ttl | int | Default TTL in seconds. | 3600 |
compress_threshold | int | Size threshold (bytes) for compression. | 1048576 |
compression_level | int | Gzip compression level (1-9). | 6 |
Source code in src/infrastructure/cache/dataframe_cache.py
Functions¶
get ¶
Get value from cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
Returns:
| Type | Description |
|---|---|
Optional[Any] | Cached value or None if not found/expired |
Source code in src/infrastructure/cache/memory_cache.py
set ¶
Set value in cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
value | Any | Value to cache | required |
ttl | Optional[int] | TTL in seconds (if None, uses default_ttl) | None |
Source code in src/infrastructure/cache/memory_cache.py
delete ¶
Delete entry from cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
Returns:
| Type | Description |
|---|---|
bool | True if deleted, False if key not found |
Source code in src/infrastructure/cache/memory_cache.py
clear ¶
Clear all cache entries.
Source code in src/infrastructure/cache/memory_cache.py
exists ¶
Check if key exists in cache (and not expired).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
Returns:
| Type | Description |
|---|---|
bool | True if exists and not expired |
Source code in src/infrastructure/cache/memory_cache.py
size ¶
Get current cache size.
Returns:
| Type | Description |
|---|---|
int | Number of entries in cache |
get_stats ¶
Get cache statistics.
Returns:
| Type | Description |
|---|---|
dict | Statistics including size, max_size, usage percentage |
Source code in src/infrastructure/cache/memory_cache.py
cache_dataframe ¶
Cache a DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
df | DataFrame | DataFrame to cache | required |
ttl | Optional[int] | TTL in seconds | None |
Source code in src/infrastructure/cache/dataframe_cache.py
get_cached_dataframe ¶
Retrieve cached DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
Returns:
| Type | Description |
|---|---|
Optional[DataFrame] | Cached DataFrame or None if not found |
Source code in src/infrastructure/cache/dataframe_cache.py
generate_dataframe_key ¶
Generate unique cache key for DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df | DataFrame | DataFrame to generate key for | required |
prefix | str | Optional prefix for key | '' |
Returns:
| Type | Description |
|---|---|
str | Cache key based on DataFrame content hash |
Notes
- Key based on columns, shape, and sample data
Source code in src/infrastructure/cache/dataframe_cache.py
GraphCache¶
GraphCache ¶
Bases: MemoryCache
Specialized cache for Plotly graph objects.
Provides JSON serialization for Plotly figures, graph-specific hash generation, and efficient storage of figure configurations.
Attributes:
| Name | Type | Description |
|---|---|---|
max_size | int | Maximum number of graphs to cache |
default_ttl | int | Default TTL in seconds |
Initialize graph cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_size | int | Maximum number of figures to cache. | 100 |
default_ttl | int | Default TTL in seconds (30 minutes). | 1800 |
Source code in src/infrastructure/cache/graph_cache.py
Functions¶
get ¶
Get value from cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
Returns:
| Type | Description |
|---|---|
Optional[Any] | Cached value or None if not found/expired |
Source code in src/infrastructure/cache/memory_cache.py
set ¶
Set value in cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
value | Any | Value to cache | required |
ttl | Optional[int] | TTL in seconds (if None, uses default_ttl) | None |
Source code in src/infrastructure/cache/memory_cache.py
delete ¶
Delete entry from cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
Returns:
| Type | Description |
|---|---|
bool | True if deleted, False if key not found |
Source code in src/infrastructure/cache/memory_cache.py
clear ¶
Clear all cache entries.
Source code in src/infrastructure/cache/memory_cache.py
exists ¶
Check if key exists in cache (and not expired).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
Returns:
| Type | Description |
|---|---|
bool | True if exists and not expired |
Source code in src/infrastructure/cache/memory_cache.py
size ¶
Get current cache size.
Returns:
| Type | Description |
|---|---|
int | Number of entries in cache |
get_stats ¶
Get cache statistics.
Returns:
| Type | Description |
|---|---|
dict | Statistics including size, max_size, usage percentage |
Source code in src/infrastructure/cache/memory_cache.py
cache_figure ¶
Cache a Plotly figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
figure | Figure | Plotly figure to cache | required |
ttl | Optional[int] | TTL in seconds | None |
Source code in src/infrastructure/cache/graph_cache.py
get_cached_figure ¶
Retrieve cached Plotly figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
Returns:
| Type | Description |
|---|---|
Optional[Figure] | Cached figure or None if not found |
Source code in src/infrastructure/cache/graph_cache.py
generate_figure_key ¶
Generate unique cache key for figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
analysis_id | str | Analysis ID (e.g., 'UC1_1') | required |
filter_values | dict | Filter values used to generate figure | required |
config | Optional[dict] | Additional configuration parameters | None |
Returns:
| Type | Description |
|---|---|
str | Cache key |
Source code in src/infrastructure/cache/graph_cache.py
GraphCacheManager¶
GraphCacheManager ¶
Manager for graph caching operations.
Wraps GraphCache with additional management capabilities and metadata support.
Attributes:
| Name | Type | Description |
|---|---|---|
cache | GraphCache | Underlying cache instance |
Methods:
| Name | Description |
|---|---|
cache_graph | Cache a Plotly figure |
get_cached_graph | Retrieve cached Plotly figure |
clear | Clear all cached graphs |
get_stats | Get cache statistics |
Initialize graph cache manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_size | int | Maximum number of graphs to cache. | 100 |
default_ttl | int | Default TTL in seconds (1 hour). | 3600 |
Source code in src/infrastructure/cache/graph_cache_manager.py
Functions¶
cache_graph ¶
cache_graph(key: str, figure: Figure, metadata: Optional[dict] = None, ttl: Optional[int] = None) -> None
Cache a Plotly figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
figure | Figure | Plotly figure to cache | required |
metadata | Optional[dict] | Optional metadata (for logging/tracking) | None |
ttl | Optional[int] | TTL in seconds (if None, uses default) | None |
Source code in src/infrastructure/cache/graph_cache_manager.py
get_cached_graph ¶
Retrieve cached Plotly figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
Returns:
| Type | Description |
|---|---|
Optional[Figure] | Cached figure or None if not found |
Source code in src/infrastructure/cache/graph_cache_manager.py
clear ¶
get_stats ¶
Get cache statistics.
Returns:
| Type | Description |
|---|---|
dict | Statistics including size, max_size, etc. |