cache recent_posts and recent_plantings partials

This commit is contained in:
Skud
2013-06-07 17:52:31 +10:00
parent 20d58f1365
commit 903f0c808c
11 changed files with 81 additions and 32 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -1,5 +1,8 @@
class PlantingsController < ApplicationController
load_and_authorize_resource
cache_sweeper :planting_sweeper
# GET /plantings
# GET /plantings.json
def index

View File

@@ -1,5 +1,8 @@
class PostsController < ApplicationController
load_and_authorize_resource
cache_sweeper :post_sweeper
# GET /posts
# GET /posts.json

View File

@@ -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

View File

@@ -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

View File

@@ -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'

View File

@@ -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'

View File

@@ -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

View File

@@ -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"

View File

@@ -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"