diff --git a/README.md b/README.md index e8eedfe..de5c27c 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,9 @@ class ListsController < ApplicationController # custom cache path with a proc caches_action :history, cache_path: -> { request.domain } + + # use version for cache invalidation + caches_action :show, version: -> { @list.cache_version } # custom cache path with a symbol caches_action :feed, cache_path: :user_cache_path diff --git a/lib/action_controller/caching/actions.rb b/lib/action_controller/caching/actions.rb index 8c0ee84..17b31c8 100644 --- a/lib/action_controller/caching/actions.rb +++ b/lib/action_controller/caching/actions.rb @@ -155,14 +155,15 @@ def around(controller) cache_layout = expand_option(controller, @cache_layout) path_options = expand_option(controller, @cache_path) cache_path = ActionCachePath.new(controller, path_options || {}) + store_options = @store_options.transform_values { |value| expand_option(controller, value) } - body = controller.read_fragment(cache_path.path, @store_options) + body = controller.read_fragment(cache_path.path, store_options) unless body controller.action_has_layout = false unless cache_layout yield controller.action_has_layout = true - body = controller._save_fragment(cache_path.path, @store_options) + body = controller._save_fragment(cache_path.path, store_options) end body = render_to_string(controller, body) unless cache_layout diff --git a/test/caching_test.rb b/test/caching_test.rb index 0805256..0adbaee 100644 --- a/test/caching_test.rb +++ b/test/caching_test.rb @@ -31,7 +31,8 @@ class ActionCachingTestController < CachingController request.params[:format] = :json end - caches_action :index, :redirected, :forbidden, if: ->(c) { c.request.format && !c.request.format.json? }, expires_in: 1.hour + caches_action :index, :redirected, :forbidden, if: ->(c) { c.request.format && !c.request.format.json? } + caches_action :store_options_index, expires_in: 1.hour, version: -> { 1 } caches_action :show, cache_path: "http://test.host/custom/show" caches_action :edit, cache_path: ->(c) { c.params[:id] ? "http://test.host/#{c.params[:id]};edit" : "http://test.host/edit" } caches_action :custom_cache_path, cache_path: CachePath.new @@ -105,6 +106,7 @@ def simple_runtime_error alias_method :layout_false, :with_layout alias_method :with_layout_proc_param, :with_layout alias_method :with_layout_proc_param_no_args, :with_layout + alias_method :store_options_index, :index def expire expire_action controller: "action_caching_test", action: "index" @@ -389,13 +391,13 @@ def test_action_cache_not_url_cache_path def test_action_cache_with_store_options draw do - get "/action_caching_test", to: "action_caching_test#index" + get "/action_caching_test", to: "action_caching_test#store_options_index" end CacheContent.expects(:to_s).returns('12345.0').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 + @controller.expects(:read_fragment).with("hostname.com/action_caching_test", expires_in: 1.hour, version: 1).once + @controller.expects(:write_fragment).with("hostname.com/action_caching_test", "12345.0", expires_in: 1.hour, version: 1).once + get :store_options_index assert_response :success end