Commit e35b85ae authored by Stan Hu's avatar Stan Hu

Refactor `cache_boolean` to `cache_value` for all Geo calls

parent 6ec3953e
...@@ -3,7 +3,7 @@ module Gitlab ...@@ -3,7 +3,7 @@ module Gitlab
class OauthApplicationUndefinedError < StandardError; end class OauthApplicationUndefinedError < StandardError; end
def self.current_node def self.current_node
RequestStore.store[:geo_node_current] ||= begin self.cache_value(:geo_node_current) do
GeoNode.find_by(host: Gitlab.config.gitlab.host, GeoNode.find_by(host: Gitlab.config.gitlab.host,
port: Gitlab.config.gitlab.port, port: Gitlab.config.gitlab.port,
relative_url_root: Gitlab.config.gitlab.relative_url_root) relative_url_root: Gitlab.config.gitlab.relative_url_root)
...@@ -11,15 +11,15 @@ module Gitlab ...@@ -11,15 +11,15 @@ module Gitlab
end end
def self.primary_node def self.primary_node
RequestStore.store[:geo_primary_node] ||= GeoNode.find_by(primary: true) self.cache_value(:geo_primary_node) { GeoNode.find_by(primary: true) }
end end
def self.secondary_nodes def self.secondary_nodes
RequestStore.store[:geo_secondary_nodes] ||= GeoNode.where(primary: false) self.cache_value(:geo_secondary_nodes) { GeoNode.where(primary: false) }
end end
def self.enabled? def self.enabled?
self.cache_boolean(:geo_node_enabled) { GeoNode.exists? } self.cache_value(:geo_node_enabled) { GeoNode.exists? }
end end
def self.license_allows? def self.license_allows?
...@@ -27,11 +27,11 @@ module Gitlab ...@@ -27,11 +27,11 @@ module Gitlab
end end
def self.primary? def self.primary?
self.cache_boolean(:geo_node_primary) { self.enabled? && self.current_node && self.current_node.primary? } self.cache_value(:geo_node_primary) { self.enabled? && self.current_node && self.current_node.primary? }
end end
def self.secondary? def self.secondary?
self.cache_boolean(:geo_node_secondary) { self.enabled? && self.current_node && !self.current_node.primary? } self.cache_value(:geo_node_secondary) { self.enabled? && self.current_node && !self.current_node.primary? }
end end
def self.geo_node?(host:, port:) def self.geo_node?(host:, port:)
...@@ -49,17 +49,15 @@ module Gitlab ...@@ -49,17 +49,15 @@ module Gitlab
def self.oauth_authentication def self.oauth_authentication
return false unless Gitlab::Geo.secondary? return false unless Gitlab::Geo.secondary?
RequestStore.store[:geo_oauth_application] ||= self.cache_value(:geo_oauth_application) do
Gitlab::Geo.current_node.oauth_application or raise OauthApplicationUndefinedError Gitlab::Geo.current_node.oauth_application or raise OauthApplicationUndefinedError
end
end end
def self.cache_boolean(key, &block) def self.cache_value(key, &block)
return yield unless RequestStore.active? return yield unless RequestStore.active?
val = RequestStore.store[key] RequestStore.fetch(key) { yield }
return val unless val.nil?
RequestStore[key] = yield
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment