Module: RailheadCacheify

Defined in:
lib/railhead_cacheify.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.cacheObject



10
11
12
# File 'lib/railhead_cacheify.rb', line 10

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

.cache_store=(options) ⇒ Object



6
7
8
# File 'lib/railhead_cacheify.rb', line 6

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

.included(base) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# 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