Skip to content

Allow cache to be re-generated on-demand without first deleting it #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------

Expand Down
4 changes: 3 additions & 1 deletion lib/action_controller/caching/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions test/caching_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ActionCachingTestController < CachingController
caches_action :streaming
caches_action :invalid
caches_action :accept
caches_action :regenerate

layout "talk_from_action"

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -853,6 +859,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"
Expand Down