1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
# frozen_string_literal: true
module BulkImports
class UserContributionsExportMapper
USER_CONTRIBUTIONS_CACHE_KEY = 'bulk_imports/%{portable_class}/%{portable_id}/user_contribution_ids'
def initialize(portable)
@portable_class = portable.class
@portable_id = portable.id
end
def cache_user_contributions_on_record(record)
return if !record || record.is_a?(User)
user_references = record.attribute_names & ::Gitlab::ImportExport::Base::RelationFactory::USER_REFERENCES
return if user_references.empty?
user_ids = user_references.filter_map { |reference| record.try(reference) }
import_cache.set_add(generate_cache_key, user_ids, timeout: timeout) if user_ids.present?
end
def get_contributing_users
user_ids = import_cache.values_from_set(generate_cache_key)
User.by_ids(user_ids)
end
def clear_cache
import_cache.expire(generate_cache_key, 0)
end
private
attr_reader :portable_class, :portable_id
def generate_cache_key
Kernel.format(USER_CONTRIBUTIONS_CACHE_KEY, portable_class: portable_class, portable_id: portable_id)
end
def import_cache
Gitlab::Cache::Import::Caching
end
def timeout
::Gitlab::Cache::Import::Caching::LONGER_TIMEOUT
end
end
end
|