class
CQL::Cache::Cache
- CQL::Cache::Cache
- Reference
- Object
Overview
Enhanced Query cache for storing and retrieving query results Now supports multiple cache backends (Memory, Redis) via CacheStore
Defined in:
cache/cache.crClass Method Summary
-
.cache(cache_name : String, params : Hash, ttl : Time::Span = @@default_ttl, &)
Cache a query result using a block - @param cache_name [String] Unique cache name - @param params [Hash] Query parameters for cache key generation - @param ttl [Time::Span] Time to live (default: 1 hour) - @yield [Void] Block to execute if cache miss - @return [T] The cached or computed result
-
.cache_name : String
Get default cache name - @return [String] The default cache name
-
.cache_name=(name : String)
Set default cache name - @param name [String] The default cache name to use
-
.cache_store : CacheInterface
Direct access to the underlying cache store for advanced usage
-
.cached?(cache_key : String) : Bool
Check if the current query result is cached - @param cache_key [String] The cache key to check - @return [Bool] True if the query result is cached
-
.clear
Clear all cached entries
-
.configure(config : CacheStoreConfig)
Configure cache store
-
.configure_from_env
Configure from environment variables
-
.default_ttl : Time::Span
Get default TTL - @return [Time::Span] Default time to live
-
.default_ttl=(ttl : Time::Span)
Set default TTL for cache entries - @param ttl [Time::Span] Default time to live
-
.enabled=(enabled : Bool)
Enable or disable caching - @param enabled [Bool] Whether to enable caching
-
.enabled? : Bool
Check if caching is enabled - @return [Bool] True if caching is enabled
-
.generate_cache_key(sql : String, params : Array, cache_name : String | Nil = nil) : String
Generate a cache key for SQL query and parameters - @param sql [String] The SQL query - @param params [Array] The query parameters - @param cache_name [String?] Optional cache name (uses default if nil) - @return [String] Unique cache key for the query
-
.get(key : String) : Array(DB::Any) | Nil
Get a cached result for the given key - @param key [String] The cache key - @return [Array(DB::Any)?] The cached result or nil if not found
-
.has_key?(key : String) : Bool
Check if a key exists in the cache - @param key [String] The cache key - @return [Bool] True if the key exists
-
.performance_summary : String
Get cache performance summary as a formatted string - @return [String] Formatted performance summary
-
.reset_stats
Reset cache statistics
-
.set(key : String, value : Array(DB::Any), ttl : Time::Span = @@default_ttl)
Set a cached result for the given key - @param key [String] The cache key - @param value [Array(DB::Any)] The value to cache - @param ttl [Time::Span] Time to live (default: 1 hour)
-
.size : Int32
Get the current cache size - @return [Int32] The number of cached entries
-
.statistics : Hash
Get detailed cache statistics as a hash - @return [Hash] Detailed statistics
-
.stats : CacheStats
Get cache statistics - @return [CacheStats] Current cache statistics
-
.with_cache(key : String, ttl : Time::Span = @@default_ttl, as_kind : Class | Nil = nil, &)
Helper to run a block with caching if enabled - @param key [String] The cache key - @param ttl [Time::Span] Time to live (default: 1 hour) - @param as_kind [Class] Type to cast the result to (optional) - @yield [Void] Block to execute if cache miss - @return [T] The cached or computed result
Class Method Detail
Cache a query result using a block
- @param cache_name [String] Unique cache name
- @param params [Hash] Query parameters for cache key generation
- @param ttl [Time::Span] Time to live (default: 1 hour)
- @yield [Void] Block to execute if cache miss
- @return [T] The cached or computed result
Set default cache name
- @param name [String] The default cache name to use
Direct access to the underlying cache store for advanced usage
Check if the current query result is cached
- @param cache_key [String] The cache key to check
- @return [Bool] True if the query result is cached
Set default TTL for cache entries
- @param ttl [Time::Span] Default time to live
Enable or disable caching
- @param enabled [Bool] Whether to enable caching
Generate a cache key for SQL query and parameters
- @param sql [String] The SQL query
- @param params [Array] The query parameters
- @param cache_name [String?] Optional cache name (uses default if nil)
- @return [String] Unique cache key for the query
Get a cached result for the given key
- @param key [String] The cache key
- @return [Array(DB::Any)?] The cached result or nil if not found
Check if a key exists in the cache
- @param key [String] The cache key
- @return [Bool] True if the key exists
Get cache performance summary as a formatted string
- @return [String] Formatted performance summary
Set a cached result for the given key
- @param key [String] The cache key
- @param value [Array(DB::Any)] The value to cache
- @param ttl [Time::Span] Time to live (default: 1 hour)
Get detailed cache statistics as a hash
- @return [Hash] Detailed statistics
Helper to run a block with caching if enabled
- @param key [String] The cache key
- @param ttl [Time::Span] Time to live (default: 1 hour)
- @param as_kind [Class] Type to cast the result to (optional)
- @yield [Void] Block to execute if cache miss
- @return [T] The cached or computed result