Module: ActionController::Caching::Fragments

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.expire_fragment(name, options = nil) ⇒ Object

Add expire_fragments as class method so that we can expire cached content from models, etc.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/extended_fragment_cache.rb', line 107

def self.expire_fragment(name, options = nil)
  return unless ApplicationController.perform_caching

  key = self.fragment_cache_key(name)

  if key.is_a?(Regexp)
    ApplicationController.benchmark "Expired fragments matching: #{key.source}" do
      ActionController::Base.cache_store.delete_matched(key, options)
    end
  else
    ApplicationController.benchmark "Expired fragment: #{key}" do
      ActionController::Base.cache_store.delete(key, options)
    end
  end
rescue NameError => err
  # ignore bogus uninitialized constant ApplicationController errors
end

.fragment_cache_key(name) ⇒ Object

Utility method needed by class methods



101
102
103
# File 'lib/extended_fragment_cache.rb', line 101

def self.fragment_cache_key(name)
  name.is_a?(Hash) ? name.to_s : name
end

.read_fragment(name, options = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/extended_fragment_cache.rb', line 63

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

  key = self.fragment_cache_key(name)
  content = ApplicationController.local_fragment_cache[key]
  ApplicationController.benchmark "Fragment read: #{key}" do
    if content.nil?
      content = ActionController::Base.cache_store.read(key, options)
      ApplicationController.local_fragment_cache[key] = content
    end
  end
  content
rescue NameError => err
  # ignore bogus uninitialized constant ApplicationController errors
end

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



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/extended_fragment_cache.rb', line 87

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

  key = self.fragment_cache_key(name)
  ApplicationController.benchmark "Cached fragment: #{key}" do
    ApplicationController.local_fragment_cache[key] = content
    ActionController::Base.cache_store.write(key, content, options)
  end
  content
rescue NameError => err
  # ignore bogus uninitialized constant ApplicationController errors
end

Instance Method Details

#fragment_for(buffer, name = {}, options = nil, interpolation = {}, &block) ⇒ Object

Called by CacheHelper#cache



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/extended_fragment_cache.rb', line 199

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

  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.

  • also allow fragments to be read using a class method (from a model)



58
59
60
61
# File 'lib/extended_fragment_cache.rb', line 58

def read_fragment(name, options = nil)
  name = url_for(name.merge({:only_path => true})) if name.class == Hash
  ActionController::Caching::Fragments.read_fragment(name, options)
end

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



79
80
81
82
83
84
85
# File 'lib/extended_fragment_cache.rb', line 79

def write_fragment(name, content, options=nil)
  name = url_for(name.merge({:only_path => true})) if name.class == Hash
  ActionController::Caching::Fragments.write_fragment(name, content, options)
rescue NameError => err
  # ignore bogus uninitialized constant ApplicationController errors
  # when running Rails outside of web container
end