Sha256: 7eada4b87412c80751c0fc1656b0baa49a5db4d238d6ab9a234fbf0d59516e33

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

class CacheKeeper::CachedMethod
  include Refreshable
  include SerializableTarget

  attr_accessor :klass, :method_name, :options

  def initialize(klass, method_name, options = {})
    self.klass = klass
    self.method_name = method_name
    self.options = options.with_indifferent_access
  end

  def alias_for_original_method
    :"__#{method_name}__hooked__"
  end

  def stale?
    cache_entry.blank? || cache_entry.expired?
  end

  def call(target)
    if cache_entry.blank?
      refresh target
    elsif cache_entry.expired?
      if must_revalidate?
        refresh target
      else
        refresh_later target

        cache_entry.value
      end
    else
      cache_entry.value
    end
  end

  private

  def cache_entry
    Rails.cache.send :read_entry, Rails.cache.send(:normalize_key, cache_key, {})
  end

  def cache_key
    ["CacheKeeper", klass, method_name]
  end

  def expires_in
    options[:expires_in]
  end

  def must_revalidate?
    options[:must_revalidate].nil? ? CacheKeeper.configuration.must_revalidate : options[:must_revalidate]
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cache_keeper-0.3.0 app/models/cache_keeper/cached_method.rb