在Rails中清除页面缓存

时间:2010-07-15 08:51:02

标签: ruby-on-rails

如何清除页面缓存。我尝试阻止缓存但关闭它,现在我的主页总是加载缓存版本。我尝试了以下rake任务,但没有运气。

namespace :cache_clear do
    desc 'clear a page cache'
    task :expire_cache => :environment do
        ActionController::Base::expire_page('/')
        puts "Cache cleared"
    end
end

这是我的主页代码,它显示了基本上呈现其他模型集合的部分内容。这与它有关吗?

<div class="span-17 last">
<%= render :partial => "top_votes" %>
</div>

<% content_for :right_nav do %>
  <%= render :partial => "latest_votes" %>
  <%= render :partial => "whos_voting" %>
  <%= render :partial => "top_voters" %>

<% end %>

我甚至试图使扫描程序使缓存失效,

在我的vote_topic控制器中我有

cache_sweeper:home_sweeper

class HomeSweeper < ActionController::Caching::Sweeper
    observe VoteTopic
  def after_index(vote_topic)
    expire_cache(vote_topic)
  end

  def expire_cache(vote_topic)
     expire_page :controller => :home, :action => :index
  end
end

这也不起作用,我在我的development.rb文件中关闭了缓存。

2 个答案:

答案 0 :(得分:3)

页面缓存始终位于磁盘上,因此您需要实际清除要刷新的文件/目录。这是一种不幸的缓存方式。

 cache_dir = ActionController::Base.page_cache_directory
 unless cache_dir == RAILS_ROOT+"/public"
   FileUtils.rm_r(Dir.glob(cache_dir+"/*")) rescue Errno::ENOENT
 end

答案 1 :(得分:2)

我有类似的问题。使用memcached时,这有效:

desc "Expire page cache"
task :expire_pages => :environment do
  ActionController::Base::expire_page("/")
  Rails.logger.info("Removed page cache")
end