public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Allow caches_action to accept cache store options. [#416 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
josevalim (author)
Sat Jun 14 01:45:32 -0700 2008
lifo (committer)
Thu Jul 03 18:00:51 -0700 2008
commit  bad1eac91d1549631dca8e93e7e846911649acf7
tree    2ee30ac1ac7451d9a0a40c02481d13f74f3fc9d4
parent  87fbcaa6229e9073095fb8d77c7a536c9466fbce
...
1
2
 
 
 
 
3
4
5
...
1
2
3
4
5
6
7
8
9
0
@@ -1,5 +1,9 @@
0
 *Edge*
0
 
0
+* Allow caches_action to accept cache store options. #416. [José Valim]. Example:
0
+  
0
+  caches_action :index, :redirected, :if => Proc.new { |c| !c.request.format.json? }, :expires_in => 1.hour
0
+
0
 * Remove define_javascript_functions, javascript_include_tag and friends are far superior. [Michael Koziarski]
0
 
0
 * Deprecate :use_full_path render option. The supplying the option no longer has an effect [Josh Peek]
...
27
28
29
30
 
 
 
31
32
33
34
35
36
 
37
38
39
...
56
57
58
59
60
 
 
 
 
61
62
63
...
80
81
82
83
84
 
 
85
86
87
...
96
97
98
99
 
100
101
102
...
27
28
29
 
30
31
32
33
34
35
36
37
 
38
39
40
41
...
58
59
60
 
 
61
62
63
64
65
66
67
...
84
85
86
 
 
87
88
89
90
91
...
100
101
102
 
103
104
105
106
0
@@ -27,13 +27,15 @@ module ActionController #:nodoc:
0
     # 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
0
     # for actions with multiple possible routes that should be cached differently.  If a block is given, it is called with the current controller instance.
0
     #
0
-    # And you can also use :if to pass a Proc that specifies when the action should be cached.
0
+    # And you can also use :if (or :unless) to pass a Proc that specifies when the action should be cached.
0
+    #
0
+    # Finally, if you are using memcached, you can also pass :expires_in.
0
     #
0
     #   class ListsController < ApplicationController
0
     #     before_filter :authenticate, :except => :public
0
     #     caches_page   :public
0
     #     caches_action :index, :if => Proc.new { |c| !c.request.format.json? } # cache if is not a JSON request
0
-    #     caches_action :show, :cache_path => { :project => 1 }
0
+    #     caches_action :show, :cache_path => { :project => 1 }, :expires_in => 1.hour
0
     #     caches_action :feed, :cache_path => Proc.new { |controller|
0
     #       controller.params[:user_id] ?
0
     #         controller.send(:user_list_url, c.params[:user_id], c.params[:id]) :
0
@@ -56,8 +58,10 @@ module ActionController #:nodoc:
0
         def caches_action(*actions)
0
           return unless cache_configured?
0
           options = actions.extract_options!
0
-          cache_filter = ActionCacheFilter.new(:layout => options.delete(:layout), :cache_path => options.delete(:cache_path))
0
-          around_filter(cache_filter, {:only => actions}.merge(options))
0
+          filter_options = { :only => actions, :if => options.delete(:if), :unless => options.delete(:unless) }
0
+
0
+          cache_filter = ActionCacheFilter.new(:layout => options.delete(:layout), :cache_path => options.delete(:cache_path), :store_options => options)
0
+          around_filter(cache_filter, filter_options)
0
         end
0
       end
0
 
0
@@ -80,8 +84,8 @@ module ActionController #:nodoc:
0
         end
0
 
0
         def before(controller)
0
-          cache_path = ActionCachePath.new(controller, path_options_for(controller, @options))
0
-          if cache = controller.read_fragment(cache_path.path)
0
+          cache_path = ActionCachePath.new(controller, path_options_for(controller, @options.slice(:cache_path)))
0
+          if cache = controller.read_fragment(cache_path.path, @options[:store_options])
0
             controller.rendered_action_cache = true
0
             set_content_type!(controller, cache_path.extension)
0
             options = { :text => cache }
0
@@ -96,7 +100,7 @@ module ActionController #:nodoc:
0
         def after(controller)
0
           return if controller.rendered_action_cache || !caching_allowed(controller)
0
           action_content = cache_layout? ? content_for_layout(controller) : controller.response.body
0
-          controller.write_fragment(controller.action_cache_path.path, action_content)
0
+          controller.write_fragment(controller.action_cache_path.path, action_content, @options[:store_options])
0
         end
0
 
0
         private
...
152
153
154
155
 
156
157
158
...
188
189
190
 
191
192
193
...
289
290
291
 
 
 
 
 
 
 
292
293
294
...
152
153
154
 
155
156
157
158
...
188
189
190
191
192
193
194
...
290
291
292
293
294
295
296
297
298
299
300
301
302
0
@@ -152,7 +152,7 @@ end
0
 
0
 
0
 class ActionCachingTestController < ActionController::Base
0
-  caches_action :index, :redirected, :forbidden, :if => Proc.new { |c| !c.request.format.json? }
0
+  caches_action :index, :redirected, :forbidden, :if => Proc.new { |c| !c.request.format.json? }, :expires_in => 1.hour
0
   caches_action :show, :cache_path => 'http://test.host/custom/show'
0
   caches_action :edit, :cache_path => Proc.new { |c| c.params[:id] ? "http://test.host/#{c.params[:id]};edit" : "http://test.host/edit" }
0
   caches_action :with_layout
0
@@ -188,6 +188,7 @@ class ActionCachingTestController < ActionController::Base
0
     expire_action :controller => 'action_caching_test', :action => 'index'
0
     render :nothing => true
0
   end
0
+
0
   def expire_xml
0
     expire_action :controller => 'action_caching_test', :action => 'index', :format => 'xml'
0
     render :nothing => true
0
@@ -289,6 +290,13 @@ class ActionCacheTest < Test::Unit::TestCase
0
     assert !fragment_exist?('hostname.com/action_caching_test')
0
   end
0
 
0
+  def test_action_cache_with_store_options
0
+    MockTime.expects(:now).returns(12345).once
0
+    @controller.expects(:read_fragment).with('hostname.com/action_caching_test', :expires_in => 1.hour).once
0
+    @controller.expects(:write_fragment).with('hostname.com/action_caching_test', '12345.0', :expires_in => 1.hour).once
0
+    get :index
0
+  end
0
+
0
   def test_action_cache_with_custom_cache_path
0
     get :show
0
     cached_time = content_to_cache

Comments