summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/build/cache.rb
blob: 4fcb516884706e681620feb1f8c79f2d98234800 (plain)
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
# frozen_string_literal: true

module Gitlab
  module Ci
    module Build
      class Cache
        include ::Gitlab::Utils::StrongMemoize

        def initialize(cache, pipeline)
          if multiple_cache_per_job?
            cache = Array.wrap(cache)
            @cache = cache.map do |cache|
              Gitlab::Ci::Pipeline::Seed::Build::Cache
              .new(pipeline, cache)
            end
          else
            @cache = Gitlab::Ci::Pipeline::Seed::Build::Cache
              .new(pipeline, cache)
          end
        end

        def cache_attributes
          strong_memoize(:cache_attributes) do
            if multiple_cache_per_job?
              if @cache.empty?
                {}
              else
                { options: { cache: @cache.map(&:attributes) } }
              end
            else
              @cache.build_attributes
            end
          end
        end

        private

        def multiple_cache_per_job?
          strong_memoize(:multiple_cache_per_job) do
            ::Gitlab::Ci::Features.multiple_cache_per_job?
          end
        end
      end
    end
  end
end