Module: CacheShoe::ClassMethods::Helpers

Defined in:
lib/cache_shoe.rb

Instance Method Summary collapse

Instance Method Details

#create_cache_clear_wrapper_methods(cached_method, clear_on) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/cache_shoe.rb', line 140

def create_cache_clear_wrapper_methods(cached_method, clear_on)
  clear_on.each do |clearing_method, key_extractors|
    define_method clearing_method do |*args, &blk|
      class_name = self.class.name
      CacheShoe.on_cache_clear(
        class_name, cached_method,
      clearing_method, key_extractors, *args)
      super(*args, &blk)
    end
  end
end

#wrap_the_method_to_cache(method_id) ⇒ Object

Move the CacheShoe methods here



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/cache_shoe.rb', line 124

def wrap_the_method_to_cache(method_id)
  define_method method_id do |*args, &blk|
    class_name = self.class.name
    key_val = CacheShoe.cache_key(class_name, method_id, args)

    cache_hit = true
    result = CacheShoe.cache.fetch(key_val) do
      cache_hit = false
      CacheShoe.on_cache_miss key_val
      CacheShoe.nil_wrapper super(*args, &blk)
    end
    CacheShoe.on_cache_hit(key_val) if cache_hit
    CacheShoe.nil_unwrapper result
  end
end