Class: CodeToQuery::Performance::QueryCache

Inherits:
Cache
  • Object
show all
Defined in:
lib/code_to_query/performance/cache.rb

Overview

Query execution result cache

Constant Summary

Constants inherited from Cache

Cache::DEFAULT_TTL, Cache::MAX_CACHE_SIZE

Instance Method Summary collapse

Methods inherited from Cache

#clear, #delete, #get, #hit_rate, #set, #stats

Constructor Details

#initialize(config = {}) ⇒ QueryCache

Returns a new instance of QueryCache.



225
226
227
# File 'lib/code_to_query/performance/cache.rb', line 225

def initialize(config = {})
  super(config.merge(max_size: 200)) # Smaller cache for query results
end

Instance Method Details

#cache_query_result(sql, params, result) ⇒ Object



229
230
231
232
233
# File 'lib/code_to_query/performance/cache.rb', line 229

def cache_query_result(sql, params, result)
  cache_key = build_query_key(sql, params)
  # Shorter TTL for query results as data changes frequently
  set(cache_key, result, ttl: 300) # 5 minutes
end

#get_cached_result(sql, params) ⇒ Object



235
236
237
238
# File 'lib/code_to_query/performance/cache.rb', line 235

def get_cached_result(sql, params)
  cache_key = build_query_key(sql, params)
  get(cache_key)
end