简体   繁体   中英

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.

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

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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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