Class: RailsAi::Performance::SmartCache

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai/performance.rb

Overview

Smart caching with compression

Instance Method Summary collapse

Constructor Details

#initialize(compression_threshold: 1024) ⇒ SmartCache

Returns a new instance of SmartCache.



93
94
95
# File 'lib/rails_ai/performance.rb', line 93

def initialize(compression_threshold: 1024)
  @compression_threshold = compression_threshold
end

Instance Method Details

#fetch(key, **opts, &block) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rails_ai/performance.rb', line 97

def fetch(key, **opts, &block)
  return block.call unless block_given?

  if defined?(Rails) && Rails.cache
    compressed_key = compress_key(key)
    Rails.cache.fetch(compressed_key, **opts) do
      result = block.call
      compress_if_needed(result)
    end
  else
    block.call
  end
end