Ragex.AI.Cache
(Ragex v0.10.0)
View Source
AI response cache for reducing costs and improving performance.
Caches AI provider responses using ETS with TTL-based expiration and LRU eviction.
Configuration
config :ragex, :ai_cache,
enabled: true,
ttl: 3600, # 1 hour in seconds
max_size: 1000, # maximum cache entries
operation_caches: %{
query: %{ttl: 3600, max_size: 500},
explain: %{ttl: 7200, max_size: 300},
suggest: %{ttl: 1800, max_size: 200}
}Cache Key Generation
Cache keys are SHA256 hashes of:
- Query/prompt text
- Context (if provided)
- Model name
- Provider
- Relevant parameters (temperature, etc.)
Features
- TTL-based expiration
- LRU eviction when max size exceeded
- Separate caches per operation type
- Cache hit/miss metrics
- Thread-safe ETS operations
Summary
Functions
Returns a specification to start this module under a supervisor.
Clear the entire cache.
Clear cache for a specific operation. Note: Currently clears entire cache regardless of operation.
Get a cached response if it exists and hasn't expired.
Store a response in the cache.
Start the cache GenServer.
Get cache statistics.
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
Clear the entire cache.
Clear cache for a specific operation. Note: Currently clears entire cache regardless of operation.
Get a cached response if it exists and hasn't expired.
Returns
{:ok, response}- Cache hit with valid entry{:error, :not_found}- Cache miss or expired entry
Store a response in the cache.
Start the cache GenServer.
Get cache statistics.