]> git.openstreetmap.org Git - rails.git/commitdiff
Introduce some caching for diary views.
authorTom Hughes <tom@compton.nu>
Sat, 9 Jan 2010 16:43:38 +0000 (16:43 +0000)
committerTom Hughes <tom@compton.nu>
Sat, 9 Jan 2010 16:43:38 +0000 (16:43 +0000)
app/controllers/application_controller.rb
app/controllers/diary_entry_controller.rb
app/models/diary_sweeper.rb [new file with mode: 0644]
config/environment.rb

index 66ab92a5cf379fa374d986a99261c72f170ee5d1..acadfcc251fa13597ca0a6f9763305a1f04a72c9 100644 (file)
@@ -213,7 +213,39 @@ class ApplicationController < ActionController::Base
     end
   end
 
     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 
 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 
   # 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 
index 8daeac5debc30a823b4ad7ea01c10c00478f8707..55035d700a987eb61bfedd83e3602e6a71603dd1 100644 (file)
@@ -8,6 +8,10 @@ class DiaryEntryController < ApplicationController
   before_filter :check_database_writable, :only => [:new, :edit]
   before_filter :require_administrator, :only => [:hide, :hidecomment]
 
   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'
 
   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 (file)
index 0000000..0fce2c8
--- /dev/null
@@ -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
index c26592215e921a0373879c72d01f5942a4b3a2ba..4493d07a43359b5c37d2f1a8269aad00b2260796 100644 (file)
@@ -65,6 +65,9 @@ Rails::Initializer.run do |config|
   # (by default production uses :info, the others :debug)
   # config.log_level = :debug
 
   # (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, 
   # 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,