-
-
Save renancarvalhoo/b775a370df1881ae4eab9e4d01efa335 to your computer and use it in GitHub Desktop.
Rails Action Cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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