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

client.rb « cache « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 37d6cac8d43604349539dd0f2ba5005ed5c2d337 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true

module Gitlab
  module Cache
    # It replaces Rails.cache with metrics support
    class Client
      DEFAULT_BACKING_RESOURCE = :unknown

      # Build Cache client with the metadata support
      #
      # @param cache_identifier [String] defines the location of the cache definition
      #   Example: "ProtectedBranches::CacheService#fetch"
      # @param feature_category [Symbol] name of the feature category (from config/feature_categories.yml)
      # @param backing_resource [Symbol] most affected resource by cache generation (full list: VALID_BACKING_RESOURCES)
      # @return [Gitlab::Cache::Client]
      def self.build_with_metadata(
        cache_identifier:,
        feature_category:,
        backing_resource: DEFAULT_BACKING_RESOURCE
      )
        new(Metadata.new(
          cache_identifier: cache_identifier,
          feature_category: feature_category,
          backing_resource: backing_resource
        ))
      end

      def initialize(metadata, backend: Rails.cache)
        @metadata = metadata
        @metrics = Metrics.new(metadata)
        @backend = backend
      end

      def read(name)
        read_result = backend.read(name)

        if read_result.nil?
          metrics.increment_cache_miss
        else
          metrics.increment_cache_hit
        end

        read_result
      end

      def fetch(name, options = nil, &block)
        read_result = read(name)

        return read_result unless block || read_result

        backend.fetch(name, options) do
          metrics.observe_cache_generation(&block)
        end
      end

      delegate :write, :exist?, :delete, to: :backend

      attr_reader :metadata, :metrics

      private

      attr_reader :backend
    end
  end
end