ractogateway.redis.exact_cache
Redis-backed exact-match cache — distributed drop-in for ExactMatchCache.
Stores LLM responses in Redis so the cache is shared across every process and
server in a fleet. The public API is byte-for-byte identical to
ExactMatchCache, which means you can substitute
RedisExactCache wherever ExactMatchCache is accepted (including all
developer-kit exact_cache= parameters) without changing any other code.
Thread-safety
Stats counters are guarded by threading.Lock (same pattern as the in-process
cache). The Redis operations themselves are atomic at the command level; no
additional locking is required across processes.
Cache key
"{key_prefix}:{sha256_hex}" where the SHA-256 digest is computed from
(user_message, system_prompt, model, temperature, max_tokens) — identical
hashing logic to _make_key().
Example:
from ractogateway.redis import RedisExactCache
from ractogateway import openai_developer_kit as gpt
cache = RedisExactCache(
url="redis://localhost:6379/0",
ttl_seconds=3600,
)
kit = gpt.OpenAIDeveloperKit(model="gpt-4o", exact_cache=cache)
- class ractogateway.redis.exact_cache.RedisExactCache(*, url='redis://localhost:6379/0', client=None, ttl_seconds=None, key_prefix='ractogateway:exact')[source]
Bases:
objectDistributed exact-match LRU cache backed by Redis.
- Parameters:
url (
str) – Redis connection URL (e.g."redis://localhost:6379/0"). Ignored when client is provided.client (
Any|None) – Pre-builtredis.Redis(or compatible) client. Useful when you manage the connection pool yourself or use a mock in tests.ttl_seconds (
float|None) – Optional TTL for each entry. Passed directly to RedisSET EX.Nonemeans entries never expire (Redis default).key_prefix (
str) – Namespace for all Redis keys managed by this instance.
- get(user_message, system_prompt, model, temperature, max_tokens)[source]
Return a cached response or
Noneon a miss.O(1) Redis GET.
- Return type:
- put(user_message, system_prompt, model, temperature, max_tokens, response)[source]
Store a response in Redis.
O(1) Redis SET [EX ttl].
- Return type:
- invalidate(user_message, system_prompt, model, temperature, max_tokens)[source]
Remove a specific entry. Returns
Trueif it was present.- Return type:
- clear()[source]
Delete all entries matching this instance’s key prefix.
Uses SCAN to iterate safely (no KEYS * in production). Also resets in-memory stats counters.
- Return type:
- property stats: CacheStats
Return a snapshot of hit/miss counters plus current Redis key count.