From 7a45d6b2c3d90691bbe48eb0187f5806e4f872ed Mon Sep 17 00:00:00 2001 From: Bogdan Guban Date: Mon, 21 Oct 2019 19:03:41 +0300 Subject: [PATCH 1/5] versions were added. --- README.md | 2 ++ lib/action_controller/caching/actions.rb | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e8eedfe..d851112 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,8 @@ class ListsController < ApplicationController # custom cache path with a proc caches_action :history, cache_path: -> { request.domain } + + 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..2f3bbb0 100644 --- a/lib/action_controller/caching/actions.rb +++ b/lib/action_controller/caching/actions.rb @@ -111,7 +111,7 @@ def caches_action(*actions) options = actions.extract_options! options[:layout] = true unless options.key?(:layout) filter_options = options.extract!(:if, :unless).merge(only: actions) - cache_options = options.extract!(:layout, :cache_path).merge(store_options: options) + cache_options = options.extract!(:layout, :cache_path, :version).merge(store_options: options) around_action ActionCacheFilter.new(cache_options), filter_options end @@ -147,15 +147,18 @@ def expire_action(options = {}) class ActionCacheFilter # :nodoc: def initialize(options, &block) - @cache_path, @store_options, @cache_layout = - options.values_at(:cache_path, :store_options, :layout) + @cache_path, @store_options, @cache_layout, @version = + options.values_at(:cache_path, :store_options, :layout, :version) end def around(controller) cache_layout = expand_option(controller, @cache_layout) path_options = expand_option(controller, @cache_path) + version = expand_option(controller, @version) cache_path = ActionCachePath.new(controller, path_options || {}) + @store_options.merge!(version: version) + body = controller.read_fragment(cache_path.path, @store_options) unless body From fe30c5f16a1775aea89679d85c59363a0e651838 Mon Sep 17 00:00:00 2001 From: Bogdan Guban Date: Thu, 19 Dec 2019 14:24:24 +0200 Subject: [PATCH 2/5] small fixes --- README.md | 1 + lib/action_controller/caching/actions.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d851112..de5c27c 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,7 @@ 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 diff --git a/lib/action_controller/caching/actions.rb b/lib/action_controller/caching/actions.rb index 2f3bbb0..5e97b82 100644 --- a/lib/action_controller/caching/actions.rb +++ b/lib/action_controller/caching/actions.rb @@ -157,7 +157,7 @@ def around(controller) version = expand_option(controller, @version) cache_path = ActionCachePath.new(controller, path_options || {}) - @store_options.merge!(version: version) + @store_options.merge!(version: version) if version body = controller.read_fragment(cache_path.path, @store_options) From dcb99a7e6ac42ad77a032bb233a08a6ede43b9e3 Mon Sep 17 00:00:00 2001 From: Bogdan Guban Date: Thu, 19 Dec 2019 15:54:30 +0200 Subject: [PATCH 3/5] a little another way --- lib/action_controller/caching/actions.rb | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/action_controller/caching/actions.rb b/lib/action_controller/caching/actions.rb index 5e97b82..17b31c8 100644 --- a/lib/action_controller/caching/actions.rb +++ b/lib/action_controller/caching/actions.rb @@ -111,7 +111,7 @@ def caches_action(*actions) options = actions.extract_options! options[:layout] = true unless options.key?(:layout) filter_options = options.extract!(:if, :unless).merge(only: actions) - cache_options = options.extract!(:layout, :cache_path, :version).merge(store_options: options) + cache_options = options.extract!(:layout, :cache_path).merge(store_options: options) around_action ActionCacheFilter.new(cache_options), filter_options end @@ -147,25 +147,23 @@ def expire_action(options = {}) class ActionCacheFilter # :nodoc: def initialize(options, &block) - @cache_path, @store_options, @cache_layout, @version = - options.values_at(:cache_path, :store_options, :layout, :version) + @cache_path, @store_options, @cache_layout = + options.values_at(:cache_path, :store_options, :layout) end def around(controller) cache_layout = expand_option(controller, @cache_layout) path_options = expand_option(controller, @cache_path) - version = expand_option(controller, @version) cache_path = ActionCachePath.new(controller, path_options || {}) + store_options = @store_options.transform_values { |value| expand_option(controller, value) } - @store_options.merge!(version: version) if version - - 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 From 21a30d23fc5694673eaf13866f3edbddfd074544 Mon Sep 17 00:00:00 2001 From: Bogdan Guban Date: Thu, 19 Dec 2019 16:01:30 +0200 Subject: [PATCH 4/5] test fixes --- test/caching_test.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/caching_test.rb b/test/caching_test.rb index b2f7c5f..38eab01 100644 --- a/test/caching_test.rb +++ b/test/caching_test.rb @@ -585,7 +585,7 @@ def test_xml_version_of_resource_is_treated_as_different_cache get :index, format: "xml" assert_response :success assert_equal cached_time, @response.body - assert_equal "application/xml", @response.content_type + assert_match "application/xml", @response.content_type get :expire_xml assert_response :success @@ -604,7 +604,7 @@ def test_correct_content_type_is_returned_for_cache_hit get :index, params: { id: "content-type" }, format: "xml" get :index, params: { id: "content-type" }, format: "xml" assert_response :success - assert_equal "application/xml", @response.content_type + assert_match "application/xml", @response.content_type end def test_correct_content_type_is_returned_for_cache_hit_on_action_with_string_key @@ -616,7 +616,7 @@ def test_correct_content_type_is_returned_for_cache_hit_on_action_with_string_ke get :show, format: "xml" get :show, format: "xml" assert_response :success - assert_equal "application/xml", @response.content_type + assert_match "application/xml", @response.content_type end def test_correct_content_type_is_returned_for_cache_hit_on_action_with_string_key_from_proc @@ -628,7 +628,7 @@ def test_correct_content_type_is_returned_for_cache_hit_on_action_with_string_ke get :edit, params: { id: 1 }, format: "xml" get :edit, params: { id: 1 }, format: "xml" assert_response :success - assert_equal "application/xml", @response.content_type + assert_match "application/xml", @response.content_type end def test_empty_path_is_normalized @@ -854,13 +854,13 @@ def get_json(*args) def assert_cached(cache_time, content_type = "text/html") assert_response :success assert_equal cache_time, @response.body - assert_equal content_type, @response.content_type + assert_match content_type, @response.content_type end def assert_not_cached(cache_time, content_type = "text/html") assert_response :success assert_not_equal cache_time, @response.body - assert_equal content_type, @response.content_type + assert_match content_type, @response.content_type end def content_to_cache From f02420089c8b6bf21f4e7f89efec657054349843 Mon Sep 17 00:00:00 2001 From: Bogdan Guban Date: Thu, 19 Dec 2019 16:43:44 +0200 Subject: [PATCH 5/5] tests coverage --- test/caching_test.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/test/caching_test.rb b/test/caching_test.rb index 38eab01..68d7301 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