StatementCache¶
class
Defined in src/ralph/statement_cache.cr:19
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¶
.new(max_size : Int32 = 100, enabled : Bool = true)¶
Creates a new statement cache
Parameters¶
max_size: Maximum number of statements to cache (default: 100)enabled: Whether caching is enabled (default: true)
Instance Methods¶
#clear¶
Clear all cached statements
Returns an array of all evicted values so they can be cleaned up.
#delete(key : String) : V | Nil¶
Remove a specific entry from the cache
Returns the removed value if found.
#get(key : String) : V | Nil¶
Get a cached value, marking it as recently used
Returns nil if not found or caching is disabled.
#has?(key : String) : Bool¶
Check if a key exists in the cache
#set(key : String, value : V) : V | Nil¶
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.
#size¶
Get current number of cached statements
#stats¶
Get cache statistics