caching
caching
¶
Intermediate result caching for perfusion analysis.
This module provides configurable caching for intermediate computation results like T1 maps and concentration curves.
RetentionPolicy
¶
Bases: Enum
Retention policy for intermediate results.
| ATTRIBUTE | DESCRIPTION |
|---|---|
TRANSIENT |
Results are discarded after use. Minimal memory footprint.
TYPE:
|
CACHED |
Results are cached in memory during session. Cleared when session ends.
TYPE:
|
PERSISTENT |
Results are saved to disk and survive between sessions.
TYPE:
|
CacheConfig
dataclass
¶
CacheConfig(
default_policy=CACHED,
policies=dict(),
cache_dir=None,
max_memory_mb=1024,
max_age_seconds=3600,
compression=True,
)
Configuration for intermediate result caching.
| ATTRIBUTE | DESCRIPTION |
|---|---|
default_policy |
Default retention policy for intermediate results.
TYPE:
|
policies |
Per-type retention policies (overrides default).
TYPE:
|
cache_dir |
Directory for persistent cache. Uses temp dir if None.
TYPE:
|
max_memory_mb |
Maximum memory for cached results in MB.
TYPE:
|
max_age_seconds |
Maximum age for cached results (0 = no limit).
TYPE:
|
compression |
Whether to compress persistent cache files.
TYPE:
|
CacheEntry
dataclass
¶
Single cache entry.
| ATTRIBUTE | DESCRIPTION |
|---|---|
key |
Unique identifier for this entry.
TYPE:
|
data |
Cached data (numpy array or dict).
TYPE:
|
created_at |
Timestamp when entry was created.
TYPE:
|
size_bytes |
Approximate size of cached data in bytes.
TYPE:
|
result_type |
Type of intermediate result (e.g., "t1_map", "concentration").
TYPE:
|
metadata |
Additional metadata about the cached result.
TYPE:
|
IntermediateCache
¶
Cache for intermediate computation results.
Provides memory and disk caching with configurable retention policies per result type.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
Cache configuration.
TYPE:
|
Examples:
>>> from osipy.common.caching import IntermediateCache, CacheConfig, RetentionPolicy
>>> config = CacheConfig(
... default_policy=RetentionPolicy.CACHED,
... policies={"t1_map": RetentionPolicy.PERSISTENT},
... )
>>> cache = IntermediateCache(config)
>>> cache.put("t1_map", "session1_t1", t1_data)
>>> t1 = cache.get("t1_map", "session1_t1")
get_policy
¶
Get retention policy for a result type.
| PARAMETER | DESCRIPTION |
|---|---|
result_type
|
Type of intermediate result.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RetentionPolicy
|
Applicable retention policy. |
put
¶
Store intermediate result.
| PARAMETER | DESCRIPTION |
|---|---|
result_type
|
Type of result (e.g., "t1_map", "concentration").
TYPE:
|
key
|
Unique key for this result.
TYPE:
|
data
|
Data to cache.
TYPE:
|
metadata
|
Additional metadata.
TYPE:
|
get
¶
Retrieve intermediate result.
| PARAMETER | DESCRIPTION |
|---|---|
result_type
|
Type of result.
TYPE:
|
key
|
Unique key.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray | dict | None
|
Cached data or None if not found. |
has
¶
Check if result is cached.
| PARAMETER | DESCRIPTION |
|---|---|
result_type
|
Type of result.
TYPE:
|
key
|
Unique key.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if result is cached and valid. |
invalidate
¶
Invalidate (remove) cached result.
| PARAMETER | DESCRIPTION |
|---|---|
result_type
|
Type of result.
TYPE:
|
key
|
Unique key.
TYPE:
|
clear
¶
Clear cached results.
| PARAMETER | DESCRIPTION |
|---|---|
result_type
|
If provided, only clear results of this type. If None, clear all results.
TYPE:
|
get_stats
¶
Get cache statistics.
| RETURNS | DESCRIPTION |
|---|---|
dict
|
Cache statistics including size, count, hit rate. |
get_cache
¶
Get global cache instance.
| RETURNS | DESCRIPTION |
|---|---|
IntermediateCache
|
Global cache instance. |
configure_cache
¶
Configure global cache.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
Cache configuration.
TYPE:
|