Skip to content

Commit

Permalink
Allow caches_action to accept cache store options. [#416 state:resolved]
Browse files Browse the repository at this point in the history
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
josevalim authored and lifo committed Jul 4, 2008
1 parent 87fbcaa commit bad1eac
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
4 changes: 4 additions & 0 deletions actionpack/CHANGELOG
@@ -1,5 +1,9 @@
*Edge*

* Allow caches_action to accept cache store options. #416. [José Valim]. Example:

caches_action :index, :redirected, :if => Proc.new { |c| !c.request.format.json? }, :expires_in => 1.hour

* Remove define_javascript_functions, javascript_include_tag and friends are far superior. [Michael Koziarski]

* Deprecate :use_full_path render option. The supplying the option no longer has an effect [Josh Peek]
Expand Down
18 changes: 11 additions & 7 deletions actionpack/lib/action_controller/caching/actions.rb
Expand Up @@ -27,13 +27,15 @@ module Caching
# You can set modify the default action cache path by passing a :cache_path option. This will be passed directly to ActionCachePath.path_for. 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 :if to pass a Proc that specifies when the action should be cached.
# And you can also use :if (or :unless) to pass a Proc that specifies when the action should be cached.
#
# Finally, if you are using memcached, you can also pass :expires_in.
#
# class ListsController < ApplicationController
# before_filter :authenticate, :except => :public
# caches_page :public
# caches_action :index, :if => Proc.new { |c| !c.request.format.json? } # cache if is not a JSON request
# caches_action :show, :cache_path => { :project => 1 }
# caches_action :show, :cache_path => { :project => 1 }, :expires_in => 1.hour
# caches_action :feed, :cache_path => Proc.new { |controller|
# controller.params[:user_id] ?
# controller.send(:user_list_url, c.params[:user_id], c.params[:id]) :
Expand All @@ -56,8 +58,10 @@ module ClassMethods
def caches_action(*actions)
return unless cache_configured?
options = actions.extract_options!
cache_filter = ActionCacheFilter.new(:layout => options.delete(:layout), :cache_path => options.delete(:cache_path))
around_filter(cache_filter, {:only => actions}.merge(options))
filter_options = { :only => actions, :if => options.delete(:if), :unless => options.delete(:unless) }

cache_filter = ActionCacheFilter.new(:layout => options.delete(:layout), :cache_path => options.delete(:cache_path), :store_options => options)
around_filter(cache_filter, filter_options)
end
end

Expand All @@ -80,8 +84,8 @@ def initialize(options, &block)
end

def before(controller)
cache_path = ActionCachePath.new(controller, path_options_for(controller, @options))
if cache = controller.read_fragment(cache_path.path)
cache_path = ActionCachePath.new(controller, path_options_for(controller, @options.slice(:cache_path)))
if cache = controller.read_fragment(cache_path.path, @options[:store_options])
controller.rendered_action_cache = true
set_content_type!(controller, cache_path.extension)
options = { :text => cache }
Expand All @@ -96,7 +100,7 @@ def before(controller)
def after(controller)
return if controller.rendered_action_cache || !caching_allowed(controller)
action_content = cache_layout? ? content_for_layout(controller) : controller.response.body
controller.write_fragment(controller.action_cache_path.path, action_content)
controller.write_fragment(controller.action_cache_path.path, action_content, @options[:store_options])
end

private
Expand Down
10 changes: 9 additions & 1 deletion actionpack/test/controller/caching_test.rb
Expand Up @@ -152,7 +152,7 @@ def page_cached?(action)


class ActionCachingTestController < ActionController::Base
caches_action :index, :redirected, :forbidden, :if => Proc.new { |c| !c.request.format.json? }
caches_action :index, :redirected, :forbidden, :if => Proc.new { |c| !c.request.format.json? }, :expires_in => 1.hour
caches_action :show, :cache_path => 'http://test.host/custom/show'
caches_action :edit, :cache_path => Proc.new { |c| c.params[:id] ? "http://test.host/#{c.params[:id]};edit" : "http://test.host/edit" }
caches_action :with_layout
Expand Down Expand Up @@ -188,6 +188,7 @@ def expire
expire_action :controller => 'action_caching_test', :action => 'index'
render :nothing => true
end

def expire_xml
expire_action :controller => 'action_caching_test', :action => 'index', :format => 'xml'
render :nothing => true
Expand Down Expand Up @@ -289,6 +290,13 @@ def test_action_cache_conditional_options
assert !fragment_exist?('hostname.com/action_caching_test')
end

def test_action_cache_with_store_options
MockTime.expects(:now).returns(12345).once
@controller.expects(:read_fragment).with('hostname.com/action_caching_test', :expires_in => 1.hour).once
@controller.expects(:write_fragment).with('hostname.com/action_caching_test', '12345.0', :expires_in => 1.hour).once
get :index
end

def test_action_cache_with_custom_cache_path
get :show
cached_time = content_to_cache
Expand Down

0 comments on commit bad1eac

Please sign in to comment.