Module | ActionController::Caching::Fragments |
In: |
vendor/rails/actionpack/lib/action_controller/caching.rb
|
Fragment caching is used for caching various blocks within templates without caching the entire action as a whole. This is useful when certain elements of an action change frequently or depend on complicated state while other parts rarely change or can be shared amongst multiple parties. The caching is doing using the cache helper available in the Action View. A template with caching might look something like:
<b>Hello <%= @name %></b> <% cache do %> All the topics in the system: <%= render_collection_of_partials "topic", Topic.find_all %> <% end %>
This cache will bind to the name of action that called it. So you would be able to invalidate it using expire_fragment(:controller => "topics", :action => "list") — if that was the controller/action used. This is not too helpful if you need to cache multiple fragments per action or if the action itself is cached using caches_action. So instead we should qualify the name of the action used with something like:
<% cache(:action => "list", :action_suffix => "all_topics") do %>
That would result in a name such as "/topics/list/all_topics", which wouldn’t conflict with any action cache and neither with another fragment using a different suffix. Note that the URL doesn’t have to really exist or be callable. We‘re just using the url_for system to generate unique cache names that we can refer to later for expirations. The expiration call for this example would be expire_fragment(:controller => "topics", :action => "list", :action_suffix => "all_topics").
In order to use the fragment caching, you need to designate where the caches should be stored. This is done by assigning a fragment store of which there are four different kinds:
Configuration examples (MemoryStore is the default):
ActionController::Base.fragment_cache_store = :memory_store ActionController::Base.fragment_cache_store = :file_store, "/path/to/cache/directory" ActionController::Base.fragment_cache_store = :drb_store, "druby://localhost:9192" ActionController::Base.fragment_cache_store = :mem_cache_store, "localhost" ActionController::Base.fragment_cache_store = MyOwnStore.new("parameter")
# File vendor/rails/actionpack/lib/action_controller/caching.rb, line 255 255: def self.fragment_cache_store=(store_option) 256: store, *parameters = *([ store_option ].flatten) 257: @@fragment_cache_store = if store.is_a?(Symbol) 258: store_class_name = (store == :drb_store ? "DRbStore" : store.to_s.camelize) 259: store_class = ActionController::Caching::Fragments.const_get(store_class_name) 260: store_class.new(*parameters) 261: else 262: store 263: end 264: end
Called by CacheHelper#cache
# File vendor/rails/actionpack/lib/action_controller/caching.rb, line 273 273: def cache_erb_fragment(block, name = {}, options = nil) 274: unless perform_caching then block.call; return end 275: 276: buffer = eval("_erbout", block.binding) 277: 278: if cache = read_fragment(name, options) 279: buffer.concat(cache) 280: else 281: pos = buffer.length 282: block.call 283: write_fragment(name, buffer[pos..-1], options) 284: end 285: end
Name can take one of three forms:
# File vendor/rails/actionpack/lib/action_controller/caching.rb, line 310 310: def expire_fragment(name, options = nil) 311: return unless perform_caching 312: 313: key = fragment_cache_key(name) 314: 315: if key.is_a?(Regexp) 316: self.class.benchmark "Expired fragments matching: #{key.source}" do 317: fragment_cache_store.delete_matched(key, options) 318: end 319: else 320: self.class.benchmark "Expired fragment: #{key}" do 321: fragment_cache_store.delete(key, options) 322: end 323: end 324: end
# File vendor/rails/actionpack/lib/action_controller/caching.rb, line 268 268: def fragment_cache_key(name) 269: name.is_a?(Hash) ? url_for(name).split("://").last : name 270: end
# File vendor/rails/actionpack/lib/action_controller/caching.rb, line 297 297: def read_fragment(name, options = nil) 298: return unless perform_caching 299: 300: key = fragment_cache_key(name) 301: self.class.benchmark "Fragment read: #{key}" do 302: fragment_cache_store.read(key, options) 303: end 304: end
# File vendor/rails/actionpack/lib/action_controller/caching.rb, line 287 287: def write_fragment(name, content, options = nil) 288: return unless perform_caching 289: 290: key = fragment_cache_key(name) 291: self.class.benchmark "Cached fragment: #{key}" do 292: fragment_cache_store.write(key, content, options) 293: end 294: content 295: end