From ae5d4448951c010462c80ce1b5c18d089a67f0b7 Mon Sep 17 00:00:00 2001 From: Travis Roberts Date: Thu, 21 Sep 2017 13:55:54 -0500 Subject: [PATCH] Allow cache to be re-generated on-demand without first deleting it --- README.md | 4 ++++ lib/action_controller/caching/actions.rb | 4 +++- test/caching_test.rb | 22 ++++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8d1c10c..d211b4c 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,10 @@ indicates a HTML request the fragment is stored without the extension but if an explicit `"html"` is passed in `:format` then that _is_ used for storing the fragment. +If you would like your cache to be regenerated on-demand without +first clearing it, you can append a `cache=force` param to your +URL (ex: `http://mysite.com/videos/list?cache=force`). + Contributing ------------ diff --git a/lib/action_controller/caching/actions.rb b/lib/action_controller/caching/actions.rb index 0127d61..59886f7 100644 --- a/lib/action_controller/caching/actions.rb +++ b/lib/action_controller/caching/actions.rb @@ -156,7 +156,9 @@ def around(controller) path_options = expand_option(controller, @cache_path) cache_path = ActionCachePath.new(controller, path_options || {}) - body = controller.read_fragment(cache_path.path, @store_options) + unless controller.params[:cache] == 'force' + body = controller.read_fragment(cache_path.path, @store_options) + end unless body controller.action_has_layout = false unless cache_layout diff --git a/test/caching_test.rb b/test/caching_test.rb index 49b3d3b..9baff99 100644 --- a/test/caching_test.rb +++ b/test/caching_test.rb @@ -48,6 +48,7 @@ class ActionCachingTestController < CachingController caches_action :streaming caches_action :invalid caches_action :accept + caches_action :regenerate layout "talk_from_action" @@ -142,6 +143,11 @@ def accept end end + def regenerate + @cache_this = MockTime.now.to_f.to_s + render plain: @cache_this + end + def expire_accept if params.key?(:format) expire_action action: "accept", format: params[:format] @@ -839,6 +845,22 @@ def test_lambda_arity_with_layout assert_equal cached_time, read_fragment("hostname.com/action_caching_test/with_layout_proc_param_no_args") end + def test_regenerate_without_expiring + draw do + get "/action_caching_test/regenerate", to: "action_caching_test#regenerate" + end + + get :regenerate + cached_time = content_to_cache + assert_equal cached_time, @response.body + assert fragment_exist?("hostname.com/action_caching_test/regenerate") + + get :regenerate, params: { cache: 'force' } + updated_cached_time = content_to_cache + assert_equal updated_cached_time, @response.body + assert_equal read_fragment("hostname.com/action_caching_test/regenerate"), updated_cached_time + end + private def get_html(*args) @request.accept = "text/html"