Raxol.Performance.ETS.CacheHelper (Raxol v2.6.0)

View Source

Shared helpers for ETS-backed LRU and simple caches.

Summary

Functions

Evicts the oldest entries (by timestamp in element position 3) when the table exceeds max_entries.

LRU lookup: returns {:ok, value} if found (updating the access timestamp), or :miss on cache miss.

Simple cache lookup: returns the cached value or calls compute_fn on miss, caching the result. No TTL or LRU tracking.

LRU insert: stores {key, value, now} and evicts oldest entries when the table exceeds max_entries.

Functions

enforce_limit(table, max_entries)

@spec enforce_limit(atom(), pos_integer()) :: :ok

Evicts the oldest entries (by timestamp in element position 3) when the table exceeds max_entries.

get_lru(table, key)

@spec get_lru(atom(), term()) :: {:ok, term()} | :miss

LRU lookup: returns {:ok, value} if found (updating the access timestamp), or :miss on cache miss.

Expects entries stored as {key, value, timestamp}.

get_or_compute(table, key, compute_fn)

@spec get_or_compute(atom(), term(), (-> term())) :: term()

Simple cache lookup: returns the cached value or calls compute_fn on miss, caching the result. No TTL or LRU tracking.

Expects entries stored as {key, value}.

put_lru(table, key, value, max_entries)

@spec put_lru(atom(), term(), term(), pos_integer()) :: :ok

LRU insert: stores {key, value, now} and evicts oldest entries when the table exceeds max_entries.