Reputation: 1884
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.
Upvotes: 1
Views: 4762
Reputation: 1830
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
Upvotes: 2
Reputation: 19145
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
Upvotes: 3