Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add a rake task to wipe cache and some condition before wiping it all

  • Loading branch information...
commit 9b938b4e4279f85f915c69ec75d55b05c61a6f11 1 parent 68a0524
Jean-Philippe Boily authored
View
2  app/sweepers/monologue/total_sweeper.rb
@@ -1,5 +1,5 @@
class Monologue::TotalSweeper
def self.wipe_all
- FileUtils.rm_rf(Dir.glob("#{ActionController::Base.page_cache_directory}/*"))
+ FileUtils.rm_rf(Dir.glob("#{ActionController::Base.page_cache_directory}/*")) if ActionController::Base.perform_caching && Monologue::PageCache.enabled && Monologue::PageCache.wipe_enabled && (ActionController::Base.page_cache_directory != Rails.public_path)
end
end
View
26 lib/tasks/monologue_cache.rake
@@ -0,0 +1,26 @@
+namespace :monologue do
+ namespace :cache do
+ desc "Completely wipe Monologue's cache"
+ task :wipe => :environment do
+ puts "********************************************************************************"
+ puts "********** Monologue, your blogging engine: rake edition ************"
+ puts "********************************************************************************"
+ puts " Starting to run 'rake monologue:cache:wipe'... "
+ if ActionController::Base.perform_caching && Monologue::PageCache.enabled && Monologue::PageCache.wipe_enabled && (ActionController::Base.page_cache_directory != Rails.public_path)
+ puts " Starting to wipe cache...hold tight..."
+ Monologue::TotalSweeper.wipe_all
+ puts " \e[#{32}mDone!\e[0m"
+ else
+ msg = " Monologue's cache configuration isn't set correctly to use this wipe task. Doc here: https://github.com/jipiboily/monologue/wiki/Configure-Monologue's-cache"
+ puts "\e[#{31}m#{msg}\e[0m"
+ puts " Current configuration: "
+ puts " ActionController::Base.perform_caching == #{ActionController::Base.perform_caching}"
+ puts " Monologue::PageCache.enabled == #{Monologue::PageCache.enabled}"
+ puts " Monologue::PageCache.wipe_enabled == #{Monologue::PageCache.wipe_enabled}"
+ puts " ActionController::Base.page_cache_directory == #{ActionController::Base.page_cache_directory}"
+ puts " Rails.public_path == #{Rails.public_path}"
+ end
+ puts "********************************************************************************"
+ end
+ end
+end
View
53 spec/sweepers/total_sweeper_spec.rb
@@ -0,0 +1,53 @@
+# encoding: UTF-8
+require 'spec_helper'
+describe "Monologue::TotalSweeper" do
+ before do
+ ActionController::Base.perform_caching = true
+ Monologue::PageCache.enabled = true
+ Monologue::PageCache.wipe_enabled = true
+ ActionController::Base.page_cache_directory = Rails.public_path + "/my-cache-dir"
+
+ @post_1 = Factory(:posts_revision).post
+ @post_2 = Factory(:posts_revision).post
+ FileUtils.mkdir_p("#{ActionController::Base.page_cache_directory}/monologue/post")
+ FileUtils.touch "#{ActionController::Base.page_cache_directory}/monologue/#{@post_1.active_revision.url}.html"
+ FileUtils.touch "#{ActionController::Base.page_cache_directory}/monologue/#{@post_2.active_revision.url}.html"
+ end
+
+ after do
+ FileUtils.rm_rf(Dir.glob("#{Rails.public_path}/my-cache-dir/*"))
+ FileUtils.rm_rf(Dir.glob("#{Rails.public_path}/monologue/*"))
+ end
+
+ it "wipe if all config is set correctly" do
+ Monologue::TotalSweeper.wipe_all
+ Dir["#{ActionController::Base.page_cache_directory}/**/*"].length.should eq 0
+ end
+
+ it "do NOT wipe unless ActionController::Base.perform_caching" do
+ ActionController::Base.perform_caching = false
+ Monologue::TotalSweeper.wipe_all
+ Dir["#{ActionController::Base.page_cache_directory}/**/*"].length.should eq 4
+ end
+
+ it "do NOT wipe unless Monologue::PageCache.enabled" do
+ ActionController::Base.perform_caching = false
+ Monologue::TotalSweeper.wipe_all
+ Dir["#{ActionController::Base.page_cache_directory}/**/*"].length.should eq 4
+ end
+
+ it "do NOT wipe if public ActionController::Base.page_cache_directory == Rails.public_path" do
+ ActionController::Base.page_cache_directory = Rails.public_path
+ FileUtils.mkdir_p("#{ActionController::Base.page_cache_directory}/monologue/post")
+ FileUtils.touch "#{ActionController::Base.page_cache_directory}/monologue/#{@post_1.active_revision.url}.html"
+ FileUtils.touch "#{ActionController::Base.page_cache_directory}/monologue/#{@post_2.active_revision.url}.html"
+ Monologue::TotalSweeper.wipe_all
+ Dir["#{ActionController::Base.page_cache_directory}/monologue/**/*"].length.should eq 3
+ end
+
+ it "do NOT wipe unless Monologue::PageCache.wipe_enabled" do
+ Monologue::PageCache.wipe_enabled = nil
+ Monologue::TotalSweeper.wipe_all
+ Dir["#{ActionController::Base.page_cache_directory}/**/*"].length.should eq 4
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.