Page and action caching help your website load faster by saving copies of pages or actions. This means the server can quickly show saved content instead of making it again every time.
Page and action caching in Ruby on Rails
caches_page :index caches_action :show
caches_page saves the entire HTML output of a page.
caches_action saves the result of a controller action, which can be more flexible.
class ProductsController < ApplicationController
caches_page :index
endclass ArticlesController < ApplicationController
caches_action :show
endclass HomeController < ApplicationController
caches_page :welcome
caches_action :dashboard
endThis example caches the full HTML of the posts index page. When users visit again, Rails serves the saved page quickly without running the index code again.
class PostsController < ApplicationController caches_page :index def index @posts = Post.all end end
Page caching works best for static content but is less flexible if you need to show different content to different users.
Action caching allows filters and authentication to run before serving cached content.
Remember to expire caches when content changes, so users see fresh data.
Page caching saves the full HTML page for fast loading.
Action caching saves the controller action output and can run filters before serving.
Use caching to improve speed but manage cache expiration carefully.