class CQL::Cache::Cache

Overview

Enhanced Query cache for storing and retrieving query results Now supports multiple cache backends (Memory, Redis) via CacheStore

Defined in:

cache/cache.cr

Class Method Summary

Class Method Detail

def self.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

[View source]
def self.cache_name : String #

Get default cache name

  • @return [String] The default cache name

[View source]
def self.cache_name=(name : String) #

Set default cache name

  • @param name [String] The default cache name to use

[View source]
def self.cache_store : CacheInterface #

Direct access to the underlying cache store for advanced usage


[View source]
def self.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

[View source]
def self.clear #

Clear all cached entries


[View source]
def self.configure(config : CacheStoreConfig) #

Configure cache store


[View source]
def self.configure_from_env #

Configure from environment variables


[View source]
def self.default_ttl : Time::Span #

Get default TTL

  • @return [Time::Span] Default time to live

[View source]
def self.default_ttl=(ttl : Time::Span) #

Set default TTL for cache entries

  • @param ttl [Time::Span] Default time to live

[View source]
def self.enabled=(enabled : Bool) #

Enable or disable caching

  • @param enabled [Bool] Whether to enable caching

[View source]
def self.enabled? : Bool #

Check if caching is enabled

  • @return [Bool] True if caching is enabled

[View source]
def self.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

[View source]
def self.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

[View source]
def self.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

[View source]
def self.performance_summary : String #

Get cache performance summary as a formatted string

  • @return [String] Formatted performance summary

[View source]
def self.reset_stats #

Reset cache statistics


[View source]
def self.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)

[View source]
def self.size : Int32 #

Get the current cache size

  • @return [Int32] The number of cached entries

[View source]
def self.statistics : Hash #

Get detailed cache statistics as a hash

  • @return [Hash] Detailed statistics

[View source]
def self.stats : CacheStats #

Get cache statistics

  • @return [CacheStats] Current cache statistics

[View source]
def self.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

[View source]