From: Tom Hughes Date: Sat, 9 Jan 2010 16:43:38 +0000 (+0000) Subject: Introduce some caching for diary views. X-Git-Tag: live~6336 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/314b734aa59ac762de09daacd08bbb952c94783d Introduce some caching for diary views. --- diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 66ab92a5c..acadfcc25 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -213,7 +213,39 @@ class ApplicationController < ActionController::Base end end + ## + # extend caches_action to include the parameters, locale and logged in + # status in all cache keys + def self.caches_action(*actions) + options = actions.extract_options! + cache_path = options[:cache_path] || Hash.new + + options[:cache_path] = Proc.new do |controller| + user = controller.instance_variable_get("@user") + + case + when user.nil? then user = :none + when user.display_name == controller.params[:display_name] then user = :self + else user = :other + end + + cache_path.merge(controller.params).merge(:locale => I18n.locale, :user => user) + end + + actions.push(options) + + super *actions + end + + ## + # extend expire_action to expire all variants + def expire_action(options = {}) + path = fragment_cache_key(options).gsub('?', '.').gsub(':', '.') + expire_fragment(Regexp.new(Regexp.escape(path) + "\\..*")) + end + private + # extract authorisation credentials from headers, returns user = nil if none def get_auth_data if request.env.has_key? 'X-HTTP_AUTHORIZATION' # where mod_rewrite might have put it diff --git a/app/controllers/diary_entry_controller.rb b/app/controllers/diary_entry_controller.rb index 8daeac5de..55035d700 100644 --- a/app/controllers/diary_entry_controller.rb +++ b/app/controllers/diary_entry_controller.rb @@ -8,6 +8,10 @@ class DiaryEntryController < ApplicationController before_filter :check_database_writable, :only => [:new, :edit] before_filter :require_administrator, :only => [:hide, :hidecomment] + caches_action :list, :view, :layout => false + caches_action :rss, :layout => true + cache_sweeper :diary_sweeper, :only => [:new, :edit, :comment, :hide, :hidecomment] + def new @title = t 'diary_entry.new.title' diff --git a/app/models/diary_sweeper.rb b/app/models/diary_sweeper.rb new file mode 100644 index 000000000..0fce2c819 --- /dev/null +++ b/app/models/diary_sweeper.rb @@ -0,0 +1,34 @@ +class DiarySweeper < ActionController::Caching::Sweeper + observe DiaryComment, DiaryEntry + + def after_create(record) + expire_cache_for(record) + end + + def after_update(record) + expire_cache_for(record) + end + + def after_destroy(record) + expire_cache_for(record) + end + +private + + def expire_cache_for(record) + case + when record.is_a?(DiaryEntry): entry = record + when record.is_a?(DiaryComment): entry = record.diary_entry + end + + expire_action(:controller => 'diary_entry', :action => 'view', :id => entry.id) + + expire_action(:controller => 'diary_entry', :action => 'list', :language => nil, :display_name => nil) + expire_action(:controller => 'diary_entry', :action => 'list', :language => entry.language_code, :display_name => nil) + expire_action(:controller => 'diary_entry', :action => 'list', :language => nil, :display_name => entry.user.display_name) + + expire_action(:controller => 'diary_entry', :action => 'rss', :language => nil, :display_name => nil) + expire_action(:controller => 'diary_entry', :action => 'rss', :language => entry.language_code, :display_name => nil) + expire_action(:controller => 'diary_entry', :action => 'rss', :language => nil, :display_name => entry.user.display_name) + end +end diff --git a/config/environment.rb b/config/environment.rb index c26592215..4493d07a4 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -65,6 +65,9 @@ Rails::Initializer.run do |config| # (by default production uses :info, the others :debug) # config.log_level = :debug + # Configure cache + config.cache_store = :file_store, "#{RAILS_ROOT}/tmp/cache" + # Your secret key for verifying cookie session data integrity. # If you change this key, all old sessions will become invalid! # Make sure the secret is at least 30 characters and all random,