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
-
#llm_cache_key(operation, model, input) ⇒ String
Compute the cache key for an LLM operation.
-
#llm_cache_lookup(cache_key) ⇒ Hash?
Look up a cached LLM result by its cache key.
-
#llm_cache_prune(max_age_seconds: 604_800) ⇒ Integer
Delete LLM cache entries older than the given age.
-
#llm_cache_store(operation:, model:, input_hash:, result_json:, input_tokens: nil, output_tokens: nil) ⇒ void
Store or update a cached LLM result.
Instance Method Details
#llm_cache_key(operation, model, input) ⇒ String
Compute the cache key for an LLM operation.
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.
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.
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.
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 |