Cache Service¶
cache_service ¶
Cache Service - Application-Level Caching with Hash-Based Keys.
Provides caching functionality for expensive operations like database merges. Uses hash-based keys for cache invalidation and supports multiple data types (DataFrame, Dataset, MergedDataDTO).
Classes:
| Name | Description |
|---|---|
CacheService | Manages application-level caching with TTL and size limits |
Classes¶
CacheService ¶
Manage application-level caching with hash-based keys.
Provides caching for expensive operations with automatic expiration, size limits, and hash-based key generation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_size | int | Maximum number of cached items | 100 |
default_ttl_seconds | int | Default time-to-live in seconds (1 hour) | 3600 |
Attributes:
| Name | Type | Description |
|---|---|---|
_cache | Dict[str, Dict[str, Any]] | Internal cache storage |
_max_size | int | Maximum cache size |
_default_ttl | int | Default TTL |
Methods:
| Name | Description |
|---|---|
set | Store value in cache |
get | Retrieve value from cache |
delete | Remove value from cache |
clear | Clear entire cache |
generate_hash_key | Generate hash-based cache key |
has | Check if key exists and is valid |
size | Get current cache size |
Notes
Cache entries structure: { 'key': { 'value': cached_value, 'timestamp': creation_time, 'ttl': time_to_live_seconds } }
Uses SHA256 for hash key generation.
Initialize cache service.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_size | int | Maximum cached items | 100 |
default_ttl_seconds | int | Default TTL (1 hour) | 3600 |
Source code in src/application/services/cache_service.py
Functions¶
set ¶
Store value in cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
value | Any | Value to cache | required |
ttl_seconds | Optional[int] | Time-to-live (uses default if None) | None |
Notes
If cache is full, removes oldest entry (FIFO eviction).
Source code in src/application/services/cache_service.py
get ¶
Retrieve 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 |
Notes
Automatically removes expired entries.
Source code in src/application/services/cache_service.py
delete ¶
Remove value from cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
Returns:
| Type | Description |
|---|---|
bool | True if key was deleted, False if not found |
Source code in src/application/services/cache_service.py
clear ¶
generate_hash_key ¶
Generate SHA256 hash-based cache key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content | str | Content to hash (e.g., upload content or dataset identifier) | required |
Returns:
| Type | Description |
|---|---|
str | SHA256 hash hexadecimal string |
Notes
- Same content always generates same key (deterministic)
- Compatible with legacy cache key generation
Source code in src/application/services/cache_service.py
has ¶
Check if key exists and is valid (not expired).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
Returns:
| Type | Description |
|---|---|
bool | True if key exists and not expired |
Source code in src/application/services/cache_service.py
size ¶
Get current cache size (number of entries).
Returns:
| Type | Description |
|---|---|
int | Number of cached entries |