Skip to content

Instantly share code, notes, and snippets.

@renancarvalhoo
Forked from marshluca/caches_action.rb
Last active July 13, 2018 19:31
Show Gist options
  • Save renancarvalhoo/b775a370df1881ae4eab9e4d01efa335 to your computer and use it in GitHub Desktop.
Save renancarvalhoo/b775a370df1881ae4eab9e4d01efa335 to your computer and use it in GitHub Desktop.
Rails Action Cache
class ListsController < ApplicationController
before_filter :authenticate, :except => :public
caches_page :public
caches_action :index, :if => proc do
!request.format.json? # cache if is not a JSON request
end
caches_action :show, :cache_path => { :project => 1 },
:expires_in => 1.hour
caches_action :feed, :cache_path => proc do
if params[:user_id]
user_list_url(params[:user_id, params[:id])
else
list_url(params[:id])
end
end
end
# http://api.rubyonrails.org/classes/ActionController/Caching/Actions.html
class ListsController < ApplicationController
before_filter :authenticate, :except => :public
caches_page :public
caches_action :index, :if => proc do
!request.format.json? # cache if is not a JSON request
end
caches_action :show, :cache_path => { :project => 1 },
:expires_in => 1.hour, :layout => false
caches_action :feed, :cache_path => proc do
if params[:user_id]
user_list_url(params[:user_id, params[:id])
else
list_url(params[:id])
end
end
end
# http://api.rubyonrails.org/classes/ActionController/Caching/Actions.html
caches_action :my_action, :cache_path => Proc.new { |controller| controller.params }
caches_action :action_one, :action_two,
:cache_path => Proc.new { |c| c.params.delete_if { |k,v| k.starts_with?('utm_') } },
:expires_in => 4.hours,
:unless => Proc.new { |c| c.request.xml_http_request? || c.send(:current_user).try(:admin?) }
References:
http://guides.rubyonrails.org/caching_with_rails.html
http://cobaltedge.com/rails-action-caching-with-query-parameters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment