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

cache.rb « dashboard « metrics « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a9ccf0fea9b24ab85e8198f3bb36087b9acc654a (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
# frozen_string_literal: true

require 'set'

module Gitlab
  module Metrics
    module Dashboard
      class Cache
        CACHE_KEYS = 'all_cached_metric_dashboards'

        class << self
          # Stores a dashboard in the cache, documenting the key
          # so the cached can be cleared in bulk at another time.
          def fetch(key)
            register_key(key)

            Rails.cache.fetch(key) { yield }
          end

          # Resets all dashboard caches, such that all
          # dashboard content will be loaded from source on
          # subsequent dashboard calls.
          def delete_all!
            all_keys.each { |key| Rails.cache.delete(key) }

            Rails.cache.delete(CACHE_KEYS)
          end

          private

          def register_key(key)
            new_keys = all_keys.add(key).to_a.join('|')

            Rails.cache.write(CACHE_KEYS, new_keys)
          end

          def all_keys
            Set.new(Rails.cache.read(CACHE_KEYS)&.split('|'))
          end
        end
      end
    end
  end
end