Permalink
Browse files

added caching (fix #15)

added a sweeper with non-DRY tests

DRYed up cache spec

added caching (fix #15)
  • Loading branch information...
1 parent c9969a5 commit 17a9e738a0fc64058686aa1848b259dfe9922c47 jipiboily committed Mar 28, 2012
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- monologue (0.0.1.alpha6)
+ monologue (0.0.1.beta1)
bcrypt-ruby
rails (>= 3.1.3)
tinymce-rails
@@ -136,7 +136,7 @@ GEM
treetop (1.4.10)
polyglot
polyglot (>= 0.3.1)
- tzinfo (0.3.31)
+ tzinfo (0.3.32)
xpath (0.1.4)
nokogiri (~> 1.3)
View
@@ -26,6 +26,11 @@ Monologue is a somewhat basic mountable blogging engine in Rails built to be eas
rails c
Monologue::User.create(name: "jipiboily", email:"j@jipi.ca", password:"password", password_confirmation: "password")
+### Enable caching
+Just turn perform_caching to true in your environment config file (`config/environment/{environment}.rb):
+
+ config.action_controller.perform_caching = true
+
## Customization
- RSS feed URL is "/feed"
@@ -1,5 +1,6 @@
class Monologue::Admin::PostsController < Monologue::Admin::BaseController
respond_to :html
+ cache_sweeper Monologue::PostsSweeper, only: [:create, :update, :destroy]
def index
@posts = Monologue::Post.includes(:posts_revisions).joins(:posts_revisions).order("published_at DESC").all
@@ -1,4 +1,6 @@
class Monologue::PostsController < Monologue::ApplicationController
+ caches_page :index, :show, :feed
+
def index
@posts = Monologue::Post.published
end
@@ -0,0 +1,15 @@
+class Monologue::PostsSweeper < ActionController::Caching::Sweeper
+ observe Monologue::Post
+
+
+ def sweep(post)
+ all_monologue_cache_dir = ActionController::Base.page_cache_path('/monologue/').gsub(".html","")
+ FileUtils.rm_rf ActionController::Base.page_cache_path('/monologue/')
+ FileUtils.rm_rf all_monologue_cache_dir
+ end
+
+ alias_method :after_create, :sweep
+ alias_method :after_update, :sweep
+ alias_method :after_destroy, :sweep
+
+end
View
@@ -11,5 +11,5 @@
resources :posts
end
- match "*post_url" => "posts#show"
+ match "*post_url" => "posts#show", as: "post"
end
@@ -11,7 +11,7 @@
# Show full error reports and disable caching
config.consider_all_requests_local = true
- config.action_controller.perform_caching = false
+ config.action_controller.perform_caching = true
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
@@ -16,7 +16,7 @@
# Show full error reports and disable caching
config.consider_all_requests_local = true
- config.action_controller.perform_caching = false
+ config.action_controller.perform_caching = true
# Raise exceptions instead of rendering exception templates
config.action_dispatch.show_exceptions = false
@@ -0,0 +1,68 @@
+# encoding: UTF-8
+require 'spec_helper'
+describe "cache" do
+ before(:each) do
+ user = Factory(:user)
+ posts = 0
+ 25.times do
+ revisions = 0
+ posts+=1
+ @post = Factory(:post)
+ 3.times do
+ revisions+=1
+ @post.posts_revisions.build(Factory.attributes_for(:posts_revision, user_id: user.id, url: "/monologue/post/#{posts}" , title: "post #{posts} | revision #{revisions}"))
+ end
+ @post.save
+ end
+
+ ActionController::Base.perform_caching = true
+ clear_cache
+ end
+
+ after do
+ ActionController::Base.perform_caching = false
+ clear_cache
+ end
+
+ describe "cache creation" do
+ it "creates post's index cache" do
+ assert_create_cache(root_path, "post 2")
+ end
+
+ it "creates post's show cache" do
+ assert_create_cache("/monologue/post/2", "post 2")
+ end
+
+ it "creates feed cache" do
+ assert_create_cache(feed_path, "post 2", "rss")
+ end
+ end
+
+ describe "cache sweeping" do
+ before(:each) do
+ @test_paths = [root_path, "/monologue/post/2", "/monologue/post/3"]
+ @test_paths.each do |path|
+ assert_create_cache(path)
+ end
+ assert_create_cache(feed_path,nil,"rss")
+ end
+
+ it "should clear cache on create" do
+ post = Factory(:post)
+ cache_sweeped?(@test_paths).should be_true
+ cache_sweeped?([feed_path], "rss").should be_true
+ end
+
+ it "should clear cache on update" do
+ @post.save!
+ cache_sweeped?(@test_paths).should be_true
+ cache_sweeped?([feed_path], "rss").should be_true
+ end
+
+ it "should clear cache on destroy" do
+ @post.destroy
+ cache_sweeped?(@test_paths).should be_true
+ cache_sweeped?([feed_path], "rss").should be_true
+ end
+ end
+end
@@ -0,0 +1,44 @@
+# encoding: UTF-8
+ActionController::Base.public_class_method :page_cache_path
+
+class String
+ def is_page_cached? extension = nil
+ file = extension.nil? ? self : "#{self}.#{extension}"
+ File.exists? ActionController::Base.page_cache_path(file)
+ end
+end
+
+module ResponseHelper
+ def page_cached?(request, extension = nil)
+ request.path.is_page_cached? extension
+ end
+end
+
+module CacheHelper
+ def cache_sweeped?(path_array, extension = nil)
+ path_array.each do |path|
+ return false if path.chomp("/").is_page_cached?(extension) == true
+ end
+ end
+
+ def assert_create_cache(path, content = nil, extension = nil)
+ path.is_page_cached?.should be_false
+ get path
+ response.status.should be(200)
+ response.body.should have_content(content) unless content.nil?
+ response.should be_page_cached(request, extension)
+ end
+
+ def clear_cache
+ show_cache_dir = "#{Rails.root}/public/monologue"
+ index_cache_file = "#{show_cache_dir}.html"
+ FileUtils.rm_rf show_cache_dir
+ FileUtils.rm index_cache_file, :force => true
+ end
+end
+
+ActionDispatch::TestResponse.send(:include, ResponseHelper)
+
+RSpec.configure do |c|
+ c.include CacheHelper, :type => :request
+end

0 comments on commit 17a9e73

Please sign in to comment.