rails4中使用cache_action, cache_page
访问量: 2989
TODO: 尚未测试。。。不知道是否需要配置 nginx .... 如果是那样的话速度估计会很快。
refer to: https://github.com/rails/actionpack-page_caching
cache_action一直是接口开发的神器, 但是在rails4 中用法略有不同。
1. in your Gemfile:
gem 'actionpack-page_caching'
2 in your controller:
class ItemsController < ApplicationController before_action :authenticate_user!, except: [:index, :show] + caches_page :index, :show def create @item = Item.new(item_params) @item.save + expire_page action: 'index' + expire_page action: 'show', id: @item.id redirect_to edit_item_path(@item), notice: '操作成功' end def update @item.update(item_params) + expire_page action: 'index' + expire_page action: 'show', id: @item.id redirect_to :back, notice: '操作成功' end def destroy @item.destroy + expire_page action: 'index' + expire_page action: 'show', id: @item.id redirect_to :back, notice: '操作成功' end
3. in your config file:
# config/environments/production.rb Rails.application.configure do + config.action_controller.page_cache_directory = "#{Rails.root.to_s}/public/deploy" config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }