Module: ClaudeMemory::Store::LLMCache

Included in:
SQLiteStore
Defined in:
lib/claude_memory/store/llm_cache.rb

Overview

LLM cache persistence for the SQLiteStore. Keyed on SHA-256 of “operation:model:input_hash” so identical (operation, model, input) tuples collapse to a single row via upsert. Pruning is age-based — callers decide the retention window.

Instance Method Summary collapse

Instance Method Details

#llm_cache_key(operation, model, input) ⇒ String

Compute the cache key for an LLM operation.

Parameters:

  • operation (String)

    operation name

  • model (String)

    model identifier

  • input (String)

    raw input text

Returns:

  • (String)

    SHA-256 hex cache key



54
55
56
57
# File 'lib/claude_memory/store/llm_cache.rb', line 54

def llm_cache_key(operation, model, input)
  input_hash = Digest::SHA256.hexdigest(input)
  Digest::SHA256.hexdigest("#{operation}:#{model}:#{input_hash}")
end

#llm_cache_lookup(cache_key) ⇒ Hash?

Look up a cached LLM result by its cache key.

Parameters:

  • cache_key (String)

    SHA-256 hex cache key

Returns:

  • (Hash, nil)


15
16
17
# File 'lib/claude_memory/store/llm_cache.rb', line 15

def llm_cache_lookup(cache_key)
  llm_cache.where(cache_key: cache_key).first
end

#llm_cache_prune(max_age_seconds: 604_800) ⇒ Integer

Delete LLM cache entries older than the given age.

Parameters:

  • max_age_seconds (Integer) (defaults to: 604_800)

    maximum age in seconds (default: 7 days)

Returns:

  • (Integer)

    number of rows deleted



62
63
64
65
# File 'lib/claude_memory/store/llm_cache.rb', line 62

def llm_cache_prune(max_age_seconds: 604_800)
  cutoff = (Time.now - max_age_seconds).utc.iso8601
  llm_cache.where { created_at < cutoff }.delete
end

#llm_cache_store(operation:, model:, input_hash:, result_json:, input_tokens: nil, output_tokens: nil) ⇒ void

This method returns an undefined value.

Store or update a cached LLM result. Uses upsert on the cache_key.

Parameters:

  • operation (String)

    operation name (e.g. “distill”, “embed”)

  • model (String)

    model identifier

  • input_hash (String)

    SHA-256 hex digest of the input

  • result_json (String)

    JSON-serialized result

  • input_tokens (Integer, nil) (defaults to: nil)

    input tokens consumed

  • output_tokens (Integer, nil) (defaults to: nil)

    output tokens consumed



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/claude_memory/store/llm_cache.rb', line 27

def llm_cache_store(operation:, model:, input_hash:, result_json:, input_tokens: nil, output_tokens: nil)
  cache_key = Digest::SHA256.hexdigest("#{operation}:#{model}:#{input_hash}")

  llm_cache
    .insert_conflict(target: :cache_key, update: {
      result_json: result_json,
      input_tokens: input_tokens,
      output_tokens: output_tokens,
      created_at: Time.now.utc.iso8601
    })
    .insert(
      cache_key: cache_key,
      operation: operation,
      model: model,
      input_hash: input_hash,
      result_json: result_json,
      input_tokens: input_tokens,
      output_tokens: output_tokens,
      created_at: Time.now.utc.iso8601
    )
end