Module | ActionController::Caching::Fragments |
In: |
vendor/rails/actionpack/lib/action_controller/caching/fragments.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 :partial => "topic", :collection => Topic.find(:all) %> <% end %>
This cache will bind to the name of the action that called it, so if this code was part of the view for the topics/list action, you would be able to invalidate it using expire_fragment(:controller => "topics", :action => "list").
This default behavior is of limited use if you need to cache multiple fragments per action or if the action itself is cached using caches_action, so we also have the option to qualify the name of the cached fragment with something like:
<% cache(:action => "list", :action_suffix => "all_topics") do %>
That would result in a name such as "/topics/list/all_topics", avoiding conflicts with the action cache and with any fragments that use a different suffix. Note that the URL doesn‘t have to really exist or be callable - the url_for system is just used to generate unique cache names that we can refer to when we need to expire the cache.
The expiration call for this example is:
expire_fragment(:controller => "topics", :action => "list", :action_suffix => "all_topics")
Name can take one of three forms:
%r{pages/\d*/notes}
Ensure you do not specify start and finish in the regex (^$) because the actual filename matched looks like ./cache/filename/path.cache Regexp expiration is only supported on caches that can iterate over all keys (unlike memcached).
# File vendor/rails/actionpack/lib/action_controller/caching/fragments.rb, line 121 121: def expire_fragment(key, options = nil) 122: return unless cache_configured? 123: 124: key = key.is_a?(Regexp) ? key : fragment_cache_key(key) 125: 126: if key.is_a?(Regexp) 127: self.class.benchmark "Expired fragments matching: #{key.source}" do 128: cache_store.delete_matched(key, options) 129: end 130: else 131: self.class.benchmark "Expired fragment: #{key}" do 132: cache_store.delete(key, options) 133: end 134: end 135: end
Given a key (as described in expire_fragment), returns a key suitable for use in reading, writing, or expiring a cached fragment. If the key is a hash, the generated key is the return value of url_for on that hash (without the protocol). All keys are prefixed with "views/" and uses ActiveSupport::Cache.expand_cache_key for the expansion.
# File vendor/rails/actionpack/lib/action_controller/caching/fragments.rb, line 59 59: def fragment_cache_key(key) 60: ActiveSupport::Cache.expand_cache_key(key.is_a?(Hash) ? url_for(key).split("://").last : key, :views) 61: end
Check if a cached fragment from the location signified by key exists (see expire_fragment for acceptable formats)
# File vendor/rails/actionpack/lib/action_controller/caching/fragments.rb, line 102 102: def fragment_exist?(key, options = nil) 103: return unless cache_configured? 104: 105: key = fragment_cache_key(key) 106: 107: self.class.benchmark "Cached fragment exists?: #{key}" do 108: cache_store.exist?(key, options) 109: end 110: end
Reads a cached fragment from the location signified by key (see expire_fragment for acceptable formats)
# File vendor/rails/actionpack/lib/action_controller/caching/fragments.rb, line 91 91: def read_fragment(key, options = nil) 92: return unless cache_configured? 93: 94: key = fragment_cache_key(key) 95: 96: self.class.benchmark "Cached fragment hit: #{key}" do 97: cache_store.read(key, options) 98: end 99: end
Writes content to the location signified by key (see expire_fragment for acceptable formats)
# File vendor/rails/actionpack/lib/action_controller/caching/fragments.rb, line 78 78: def write_fragment(key, content, options = nil) 79: return unless cache_configured? 80: 81: key = fragment_cache_key(key) 82: 83: self.class.benchmark "Cached fragment miss: #{key}" do 84: cache_store.write(key, content, options) 85: end 86: 87: content 88: end