tl; dr
下面的expire_index
方法被调用,我在日志中看到了puts
。但是,当我刷新时,页面是陈旧版本。
注意:我正在使用Rails_admin来更新模型。但也直接使用Rails控制台注意到了相同的行为。
感谢您的帮助。非常感谢!
详细信息
app/controllers/posts_controller.rb
class PostsController < ApplicationController
caches_action :index
cache_sweeper :post_sweeper
def index
@posts = Post.published
end
def show
@post = Post.find(params[:id])
end
end
app/sweepers/post_sweeper.rb
class PostSweeper < ActionController::Caching::Sweeper
observe Post
def after_save(post)
puts "======================"
puts " AFTER SAVE "
puts "======================"
expire_index
end
private
def expire_index
puts "======================"
puts " EXPIRING INDEX "
puts "======================"
expire_action(:controller => '/posts', :action => 'index')
end
end
config/environments/production.rb
config.action_controller.perform_caching = true
config.cache_store = :dalli_store # using memcachier on heroku
使它起作用。这就是所需要的:
def expire_index
cache_key = "views/#{request.host_with_port}/posts"
Rails.cache.delete(cache_key)
end
有关此要约的更多详细信息 -> https://gist.github.com/4400728