Module: ExtremistCache

Defined in:
lib/extremist_cache.rb

Constant Summary collapse

DIGEST =
OpenSSL::Digest::MD4
VERSION =
'0.0.3'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bootstrap!Object

To be called from a plugin init.rb



7
8
9
10
11
12
# File 'lib/extremist_cache.rb', line 7

def self.bootstrap!
  if !@boostrapped
    ::ActionController::Base.send(:include, ExtremistCache)
    ::ActionController::Base.send(:helper, ExtremistCache)
  end
end

Instance Method Details

#cached_based_on(*whatever) ⇒ Object

This one should be used from controllers and helpers



15
16
17
18
# File 'lib/extremist_cache.rb', line 15

def cached_based_on(*whatever)
  segmented_path = lazy_cache_key_for(*whatever)
  (@controller || self).read_fragment(segmented_path) || (@controller || self).write_fragment(segmented_path, yield)
end

#erb_cache_based_on(*whatever, &block) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/extremist_cache.rb', line 27

def erb_cache_based_on(*whatever, &block)
  begin
    name = {:__extremist => lazy_cache_key_for(whatever)}
    @controller.fragment_for(output_buffer, name, nil, &block)
  rescue NoMethodError
    @controller.cache_erb_fragment(block, lazy_cache_key_for(*whatever))
  end
end

#value_cached_based_on(*whatever) ⇒ Object

This one is for blocks that return objects, results of expensive computation



21
22
23
24
25
# File 'lib/extremist_cache.rb', line 21

def value_cached_based_on(*whatever)
  segmented_path = lazy_cache_key_for(whatever)
  fragment = (@controller || self).read_fragment(segmented_path)
  fragment ? Marshal.load(fragment) : returning(yield) {|f| (@controller || self).write_fragment(segmented_path, Marshal.dump(f)) }
end