Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/import_export/group_project_object_builder.rb')
-rw-r--r--lib/gitlab/import_export/group_project_object_builder.rb28
1 files changed, 26 insertions, 2 deletions
diff --git a/lib/gitlab/import_export/group_project_object_builder.rb b/lib/gitlab/import_export/group_project_object_builder.rb
index 2e7ab3d4b69..78ba4894459 100644
--- a/lib/gitlab/import_export/group_project_object_builder.rb
+++ b/lib/gitlab/import_export/group_project_object_builder.rb
@@ -12,6 +12,13 @@ module Gitlab
#
# It also adds some logic around Group Labels/Milestones for edge cases.
class GroupProjectObjectBuilder
+ # Cache keeps 1000 entries at most, 1000 is chosen based on:
+ # - one cache entry uses around 0.5K memory, 1000 items uses around 500K.
+ # (leave some buffer it should be less than 1M). It is afforable cost for project import.
+ # - for projects in Gitlab.com, it seems 1000 entries for labels/milestones is enough.
+ # For example, gitlab has ~970 labels and 26 milestones.
+ LRU_CACHE_SIZE = 1000
+
def self.build(*args)
Project.transaction do
new(*args).find
@@ -23,17 +30,34 @@ module Gitlab
@attributes = attributes
@group = @attributes['group']
@project = @attributes['project']
+
+ if Gitlab::SafeRequestStore.active?
+ @lru_cache = cache_from_request_store
+ @cache_key = [klass, attributes]
+ end
end
def find
return if epic? && group.nil?
- find_object || klass.create(project_attributes)
+ find_with_cache do
+ find_object || klass.create(project_attributes)
+ end
end
private
- attr_reader :klass, :attributes, :group, :project
+ attr_reader :klass, :attributes, :group, :project, :lru_cache, :cache_key
+
+ def find_with_cache
+ return yield unless lru_cache && cache_key
+
+ lru_cache[cache_key] ||= yield
+ end
+
+ def cache_from_request_store
+ Gitlab::SafeRequestStore[:lru_cache] ||= LruRedux::Cache.new(LRU_CACHE_SIZE)
+ end
def find_object
klass.where(where_clause).first