LlmCore.Memory.Hindsight.Cache (llm_core v0.3.0)

Copy Markdown View Source

Smart caching layer for Hindsight operations.

Features:

  • TTL-based expiry
  • LRU eviction when max entries exceeded
  • Stale-while-revalidate for fast responses
  • Background refresh before TTL expiry
  • Manual invalidation

Cache Keys

  • Recall: {:recall, query_hash, opts_hash}
  • Reflect: {:reflect, question_hash}

Summary

Functions

Returns a specification to start this module under a supervisor.

Clears all cached entries.

Gets a cached value by key, returning {:hit, value} or :miss.

Gets a cached value, allowing stale data within grace period. Returns {:hit, value} or {:stale, value} or :miss.

Invalidates a specific cache entry.

Invalidates entries matching a key prefix/pattern.

Stores a value in the cache with TTL.

Generates a cache key for recall operations.

Generates a cache key for reflect operations.

Starts the cache GenServer.

Returns cache statistics.

Types

cache_entry()

@type cache_entry() :: %{
  value: term(),
  inserted_at: integer(),
  ttl_ms: pos_integer(),
  last_access: integer()
}

stats()

@type stats() :: %{
  hits: non_neg_integer(),
  misses: non_neg_integer(),
  size: non_neg_integer()
}

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

clear()

@spec clear() :: :ok

Clears all cached entries.

get(key)

@spec get(term()) :: {:hit, term()} | :miss

Gets a cached value by key, returning {:hit, value} or :miss.

get_with_stale(key)

@spec get_with_stale(term()) :: {:hit, term()} | {:stale, term()} | :miss

Gets a cached value, allowing stale data within grace period. Returns {:hit, value} or {:stale, value} or :miss.

invalidate(key)

@spec invalidate(term()) :: :ok

Invalidates a specific cache entry.

invalidate_pattern(pattern)

@spec invalidate_pattern(term()) :: :ok

Invalidates entries matching a key prefix/pattern.

put(key, value, opts \\ [])

@spec put(term(), term(), keyword()) :: :ok

Stores a value in the cache with TTL.

recall_key(query, opts)

@spec recall_key(
  String.t(),
  keyword()
) :: term()

Generates a cache key for recall operations.

reflect_key(question, opts \\ [])

@spec reflect_key(
  String.t() | atom(),
  keyword()
) :: term()

Generates a cache key for reflect operations.

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

Starts the cache GenServer.

stats()

@spec stats() :: stats()

Returns cache statistics.