Sha256: f1c96db81ce83dbee5889ba9db2ee6ed7d420e2deffa20093468616387fb4354

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

require 'active_support'


module RailheadCacheify

  def self.cache_store=(options)
    @cache_store = ActiveSupport::Cache.lookup_store(options)
  end

  def self.cache
    @cache_store ||= Rails.cache
  end

  def self.included(base)
    base.extend ClassMethods
    base.class_eval do
      def read_cache(key, options = {}, &block)
        new_record? ? yield : RailheadCacheify.cache.fetch("#{key}:#{self.class.name}:#{self.id}", options) { yield }
      end

      def delete_cache(key)
        RailheadCacheify.cache.delete("#{key}:#{self.class.name}:#{self.id}") unless new_record?
      end
    end
  end

  module ClassMethods

    def cacheify(key, options = {})
      class_eval <<-END
        alias _original_#{key} #{key}
        def #{key}(*args)
          @#{key} ||= read_cache(:#{key}, #{options[:expires_in] ? "{expires_in: #{options[:expires_in]}}" : '{}'}) { _original_#{key}(*args) }
        end
      END
    end
  end
end


module RailheadCacheifyLoader

  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods

    def use_cacheify
      include RailheadCacheify
    end
  end
end


ActiveRecord::Base.send :include, RailheadCacheifyLoader

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
railhead_cacheify-0.0.5 lib/railhead_cacheify.rb