2008-01-03 16:05:12 -05:00
|
|
|
require 'set'
|
|
|
|
|
|
|
|
module ActionController #:nodoc:
|
|
|
|
module Caching
|
2009-10-29 00:37:29 -04:00
|
|
|
# Action caching is similar to page caching by the fact that the entire
|
|
|
|
# output of the response is cached, but unlike page caching, every
|
2010-12-08 15:20:53 -05:00
|
|
|
# request still goes through Action Pack. The key benefit of this is
|
|
|
|
# that filters run before the cache is served, which allows for
|
|
|
|
# authentication and other restrictions on whether someone is allowed
|
|
|
|
# to execute such action. Example:
|
2008-01-03 16:05:12 -05:00
|
|
|
#
|
|
|
|
# class ListsController < ApplicationController
|
|
|
|
# before_filter :authenticate, :except => :public
|
2010-12-08 15:20:53 -05:00
|
|
|
#
|
2008-01-03 16:05:12 -05:00
|
|
|
# caches_page :public
|
2010-12-08 15:20:53 -05:00
|
|
|
# caches_action :index, :show
|
2008-01-03 16:05:12 -05:00
|
|
|
# end
|
|
|
|
#
|
2010-12-08 15:20:53 -05:00
|
|
|
# In this example, the +public+ action doesn't require authentication
|
|
|
|
# so it's possible to use the faster page caching. On the other hand
|
|
|
|
# +index+ and +show+ require authentication. They can still be cached,
|
|
|
|
# but we need action caching for them.
|
2008-01-03 16:05:12 -05:00
|
|
|
#
|
2010-12-08 15:20:53 -05:00
|
|
|
# Action caching uses fragment caching internally and an around
|
|
|
|
# filter to do the job. The fragment cache is named according to
|
|
|
|
# the host and path of the request. A page that is accessed at
|
2010-08-26 15:58:57 -04:00
|
|
|
# <tt>http://david.example.com/lists/show/1</tt> will result in a fragment named
|
|
|
|
# <tt>david.example.com/lists/show/1</tt>. This allows the cacher to
|
|
|
|
# differentiate between <tt>david.example.com/lists/</tt> and
|
|
|
|
# <tt>jamis.example.com/lists/</tt> -- which is a helpful way of assisting
|
2009-10-29 00:37:29 -04:00
|
|
|
# the subdomain-as-account-key pattern.
|
2008-01-03 16:05:12 -05:00
|
|
|
#
|
2009-10-29 00:37:29 -04:00
|
|
|
# Different representations of the same resource, e.g.
|
2010-08-26 15:58:57 -04:00
|
|
|
# <tt>http://david.example.com/lists</tt> and
|
|
|
|
# <tt>http://david.example.com/lists.xml</tt>
|
2009-10-29 00:37:29 -04:00
|
|
|
# are treated like separate requests and so are cached separately.
|
|
|
|
# Keep in mind when expiring an action cache that
|
|
|
|
# <tt>:action => 'lists'</tt> is not the same as
|
2009-10-28 16:53:34 -04:00
|
|
|
# <tt>:action => 'list', :format => :xml</tt>.
|
2008-01-03 16:05:12 -05:00
|
|
|
#
|
2009-10-29 00:37:29 -04:00
|
|
|
# You can set modify the default action cache path by passing a
|
2010-12-08 15:20:53 -05:00
|
|
|
# <tt>:cache_path</tt> option. This will be passed directly to
|
|
|
|
# <tt>ActionCachePath.path_for</tt>. This is handy for actions with
|
|
|
|
# multiple possible routes that should be cached differently. If a
|
|
|
|
# block is given, it is called with the current controller instance.
|
|
|
|
#
|
|
|
|
# And you can also use <tt>:if</tt> (or <tt>:unless</tt>) to pass a
|
|
|
|
# proc that specifies when the action should be cached.
|
2008-01-03 16:05:12 -05:00
|
|
|
#
|
2010-12-08 15:20:53 -05:00
|
|
|
# Finally, if you are using memcached, you can also pass <tt>:expires_in</tt>.
|
2008-06-14 04:45:32 -04:00
|
|
|
#
|
2010-12-08 15:20:53 -05:00
|
|
|
# The following example depicts some of the points made above:
|
2008-05-14 13:33:54 -04:00
|
|
|
#
|
2008-01-03 16:05:12 -05:00
|
|
|
# class ListsController < ApplicationController
|
|
|
|
# before_filter :authenticate, :except => :public
|
2010-12-08 15:20:53 -05:00
|
|
|
#
|
|
|
|
# caches_page :public
|
|
|
|
#
|
2011-03-25 20:34:05 -04:00
|
|
|
# caches_action :index, :if => proc do
|
|
|
|
# !request.format.json? # cache if is not a JSON request
|
2009-10-29 00:37:29 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# caches_action :show, :cache_path => { :project => 1 },
|
|
|
|
# :expires_in => 1.hour
|
|
|
|
#
|
2011-03-25 20:34:05 -04:00
|
|
|
# caches_action :feed, :cache_path => proc do
|
|
|
|
# if params[:user_id]
|
|
|
|
# user_list_url(params[:user_id, params[:id])
|
2009-10-29 00:37:29 -04:00
|
|
|
# else
|
2011-03-25 20:34:05 -04:00
|
|
|
# list_url(params[:id])
|
2009-10-29 00:37:29 -04:00
|
|
|
# end
|
|
|
|
# end
|
2008-01-03 16:05:12 -05:00
|
|
|
# end
|
2008-05-14 13:33:54 -04:00
|
|
|
#
|
2010-12-08 15:20:53 -05:00
|
|
|
# If you pass <tt>:layout => false</tt>, it will only cache your action
|
|
|
|
# content. That's useful when your layout has dynamic information.
|
|
|
|
#
|
|
|
|
# Warning: If the format of the request is determined by the Accept HTTP
|
|
|
|
# header the Content-Type of the cached response could be wrong because
|
|
|
|
# no information about the MIME type is stored in the cache key. So, if
|
|
|
|
# you first ask for MIME type M in the Accept header, a cache entry is
|
2011-03-04 00:09:26 -05:00
|
|
|
# created, and then perform a second request to the same resource asking
|
2010-12-08 15:20:53 -05:00
|
|
|
# for a different MIME type, you'd get the content cached for M.
|
2008-06-03 15:02:51 -04:00
|
|
|
#
|
2010-12-08 15:20:53 -05:00
|
|
|
# The <tt>:format</tt> parameter is taken into account though. The safest
|
|
|
|
# way to cache by MIME type is to pass the format in the route.
|
2008-01-03 16:05:12 -05:00
|
|
|
module Actions
|
2009-10-28 16:53:34 -04:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2008-01-03 16:05:12 -05:00
|
|
|
module ClassMethods
|
|
|
|
# Declares that +actions+ should be cached.
|
|
|
|
# See ActionController::Caching::Actions for details.
|
|
|
|
def caches_action(*actions)
|
|
|
|
return unless cache_configured?
|
2008-05-14 13:33:54 -04:00
|
|
|
options = actions.extract_options!
|
2010-03-18 17:55:13 -04:00
|
|
|
options[:layout] = true unless options.key?(:layout)
|
2009-10-28 16:53:34 -04:00
|
|
|
filter_options = options.extract!(:if, :unless).merge(:only => actions)
|
|
|
|
cache_options = options.extract!(:layout, :cache_path).merge(:store_options => options)
|
2008-06-14 04:45:32 -04:00
|
|
|
|
2009-10-28 16:53:34 -04:00
|
|
|
around_filter ActionCacheFilter.new(cache_options), filter_options
|
2008-01-03 16:05:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-18 17:55:13 -04:00
|
|
|
def _save_fragment(name, options)
|
|
|
|
content = response_body
|
2010-03-19 21:56:06 -04:00
|
|
|
content = content.join if content.is_a?(Array)
|
|
|
|
|
2011-02-27 09:57:46 -05:00
|
|
|
if caching_allowed?
|
|
|
|
write_fragment(name, content, options)
|
|
|
|
else
|
|
|
|
content
|
|
|
|
end
|
2009-10-28 16:53:34 -04:00
|
|
|
end
|
|
|
|
|
2009-10-29 00:37:29 -04:00
|
|
|
protected
|
|
|
|
def expire_action(options = {})
|
|
|
|
return unless cache_configured?
|
2008-01-03 16:05:12 -05:00
|
|
|
|
2009-10-29 00:37:29 -04:00
|
|
|
actions = options[:action]
|
|
|
|
if actions.is_a?(Array)
|
|
|
|
actions.each {|action| expire_action(options.merge(:action => action)) }
|
|
|
|
else
|
|
|
|
expire_fragment(ActionCachePath.new(self, options, false).path)
|
2008-01-03 16:05:12 -05:00
|
|
|
end
|
2009-10-29 00:37:29 -04:00
|
|
|
end
|
2008-01-03 16:05:12 -05:00
|
|
|
|
|
|
|
class ActionCacheFilter #:nodoc:
|
2008-05-14 13:33:54 -04:00
|
|
|
def initialize(options, &block)
|
2010-03-18 16:45:50 -04:00
|
|
|
@cache_path, @store_options, @cache_layout =
|
2009-10-28 16:53:34 -04:00
|
|
|
options.values_at(:cache_path, :store_options, :layout)
|
2008-01-03 16:05:12 -05:00
|
|
|
end
|
|
|
|
|
2009-06-17 19:23:11 -04:00
|
|
|
def filter(controller)
|
2009-10-29 00:37:29 -04:00
|
|
|
path_options = if @cache_path.respond_to?(:call)
|
|
|
|
controller.instance_exec(controller, &@cache_path)
|
|
|
|
else
|
|
|
|
@cache_path
|
|
|
|
end
|
|
|
|
|
2009-10-28 16:53:34 -04:00
|
|
|
cache_path = ActionCachePath.new(controller, path_options || {})
|
2009-05-19 21:05:16 -04:00
|
|
|
|
2010-03-18 17:55:13 -04:00
|
|
|
body = controller.read_fragment(cache_path.path, @store_options)
|
|
|
|
|
|
|
|
unless body
|
|
|
|
controller.action_has_layout = false unless @cache_layout
|
2009-10-28 16:53:34 -04:00
|
|
|
yield
|
2010-03-18 17:55:13 -04:00
|
|
|
controller.action_has_layout = true
|
|
|
|
body = controller._save_fragment(cache_path.path, @store_options)
|
2008-01-03 16:05:12 -05:00
|
|
|
end
|
2010-03-18 17:55:13 -04:00
|
|
|
|
2010-04-25 09:11:41 -04:00
|
|
|
body = controller.render_to_string(:text => body, :layout => true) unless @cache_layout
|
2010-03-18 17:55:13 -04:00
|
|
|
|
|
|
|
controller.response_body = body
|
|
|
|
controller.content_type = Mime[cache_path.extension || :html]
|
2008-01-03 16:05:12 -05:00
|
|
|
end
|
|
|
|
end
|
2008-05-14 13:33:54 -04:00
|
|
|
|
2008-01-03 16:05:12 -05:00
|
|
|
class ActionCachePath
|
|
|
|
attr_reader :path, :extension
|
2008-05-14 13:33:54 -04:00
|
|
|
|
2009-10-28 16:53:34 -04:00
|
|
|
# If +infer_extension+ is true, the cache path extension is looked up from the request's
|
2010-08-26 15:58:57 -04:00
|
|
|
# path and format. This is desirable when reading and writing the cache, but not when
|
2009-10-28 16:53:34 -04:00
|
|
|
# expiring the cache - expire_action should expire the same files regardless of the
|
|
|
|
# request format.
|
2009-03-10 19:02:08 -04:00
|
|
|
def initialize(controller, options = {}, infer_extension = true)
|
|
|
|
if infer_extension
|
2009-10-29 00:37:29 -04:00
|
|
|
@extension = controller.params[:format]
|
|
|
|
options.reverse_merge!(:format => @extension) if options.is_a?(Hash)
|
2008-03-06 06:50:44 -05:00
|
|
|
end
|
2009-03-10 19:02:08 -04:00
|
|
|
|
2009-10-29 00:37:29 -04:00
|
|
|
path = controller.url_for(options).split(%r{://}).last
|
|
|
|
@path = normalize!(path)
|
2008-01-03 16:05:12 -05:00
|
|
|
end
|
2008-05-14 13:33:54 -04:00
|
|
|
|
2009-10-28 16:53:34 -04:00
|
|
|
private
|
|
|
|
def normalize!(path)
|
|
|
|
path << 'index' if path[-1] == ?/
|
|
|
|
path << ".#{extension}" if extension and !path.ends_with?(extension)
|
2010-09-27 19:57:26 -04:00
|
|
|
URI.parser.unescape(path)
|
2010-09-22 15:11:15 -04:00
|
|
|
end
|
2008-01-03 16:05:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-05-14 13:33:54 -04:00
|
|
|
end
|