From 4d840d10db71a44a658bc81afaf4cdbbc9656cef Mon Sep 17 00:00:00 2001
From: Guo Xiang Tan <tgx_world@hotmail.com>
Date: Thu, 7 Sep 2017 13:29:30 +0800
Subject: [PATCH] PERF: Reduce number of Redis hits per requests.

---
 app/controllers/application_controller.rb | 11 ++++++++---
 lib/discourse.rb                          |  2 +-
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 22c06313627..c1b66293e38 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -36,6 +36,7 @@ class ApplicationController < ActionController::Base
     end
   end
 
+  before_action :check_readonly_mode
   before_filter :handle_theme
   before_filter :set_current_user_for_logs
   before_filter :clear_notifications
@@ -61,7 +62,7 @@ class ApplicationController < ActionController::Base
   end
 
   def add_readonly_header
-    response.headers['Discourse-Readonly'] = 'true' if Discourse.readonly_mode?
+    response.headers['Discourse-Readonly'] = 'true' if @readonly_mode
   end
 
   def perform_refresh_session
@@ -182,7 +183,7 @@ class ApplicationController < ActionController::Base
   end
 
   def clear_notifications
-    if current_user && !Discourse.readonly_mode?
+    if current_user && !@readonly_mode
 
       cookie_notifications = cookies['cn'.freeze]
       notifications = request.headers['Discourse-Clear-Notifications'.freeze]
@@ -400,6 +401,10 @@ class ApplicationController < ActionController::Base
 
   private
 
+    def check_readonly_mode
+      @readonly_mode = Discourse.readonly_mode?
+    end
+
     def locale_from_header
       begin
         # 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
       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
 
     def build_not_found_page(status = 404, layout = false)
diff --git a/lib/discourse.rb b/lib/discourse.rb
index e9c06f020b8..a12dacdcf97 100644
--- a/lib/discourse.rb
+++ b/lib/discourse.rb
@@ -276,7 +276,7 @@ module Discourse
   end
 
   def self.readonly_mode?
-    recently_readonly? || READONLY_KEYS.any? { |key| !!$redis.get(key) }
+    recently_readonly? || $redis.mget(*READONLY_KEYS).compact.present?
   end
 
   def self.last_read_only