无法在Rails上进行expire_action

时间:2012-12-28 07:05:22

标签: ruby-on-rails ruby-on-rails-3 caching sweeper action-caching

TL;博士

我的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

1 个答案:

答案 0 :(得分:3)

让它发挥作用。这是它花了多少时间:

def expire_index
  cache_key = "views/#{request.host_with_port}/posts"
  Rails.cache.delete(cache_key)
end

关于这个要点的更多细节 - &gt; https://gist.github.com/4400728