Data Cache#

Data caching functionality for MDA files.

This module provides caching capabilities to improve performance when loading and processing MDA files.

DataCache(*args, **kwargs)

LRU cache for MDA file data to improve performance and manage memory usage.

CachedFileData(file_path, metadata, ...)

Cached file data with metadata.

get_global_cache()

Get the global data cache instance.

class mdaviz.data_cache.CachedFileData(file_path: str, metadata: dict[str, ~typing.Any], scan_dict: dict[str, ~typing.Any], first_pos: int, first_det: int, pv_list: list, file_name: str, folder_path: str, access_time: float = <factory>, size_bytes: int = 0)[source]#

Cached file data with metadata.

get_size_mb() float[source]#

Get the size in megabytes.

update_access_time() None[source]#

Update the last access time.

class mdaviz.data_cache.DataCache(*args: Any, **kwargs: Any)[source]#

LRU cache for MDA file data to improve performance and manage memory usage.

This cache stores loaded MDA file data in memory and automatically evicts least recently used entries when memory limits are exceeded.

Attributes:

max_size_mb (float): Maximum cache size in megabytes max_entries (int): Maximum number of cached entries enable_compression (bool): Whether to compress cached data max_memory_mb (float): Maximum system memory usage in megabytes

_check_memory_usage() float[source]#

Check current memory usage and trigger cleanup if needed.

Returns:

float: Current memory usage in MB

_evict_lru() bool[source]#

Evict the least recently used entry from the cache.

Returns:

bool: True if an entry was evicted, False if cache is empty

_load_without_caching(path_obj: Path) CachedFileData | None[source]#

Load file data without caching it (for large files or high memory usage).

Parameters:

path_obj (Path): Path object to the file

Returns:

CachedFileData or None: Loaded data (not cached)

_perform_memory_cleanup() None[source]#

Perform aggressive memory cleanup when usage is high.

cache_eviction#

alias of str

cache_hit#

alias of str

cache_miss#

alias of str

clear() None[source]#

Clear all cached data.

get(file_path: str) CachedFileData | None[source]#

Get cached data for a file.

Parameters:

file_path (str): Path to the file

Returns:

CachedFileData or None: Cached data if available, None otherwise

get_or_load(file_path: str) CachedFileData | None[source]#

Get cached data or load and cache it if not available.

Parameters:

file_path (str): Path to the file

Returns:

CachedFileData or None: Data from cache or newly loaded

get_stats() dict[str, Any][source]#

Get cache statistics.

Returns:

dict: Cache statistics including size, entry count, hit rate, etc.

load_and_cache(file_path: str) CachedFileData | None[source]#

Load file data and cache it.

Parameters:

file_path (str): Path to the file to load

Returns:

CachedFileData or None: Loaded and cached data

memory_warning#

alias of float

put(file_path: str, cached_data: CachedFileData) None[source]#

Store data in the cache.

Parameters:

file_path (str): Path to the file cached_data (CachedFileData): Data to cache

remove(file_path: str) bool[source]#

Remove a file from the cache.

Parameters:

file_path (str): Path to the file to remove

Returns:

bool: True if the file was in the cache, False otherwise

set_max_entries(max_entries: int) None[source]#

Set the maximum number of cache entries.

Parameters:

max_entries (int): New maximum number of entries

set_max_size_mb(max_size_mb: float) None[source]#

Set the maximum cache size.

Parameters:

max_size_mb (float): New maximum size in megabytes

mdaviz.data_cache.get_global_cache() DataCache[source]#

Get the global data cache instance.

Returns:

DataCache: Global cache instance

mdaviz.data_cache.set_global_cache(cache: DataCache) None[source]#

Set the global data cache instance.

Parameters:

cache (DataCache): Cache instance to use globally