Search documentation of Ragex

# Ragex.AI.Cache (Ragex v0.4.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

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

clear()

Clear the entire cache.

clear(operation)

Clear cache for a specific operation. Note: Currently clears entire cache regardless of operation.

get(operation, query, context, opts \\ [])

Get a cached response if it exists and hasn't expired.

put(operation, query, context, response, opts \\ [])

Store a response in the cache.

start_link(opts \\ [])

Start the cache GenServer.

stats()

Get cache statistics.

## Functions

# child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# clear()

Clear the entire cache.

# clear(operation)

Clear cache for a specific operation. Note: Currently clears entire cache regardless of operation.

# get(operation, query, context, opts \\ [])

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

# put(operation, query, context, response, opts \\ [])

Store a response in the cache.

# start_link(opts \\ [])

Start the cache GenServer.

# stats()

Get cache statistics.