module RailheadCacheify

Public Class Methods

cache() click to toggle source
# File lib/railhead_cacheify.rb, line 10
def self.cache
  @cache_store ||= Rails.cache
end
cache_store=(options) click to toggle source
# File lib/railhead_cacheify.rb, line 6
def self.cache_store=(options)
  @cache_store = ActiveSupport::Cache.lookup_store(options)
end
included(base) click to toggle source
# File lib/railhead_cacheify.rb, line 14
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

Public Instance Methods

delete_cache(key) click to toggle source
# File lib/railhead_cacheify.rb, line 21
def delete_cache(key)
  RailheadCacheify.cache.delete("#{key}:#{self.class.name}:#{self.id}") unless new_record?
end
read_cache(key, options = {}) { |: cache.fetch("#{key}:#{class.name}:#{id}", options) { yield }| ... } click to toggle source
# File lib/railhead_cacheify.rb, line 17
def read_cache(key, options = {}, &block)
  new_record? ? yield : RailheadCacheify.cache.fetch("#{key}:#{self.class.name}:#{self.id}", options) { yield }
end