module Goodyear::QueryCache

Public Instance Methods

cache_query(query) { || ... } click to toggle source
# File lib/goodyear/query_cache.rb, line 8
def cache_query(query)
  cache_key = sha(query)
  Goodyear.force_cache
  result = if store.exist?(cache_key) && Goodyear.force_cache
      ActiveSupport::Notifications.instrument "cache.query.elasticsearch", name: self.name, query: query
      store.fetch cache_key
    else
      res = []
      ActiveSupport::Notifications.instrument "query.elasticsearch", name: self.name, query: query do
        res = yield
      end
      store.write(cache_key, res) if Goodyear.force_cache
      res
    end
  result.dup
end
store() click to toggle source
# File lib/goodyear/query_cache.rb, line 4
def store
 @query_cache ||= setup_store!
end

Private Instance Methods

setup_store!() click to toggle source
# File lib/goodyear/query_cache.rb, line 32
def setup_store!
 case Rails.application.config.goodyear_cache_store
 when :redis_store
   ActiveSupport::Cache::RedisStore
 when :memory_store
   ActiveSupport::Cache::MemoryStore
 else
   ActiveSupport::Cache::MemoryStore
 end.new(namespace: 'elasticsearch', expires_in: Rails.application.config.goodyear_expire_cache_in)
end
sha(str) click to toggle source
# File lib/goodyear/query_cache.rb, line 28
def sha(str)
  Digest::SHA256.new.hexdigest(str)
end