Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/trunk/actionpack/lib/action_controller/caching/fragments.rb

Revision 8619, 5.5 kB (checked in by bitsweat, 11 months ago)

Move fragment caching from special helper methods to TemplateHandler. Closes #10754 [Josh Peek]

Line 
1 module ActionController #:nodoc:
2   module Caching
3     # Fragment caching is used for caching various blocks within templates without caching the entire action as a whole. This is useful when
4     # certain elements of an action change frequently or depend on complicated state while other parts rarely change or can be shared amongst multiple
5     # parties. The caching is doing using the cache helper available in the Action View. A template with caching might look something like:
6     #
7     #   <b>Hello <%= @name %></b>
8     #   <% cache do %>
9     #     All the topics in the system:
10     #     <%= render :partial => "topic", :collection => Topic.find(:all) %>
11     #   <% end %>
12     #
13     # 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
14     # be able to invalidate it using <tt>expire_fragment(:controller => "topics", :action => "list")</tt>.
15     #
16     # This default behavior is of limited use if you need to cache multiple fragments per action or if the action itself is cached using
17     # <tt>caches_action</tt>, so we also have the option to qualify the name of the cached fragment with something like:
18     #
19     #   <% cache(:action => "list", :action_suffix => "all_topics") do %>
20     #
21     # 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
22     # 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
23     # cache names that we can refer to when we need to expire the cache.
24     #
25     # The expiration call for this example is:
26     #
27     #   expire_fragment(:controller => "topics", :action => "list", :action_suffix => "all_topics")
28     module Fragments
29       def self.included(base) #:nodoc:
30         base.class_eval do
31           class << self
32             def fragment_cache_store=(store_option) #:nodoc:
33               ActiveSupport::Deprecation.warn('The fragment_cache_store= method is now use cache_store=')
34               self.cache_store = store_option
35             end
36          
37             def fragment_cache_store #:nodoc:
38               ActiveSupport::Deprecation.warn('The fragment_cache_store method is now use cache_store')
39               cache_store
40             end
41           end
42
43           def fragment_cache_store=(store_option) #:nodoc:
44             ActiveSupport::Deprecation.warn('The fragment_cache_store= method is now use cache_store=')
45             self.cache_store = store_option
46           end
47        
48           def fragment_cache_store #:nodoc:
49             ActiveSupport::Deprecation.warn('The fragment_cache_store method is now use cache_store')
50             cache_store
51           end
52         end
53       end
54
55       # Given a key (as described in <tt>expire_fragment</tt>), returns a key suitable for use in reading,
56       # writing, or expiring a cached fragment. If the key is a hash, the generated key is the return
57       # value of url_for on that hash (without the protocol). All keys are prefixed with "views/" and uses
58       # ActiveSupport::Cache.expand_cache_key for the expansion.
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
62
63       def fragment_for(block, name = {}, options = nil) #:nodoc:
64         unless perform_caching then block.call; return end
65
66         buffer = yield
67
68         if cache = read_fragment(name, options)
69           buffer.concat(cache)
70         else
71           pos = buffer.length
72           block.call
73           write_fragment(name, buffer[pos..-1], options)
74         end
75       end
76
77       # Writes <tt>content</tt> to the location signified by <tt>key</tt> (see <tt>expire_fragment</tt> for acceptable formats)
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
89
90       # Reads a cached fragment from the location signified by <tt>key</tt> (see <tt>expire_fragment</tt> for acceptable formats)
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
100
101       # Name can take one of three forms:
102       # * String: This would normally take the form of a path like "pages/45/notes"
103       # * Hash: Is treated as an implicit call to url_for, like { :controller => "pages", :action => "notes", :id => 45 }
104       # * Regexp: Will destroy all the matched fragments, example:
105       #     %r{pages/\d*/notes}
106       #   Ensure you do not specify start and finish in the regex (^$) because
107       #   the actual filename matched looks like ./cache/filename/path.cache
108       #   Regexp expiration is only supported on caches that can iterate over
109       #   all keys (unlike memcached).
110       def expire_fragment(key, options = nil)
111         return unless cache_configured?
112
113         key = key.is_a?(Regexp) ? key : fragment_cache_key(key)
114
115         if key.is_a?(Regexp)
116           self.class.benchmark "Expired fragments matching: #{key.source}" do
117             cache_store.delete_matched(key, options)
118           end
119         else
120           self.class.benchmark "Expired fragment: #{key}" do
121             cache_store.delete(key, options)
122           end
123         end
124       end
125     end
126   end
127 end
Note: See TracBrowser for help on using the browser.