Skip to content

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: str

CACHED

Results are cached in memory during session. Cleared when session ends.

TYPE: str

PERSISTENT

Results are saved to disk and survive between sessions.

TYPE: str

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: RetentionPolicy

policies

Per-type retention policies (overrides default).

TYPE: dict[str, RetentionPolicy]

cache_dir

Directory for persistent cache. Uses temp dir if None.

TYPE: Path | None

max_memory_mb

Maximum memory for cached results in MB.

TYPE: int

max_age_seconds

Maximum age for cached results (0 = no limit).

TYPE: int

compression

Whether to compress persistent cache files.

TYPE: bool

CacheEntry dataclass

CacheEntry(
    key,
    data,
    created_at,
    size_bytes,
    result_type,
    metadata=dict(),
)

Single cache entry.

ATTRIBUTE DESCRIPTION
key

Unique identifier for this entry.

TYPE: str

data

Cached data (numpy array or dict).

TYPE: Any

created_at

Timestamp when entry was created.

TYPE: float

size_bytes

Approximate size of cached data in bytes.

TYPE: int

result_type

Type of intermediate result (e.g., "t1_map", "concentration").

TYPE: str

metadata

Additional metadata about the cached result.

TYPE: dict

IntermediateCache

IntermediateCache(config=None)

Cache for intermediate computation results.

Provides memory and disk caching with configurable retention policies per result type.

PARAMETER DESCRIPTION
config

Cache configuration.

TYPE: CacheConfig | None DEFAULT: None

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_policy(result_type)

Get retention policy for a result type.

PARAMETER DESCRIPTION
result_type

Type of intermediate result.

TYPE: str

RETURNS DESCRIPTION
RetentionPolicy

Applicable retention policy.

put

put(result_type, key, data, metadata=None)

Store intermediate result.

PARAMETER DESCRIPTION
result_type

Type of result (e.g., "t1_map", "concentration").

TYPE: str

key

Unique key for this result.

TYPE: str

data

Data to cache.

TYPE: NDArray | dict

metadata

Additional metadata.

TYPE: dict | None DEFAULT: None

get

get(result_type, key)

Retrieve intermediate result.

PARAMETER DESCRIPTION
result_type

Type of result.

TYPE: str

key

Unique key.

TYPE: str

RETURNS DESCRIPTION
NDArray | dict | None

Cached data or None if not found.

has

has(result_type, key)

Check if result is cached.

PARAMETER DESCRIPTION
result_type

Type of result.

TYPE: str

key

Unique key.

TYPE: str

RETURNS DESCRIPTION
bool

True if result is cached and valid.

invalidate

invalidate(result_type, key)

Invalidate (remove) cached result.

PARAMETER DESCRIPTION
result_type

Type of result.

TYPE: str

key

Unique key.

TYPE: str

clear

clear(result_type=None)

Clear cached results.

PARAMETER DESCRIPTION
result_type

If provided, only clear results of this type. If None, clear all results.

TYPE: str | None DEFAULT: None

get_stats

get_stats()

Get cache statistics.

RETURNS DESCRIPTION
dict

Cache statistics including size, count, hit rate.

get_cache

get_cache()

Get global cache instance.

RETURNS DESCRIPTION
IntermediateCache

Global cache instance.

configure_cache

configure_cache(config)

Configure global cache.

PARAMETER DESCRIPTION
config

Cache configuration.

TYPE: CacheConfig