Module: Cacheable::KeyCache

Included in:
Caches
Defined in:
lib/cacheable/types/key_cache.rb

Instance Method Summary collapse

Instance Method Details

#with_keyObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/cacheable/types/key_cache.rb', line 3

def with_key
  self.cached_key = true

  class_eval do
    after_commit :expire_key_cache, on: :update
  end

  define_singleton_method("find_cached") do |id|
    cache_key = self.instance_cache_key(id)
    Cacheable.fetch(cache_key) do
      self.find(id)
    end
  end
end