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: 1e2962a51513f1b16748ee813496976da8db69ac (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

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

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

      def read(name, options = nil, labels = {})
        read_result = backend.read(name, options)

        if read_result.nil?
          metrics.increment_cache_miss(labels)
        else
          metrics.increment_cache_hit(labels)
        end

        read_result
      end

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

        return read_result unless block || read_result

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

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

      private

      attr_reader :metrics, :backend
    end
  end
end