PERF: Reduce number of Redis hits per requests.

This commit is contained in:
Guo Xiang Tan 2017-09-07 13:29:30 +08:00
parent 5aba30ede6
commit 4d840d10db
2 changed files with 9 additions and 4 deletions

View File

@ -36,6 +36,7 @@ class ApplicationController < ActionController::Base
end end
end end
before_action :check_readonly_mode
before_filter :handle_theme before_filter :handle_theme
before_filter :set_current_user_for_logs before_filter :set_current_user_for_logs
before_filter :clear_notifications before_filter :clear_notifications
@ -61,7 +62,7 @@ class ApplicationController < ActionController::Base
end end
def add_readonly_header def add_readonly_header
response.headers['Discourse-Readonly'] = 'true' if Discourse.readonly_mode? response.headers['Discourse-Readonly'] = 'true' if @readonly_mode
end end
def perform_refresh_session def perform_refresh_session
@ -182,7 +183,7 @@ class ApplicationController < ActionController::Base
end end
def clear_notifications def clear_notifications
if current_user && !Discourse.readonly_mode? if current_user && !@readonly_mode
cookie_notifications = cookies['cn'.freeze] cookie_notifications = cookies['cn'.freeze]
notifications = request.headers['Discourse-Clear-Notifications'.freeze] notifications = request.headers['Discourse-Clear-Notifications'.freeze]
@ -400,6 +401,10 @@ class ApplicationController < ActionController::Base
private private
def check_readonly_mode
@readonly_mode = Discourse.readonly_mode?
end
def locale_from_header def locale_from_header
begin begin
# Rails I18n uses underscores between the locale and the region; the request # Rails I18n uses underscores between the locale and the region; the request
@ -574,7 +579,7 @@ class ApplicationController < ActionController::Base
def block_if_readonly_mode def block_if_readonly_mode
return if request.fullpath.start_with?(path "/admin/backups") return if request.fullpath.start_with?(path "/admin/backups")
raise Discourse::ReadOnly.new if !(request.get? || request.head?) && Discourse.readonly_mode? raise Discourse::ReadOnly.new if !(request.get? || request.head?) && @readonly_mode
end end
def build_not_found_page(status = 404, layout = false) def build_not_found_page(status = 404, layout = false)

View File

@ -276,7 +276,7 @@ module Discourse
end end
def self.readonly_mode? def self.readonly_mode?
recently_readonly? || READONLY_KEYS.any? { |key| !!$redis.get(key) } recently_readonly? || $redis.mget(*READONLY_KEYS).compact.present?
end end
def self.last_read_only def self.last_read_only