简体   繁体   English

清除Rails中的页面缓存

[英]Clear page cache in Rails

How can I clear a page cache. 如何清除页面缓存。 I tried impelemnting cache but switched it off, now my home page always loads the cached version. 我尝试取消缓存,但是将其关闭,现在我的主页始终加载缓存的版本。 I tried the following rake task no luck. 我尝试了以下rake任务,不走运。

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

Here is my home page code, it shows partials that essentially present collection of other models. 这是我的主页代码,它显示了部分地展示了其他模型的集合。 Does this have something to do with it? 这有关系吗?

<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 %>

I even tried to expire the cache from a sweeper, 我什至试图使清扫器的缓存失效,

in my vote_topic controller I have 在我的表决主题控制器中

cache_sweeper :home_sweeper 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

That doesn't work either, I do have cache turned off in my development.rb file. 那也不起作用,我的development.rb文件中确实关闭了缓存。

The page cache is always on disk, so you will need to actually clear out the files/directory you want to flush. 页面缓存始终位于磁盘上,因此您实际上需要清除要刷新的文件/目录。 It's an unfortunate way to cache. 这是一种不幸的缓存方式。

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

I had a similar problem. 我有一个类似的问题。 When using memcached, this works: 使用memcached时,此方法有效:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM