Read about our upcoming Code of Conduct on this issue

Commit 989a99fa authored by Michael Kozono's avatar Michael Kozono
Browse files

Simplify by using Gitlab::SafeRequestStore

These are clear wins.
parent 1a0043fad903
......@@ -10,11 +10,7 @@ module WithPerformanceBar
def peek_enabled?
return false unless Gitlab::PerformanceBar.enabled?(current_user)
if RequestStore.active?
RequestStore.fetch(:peek_enabled) { cookie_or_default_value }
else
cookie_or_default_value
end
Gitlab::SafeRequestStore.fetch(:peek_enabled) { cookie_or_default_value }
end
private
......
......@@ -27,11 +27,7 @@ def build_from_defaults(attributes = {})
end
def cached
if RequestStore.active?
RequestStore[:"#{name}_cached_attributes"] ||= retrieve_from_cache
else
retrieve_from_cache
end
Gitlab::SafeRequestStore[:"#{name}_cached_attributes"] ||= retrieve_from_cache
end
def retrieve_from_cache
......
......@@ -20,11 +20,7 @@ def discussion_class(*)
end
def project_repository
if RequestStore.active?
RequestStore.fetch("project:#{project_id}:repository") { self.project.repository }
else
self.project.repository
end
Gitlab::SafeRequestStore.fetch("project:#{project_id}:repository") { self.project.repository }
end
def diff_file_hash
......
......@@ -2272,11 +2272,7 @@ def fetch_branch_allows_collaboration?(user, branch_name)
end
end
if RequestStore.active?
RequestStore.fetch("project-#{id}:branch-#{branch_name}:user-#{user.id}:branch_allows_collaboration") do
check_access.call
end
else
Gitlab::SafeRequestStore.fetch("project-#{id}:branch-#{branch_name}:user-#{user.id}:branch_allows_collaboration") do
check_access.call
end
end
......
......@@ -97,9 +97,7 @@ def issue_reference_pattern
private
def external_issues_cached(attribute)
return project.public_send(attribute) unless RequestStore.active? # rubocop:disable GitlabSecurity/PublicSend
cached_attributes = RequestStore[:banzai_external_issues_tracker_attributes] ||= Hash.new { |h, k| h[k] = {} }
cached_attributes = Gitlab::SafeRequestStore[:banzai_external_issues_tracker_attributes] ||= Hash.new { |h, k| h[k] = {} }
cached_attributes[project.id][attribute] = project.public_send(attribute) if cached_attributes[project.id][attribute].nil? # rubocop:disable GitlabSecurity/PublicSend
cached_attributes[project.id][attribute]
end
......
......@@ -28,11 +28,7 @@ def get(key)
end
def persisted_names
if RequestStore.active?
RequestStore[:flipper_persisted_names] ||= FlipperFeature.feature_names
else
FlipperFeature.feature_names
end
Gitlab::SafeRequestStore[:flipper_persisted_names] ||= FlipperFeature.feature_names
end
def persisted?(feature)
......@@ -76,8 +72,8 @@ def disable_group(key, group)
end
def flipper
if RequestStore.active?
RequestStore[:flipper] ||= build_flipper_instance
if Gitlab::SafeRequestStore.active?
Gitlab::SafeRequestStore[:flipper] ||= build_flipper_instance
else
@flipper ||= build_flipper_instance
end
......
......@@ -2,11 +2,7 @@ module Gitlab
module CurrentSettings
class << self
def current_application_settings
if RequestStore.active?
RequestStore.fetch(:current_application_settings) { ensure_application_settings! }
else
ensure_application_settings!
end
Gitlab::SafeRequestStore.fetch(:current_application_settings) { ensure_application_settings! }
end
def fake_application_settings(attributes = {})
......
......@@ -101,18 +101,14 @@ def diff_file(repository)
return @diff_file if defined?(@diff_file)
@diff_file = begin
if RequestStore.active?
key = {
project_id: repository.project.id,
start_sha: start_sha,
head_sha: head_sha,
path: file_path
}
RequestStore.fetch(key) { find_diff_file(repository) }
else
find_diff_file(repository)
end
key = {
project_id: repository.project.id,
start_sha: start_sha,
head_sha: head_sha,
path: file_path
}
Gitlab::SafeRequestStore.fetch(key) { find_diff_file(repository) }
end
end
......
......@@ -115,11 +115,7 @@ def new_version(gollum_page, commit_id)
def version(commit_id)
commit_find_proc = -> { Gitlab::Git::Commit.find(@repository, commit_id) }
if RequestStore.active?
RequestStore.fetch([:wiki_version_commit, commit_id]) { commit_find_proc.call }
else
commit_find_proc.call
end
Gitlab::SafeRequestStore.fetch([:wiki_version_commit, commit_id]) { commit_find_proc.call }
end
def assert_type!(object, klass)
......
module Gitlab
# Class for counting and caching the number of issuables per state.
class IssuablesCountForState
# The name of the RequestStore cache key.
# The name of the Gitlab::SafeRequestStore cache key.
CACHE_KEY = :issuables_count_for_state
# The state values that can be safely casted to a Symbol.
......@@ -10,12 +10,7 @@ class IssuablesCountForState
# finder - The finder class to use for retrieving the issuables.
def initialize(finder)
@finder = finder
@cache =
if RequestStore.active?
RequestStore[CACHE_KEY] ||= initialize_cache
else
initialize_cache
end
@cache = Gitlab::SafeRequestStore[CACHE_KEY] ||= initialize_cache
end
def for_state_or_opened(state = nil)
......
......@@ -23,7 +23,7 @@ def setup_subscribers
end
subscribe('sql.active_record') do |_, start, finish, _, data|
if RequestStore.active? && RequestStore.store[:peek_enabled]
if Gitlab::SafeRequestStore.store[:peek_enabled]
# data[:cached] is only available starting from Rails 5.1.0
# https://github.com/rails/rails/blob/v5.1.0/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb#L113
# Before that, data[:name] was set to 'CACHE'
......
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