ExLLM.Testing.TestCacheIndex (ex_llm v0.8.1)
View SourceMaintain index of timestamped cache entries with metadata.
This module provides efficient indexing and querying of timestamp-based cache entries, including content deduplication tracking, usage statistics, and cleanup coordination for the test caching system.
Summary
Functions
Add a new cache entry to the index.
Add a new cache entry to the index and save to disk.
Add a new cache entry with options like max_entries limit.
Calculate the hit rate for the index.
Clean up the index and apply retention policies.
Clean up old entries by age.
Find duplicate content using content hashes.
Find duplicate entries based on content hash.
Get entries that match the specified status.
Get entries that match the specified status, with directory path for loading latest state.
Get entry by filename.
Get cache statistics from the index.
Get entries within the specified TTL.
Keep only the specified number of most recent entries.
Load cache index from disk or create a new one.
Rebuild index from existing cache files.
Record a cache access (hit or miss) for statistics.
Record a cache hit for statistics.
Remove a specific entry by filename.
Remove cache entries older than the specified cutoff.
Save cache index to disk.
Update cache statistics.
Types
@type cache_entry() :: %{ timestamp: DateTime.t(), filename: String.t(), status: :success | :error | :timeout, size: non_neg_integer(), content_hash: String.t(), response_time_ms: non_neg_integer(), api_version: String.t() | nil, cost: map() | nil }
@type cache_index() :: %{ cache_key: String.t(), test_context: map() | nil, ttl: non_neg_integer() | :infinity, fallback_strategy: atom(), entries: [cache_entry()], total_requests: non_neg_integer(), cache_hits: non_neg_integer(), last_accessed: DateTime.t() | nil, access_count: non_neg_integer(), last_cleanup: DateTime.t() | nil, cleanup_before: DateTime.t() | nil }
Functions
@spec add_entry(cache_index(), cache_entry()) :: cache_index()
Add a new cache entry to the index.
@spec add_entry(cache_index(), cache_entry(), String.t()) :: cache_index()
Add a new cache entry to the index and save to disk.
@spec add_entry(cache_index(), cache_entry(), String.t(), keyword()) :: cache_index()
Add a new cache entry with options like max_entries limit.
@spec calculate_hit_rate(cache_index()) :: float()
Calculate the hit rate for the index.
Clean up the index and apply retention policies.
@spec cleanup_old_entries(cache_index(), non_neg_integer(), String.t()) :: cache_index()
Clean up old entries by age.
@spec find_duplicate_content(cache_index()) :: %{ required(String.t()) => [cache_entry()] }
Find duplicate content using content hashes.
@spec find_duplicates(cache_index()) :: %{required(String.t()) => [cache_entry()]}
Find duplicate entries based on content hash.
@spec get_entries_by_status(cache_index(), :success | :error | :timeout | :any) :: [ cache_entry() ]
Get entries that match the specified status.
@spec get_entries_by_status( cache_index(), :success | :error | :timeout | :any, String.t() ) :: [ cache_entry() ]
Get entries that match the specified status, with directory path for loading latest state.
@spec get_entry_by_filename(cache_index(), String.t()) :: {:ok, cache_entry()} | :error
Get entry by filename.
@spec get_statistics(cache_index()) :: map()
Get cache statistics from the index.
@spec get_valid_entries(cache_index(), non_neg_integer() | :infinity) :: [ cache_entry() ]
Get entries within the specified TTL.
@spec limit_entries(cache_index(), non_neg_integer()) :: cache_index()
Keep only the specified number of most recent entries.
@spec load_index(String.t()) :: cache_index()
Load cache index from disk or create a new one.
@spec rebuild_index(String.t()) :: cache_index()
Rebuild index from existing cache files.
@spec record_access(cache_index()) :: cache_index()
Record a cache access (hit or miss) for statistics.
@spec record_hit(cache_index()) :: cache_index()
Record a cache hit for statistics.
@spec remove_entry(cache_index(), String.t(), String.t()) :: cache_index()
Remove a specific entry by filename.
@spec remove_old_entries(cache_index(), DateTime.t()) :: cache_index()
Remove cache entries older than the specified cutoff.
@spec save_index(String.t(), cache_index()) :: :ok | {:error, term()}
Save cache index to disk.
@spec update_stats(cache_index(), :hit | :miss, String.t()) :: cache_index()
Update cache statistics.