Module: ActionController::Caching::Fragments

Defined in:
lib/extended_fragment_cache.rb,
lib/extended_fragment_cache.rb

Instance Method Summary collapse

Instance Method Details

#cache_erb_fragment(block, name = {}, options = nil, interpolation = {}) ⇒ Object

Called by CacheHelper#cache



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/extended_fragment_cache.rb', line 154

def cache_erb_fragment(block, name={}, options=nil, interpolation={})
  unless perform_caching then 
    content = block.call
    interpolation.keys.each{|k|content.sub!(k.to_s,interpolation[k].to_s)}
    content
    return 
  end

  buffer = eval("_erbout", block.binding)

  if cache = read_fragment(name, options)
    buffer.concat(cache)
  else
    pos = buffer.length
    block.call
    write_fragment(name, buffer[pos..-1], options)
    interpolation.keys.each{|k|
      buffer[pos..-1] = buffer[pos..-1].sub!(k.to_s,interpolation[k].to_s)
    }
    buffer[pos..-1]
  end
end

#read_fragment(name, options = nil) ⇒ Object

Override read_fragment so that it checks the local_fragment_cache object before going to the fragment_cache_store backend.



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/extended_fragment_cache.rb', line 57

def read_fragment(name, options = nil)
  return unless perform_caching

  key = fragment_cache_key(name)
  self.class.benchmark "Fragment read: #{key}" do
    content = ApplicationController.local_fragment_cache[key]
    if content.nil?
      content = fragment_cache_store.read(key, options)
      ApplicationController.local_fragment_cache[key] = content
    end
    content
  end
end

#write_fragment(name, content, options = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/extended_fragment_cache.rb', line 71

def write_fragment(name, content, options = nil)
  return unless perform_caching

  key = fragment_cache_key(name)
  self.class.benchmark "Cached fragment: #{key}" do
    ApplicationController.local_fragment_cache[key] = content
    fragment_cache_store.write(key, content, options)
  end
  content
end