Ralph::StatementCache(V)
LRU (Least Recently Used) Cache for prepared statements
This cache stores compiled/prepared statements to avoid reparsing SQL queries. When the cache is full, the least recently used statement is evicted.
Thread Safety
Crystal uses fibers (green threads) with cooperative scheduling, so a mutex is used to ensure fiber-safety when accessing the cache.
Example
cache = Ralph::StatementCache(String).new(max_size: 100)
cache.set("SELECT * FROM users WHERE id = ?", "prepared_stmt_handle")
stmt = cache.get("SELECT * FROM users WHERE id = ?")
Constructors
Instance methods
Clear all cached statements
Returns an array of all evicted values so they can be cleaned up.
Remove a specific entry from the cache
Returns the removed value if found.
Enable or disable the cache
When disabled, get always returns nil and set is a no-op.
Existing entries are preserved but not accessible until re-enabled.
Get a cached value, marking it as recently used
Returns nil if not found or caching is disabled.
Store a value in the cache
If the cache is full, the least recently used entry is evicted. Returns the evicted value (if any) so it can be cleaned up.