From 903f0c808cd3628d4d9dfd93f74680c7e310dd4f Mon Sep 17 00:00:00 2001 From: Skud Date: Fri, 7 Jun 2013 17:52:31 +1000 Subject: [PATCH] cache recent_posts and recent_plantings partials --- Gemfile | 5 +-- Gemfile.lock | 2 ++ app/controllers/plantings_controller.rb | 3 ++ app/controllers/posts_controller.rb | 3 ++ app/models/planting_sweeper.rb | 21 +++++++++++++ app/models/post_sweeper.rb | 16 ++++++++++ app/views/shared/_recent_plantings.html.haml | 22 ++++++++------ app/views/shared/_recent_posts.html.haml | 32 +++++++++++--------- config/environments/development.rb | 5 ++- config/environments/production.rb | 2 +- config/environments/staging.rb | 2 +- 11 files changed, 81 insertions(+), 32 deletions(-) create mode 100644 app/models/planting_sweeper.rb create mode 100644 app/models/post_sweeper.rb diff --git a/Gemfile b/Gemfile index 6c8ced112..14881d19c 100644 --- a/Gemfile +++ b/Gemfile @@ -17,14 +17,11 @@ gem 'activemerchant', '1.33.0', gem 'active_utils', '1.0.5', :path => 'vendor/gems/active_utils-1.0.5' - -# Bundle edge Rails instead: -# gem 'rails', :git => 'git://github.com/rails/rails.git' - group :production, :staging do gem 'pg' gem 'newrelic_rpm' gem 'unicorn' + gem 'dalli' end # Gems used only for assets and not required diff --git a/Gemfile.lock b/Gemfile.lock index f45975c9b..2532e5a46 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -74,6 +74,7 @@ GEM compass (>= 0.12.2, < 0.14) daemon_controller (1.1.2) daemons (1.1.9) + dalli (2.6.4) debugger (1.5.0) columnize (>= 0.3.1) debugger-linecache (~> 1.2.0) @@ -256,6 +257,7 @@ DEPENDENCIES capistrano-ext coffee-rails (~> 3.2.1) compass-rails (~> 1.0.3) + dalli debugger devise diff-lcs diff --git a/app/controllers/plantings_controller.rb b/app/controllers/plantings_controller.rb index 7a7c61119..b8775d1e2 100644 --- a/app/controllers/plantings_controller.rb +++ b/app/controllers/plantings_controller.rb @@ -1,5 +1,8 @@ class PlantingsController < ApplicationController load_and_authorize_resource + + cache_sweeper :planting_sweeper + # GET /plantings # GET /plantings.json def index diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index de7c80091..4003d546a 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -1,5 +1,8 @@ class PostsController < ApplicationController load_and_authorize_resource + + cache_sweeper :post_sweeper + # GET /posts # GET /posts.json diff --git a/app/models/planting_sweeper.rb b/app/models/planting_sweeper.rb new file mode 100644 index 000000000..a2b259bf9 --- /dev/null +++ b/app/models/planting_sweeper.rb @@ -0,0 +1,21 @@ +class PlantingSweeper < ActionController::Caching::Sweeper + observe Planting + + def after_create(planting) + expire_cache_for(planting) + end + + def after_update(planting) + expire_cache_for(planting) + end + + def after_destroy(planting) + expire_cache_for(planting) + end + + private + def expire_cache_for(planting) + expire_fragment('recent_plantings') + end +end + diff --git a/app/models/post_sweeper.rb b/app/models/post_sweeper.rb new file mode 100644 index 000000000..e6ad03427 --- /dev/null +++ b/app/models/post_sweeper.rb @@ -0,0 +1,16 @@ +class PostSweeper < ActionController::Caching::Sweeper + observe Post + + def after_create(post) + expire_fragment('recent_posts') + end + + def after_update(post) + expire_fragment('recent_posts') + end + + def after_destroy(post) + expire_fragment('recent_posts') + end + +end diff --git a/app/views/shared/_recent_plantings.html.haml b/app/views/shared/_recent_plantings.html.haml index 4fda76254..1db77ca33 100644 --- a/app/views/shared/_recent_plantings.html.haml +++ b/app/views/shared/_recent_plantings.html.haml @@ -1,12 +1,14 @@ -- if @plantings - %ul - - @plantings.each do |p| - %li - = link_to "#{p.crop_system_name} in #{p.location}", p - - if p.planted_at - on - = p.planted_at -- else - %p None yet. +- cache('recent_plantings') do + - if @plantings + %ul + - @plantings.each do |p| + %li + = link_to "#{p.crop_system_name} in #{p.location}", p + - if p.planted_at + on + = p.planted_at + - else + %p None yet. + - if can? :create, Planting %p= link_to "Plant something", new_planting_path, :class => 'btn btn-primary' diff --git a/app/views/shared/_recent_posts.html.haml b/app/views/shared/_recent_posts.html.haml index 82d06a2d7..3107e8fc5 100644 --- a/app/views/shared/_recent_posts.html.haml +++ b/app/views/shared/_recent_posts.html.haml @@ -1,17 +1,19 @@ -- if @posts - %ul - - @posts.each do |p| - %li - = link_to p.subject, p - - unless @member - posted by - = link_to p.author, p.author - - if p.forum - in - = link_to p.forum.name, p.forum - on - = p.created_at.to_s(:date) -- else - %p None yet. +- cache('recent_posts') do + - if @posts + %ul + - @posts.each do |p| + %li + = link_to p.subject, p + - unless @member + posted by + = link_to p.author, p.author + - if p.forum + in + = link_to p.forum.name, p.forum + on + = p.created_at.to_s(:date) + - else + %p None yet. + - if can? :create, Post %p= link_to "Post something", new_post_path, :class => 'btn btn-primary' diff --git a/config/environments/development.rb b/config/environments/development.rb index b76b3ffb4..211170993 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -11,7 +11,10 @@ Growstuff::Application.configure do # Show full error reports and disable caching config.consider_all_requests_local = true - config.action_controller.perform_caching = false + + # cache for testing/experimentation - turn off for normal dev use + config.action_controller.perform_caching = true + config.cache_store = :memory_store # Don't care if the mailer can't send config.action_mailer.raise_delivery_errors = false diff --git a/config/environments/production.rb b/config/environments/production.rb index 2cc74822a..08feba071 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -40,7 +40,7 @@ Growstuff::Application.configure do # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new) # Use a different cache store in production - # config.cache_store = :mem_cache_store + config.cache_store = :dalli_store # Enable serving of images, stylesheets, and JavaScripts from an asset server # config.action_controller.asset_host = "http://assets.example.com" diff --git a/config/environments/staging.rb b/config/environments/staging.rb index 9ed1a7ced..24f77f035 100644 --- a/config/environments/staging.rb +++ b/config/environments/staging.rb @@ -40,7 +40,7 @@ Growstuff::Application.configure do # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new) # Use a different cache store in production - # config.cache_store = :mem_cache_store + config.cache_store = :dalli_store # Enable serving of images, stylesheets, and JavaScripts from an asset server # config.action_controller.asset_host = "http://assets.example.com"