Introduce some caching for diary views.
This commit is contained in:
parent
d7f1186825
commit
314b734aa5
4 changed files with 73 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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'
|
||||
|
||||
|
|
34
app/models/diary_sweeper.rb
Normal file
34
app/models/diary_sweeper.rb
Normal file
|
@ -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
|
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue