Skip to content

Class HTM::QueryCache

Inherits Object
Defined in lib/htm/query_cache.rb

Thread-safe query result cache with TTL and statistics

Provides LRU caching for expensive query results with: * Configurable size and TTL * Thread-safe statistics tracking * Fast cache key generation (using Ruby's built-in hash) * Selective cache invalidation by method type

@example Create a cache

cache = HTM::QueryCache.new(size: 1000, ttl: 300)

@example Use the cache

result = cache.fetch(:search, timeframe, query, limit) do
  expensive_search_operation
end

@example Check statistics

cache.stats
# => { hits: 42, misses: 10, hit_rate: 80.77, size: 52 }

@example Selective invalidation

cache.invalidate_methods!(:search, :hybrid)  # Only invalidate search-related entries

Constants

METHOD_PREFIX

Cache key prefix for method-based invalidation

Attributes

enabled [R]

Returns the value of attribute enabled.

Public Instance Methods

clear!()

Clear all cached entries - @return [void]

enabled?()

Check if cache is enabled - @return [Boolean]

fetch(method)

Fetch a value from cache or execute block - @param method [Symbol] Method name for cache key - @param args [Array] Arguments for cache key - @return [Object] Cached or computed value - @yield Block that computes the value if not cached

initialize(size: 1000, ttl: 300)

Initialize a new query cache - @param size [Integer] Maximum number of entries (default: 1000, use 0 to disable) - @param ttl [Integer] Time-to-live in seconds (default: 300) - @return [QueryCache] a new instance of QueryCache

invalidate!()

Invalidate cache (alias for clear!) - @return [void]

invalidate_methods!(*methods)

Invalidate cache entries for specific methods only

More efficient than full invalidation when only certain types of cached data need to be refreshed. - @param methods [Array] Method names to invalidate - @return [Integer] Number of entries invalidated

stats()

Get cache statistics - @return [Hash, nil] Statistics hash or nil if disabled