module RailsCacheIt::RelationExtension

Public Instance Methods

cache_it(options = RailsCacheIt.default_options.dup, &blk) click to toggle source
# File lib/rails_cache_it/relation_extension.rb, line 27
def cache_it options = RailsCacheIt.default_options.dup, &blk
  options[:blk] = blk if blk
  @_cache_it_options = options
  self
end
to_a_with_cache_it() click to toggle source
# File lib/rails_cache_it/relation_extension.rb, line 9
def to_a_with_cache_it
  options = @_cache_it_options
  if options
    cache_name = to_sql
    blk =  options.delete :blk
    blk_val = blk && blk.call
    result, pre_val = Rails.cache.fetch(cache_name, options)
    if result.nil? || (blk_val != pre_val)
      result, _ = Rails.cache.fetch cache_name, options.merge(force: true) do
        [self.to_a_without_cache_it, blk_val]
      end
    end
    result
  else
    self.to_a_without_cache_it
  end
end