Class: Gitlab::Cache::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/cache/client.rb

Overview

It replaces Rails.cache with metrics support

Constant Summary collapse

DEFAULT_BACKING_RESOURCE =
:unknown
DEFAULT_FEATURE_CATEGORY =
:not_owned

Instance Method Summary collapse

Constructor Details

#initialize(metrics, backend: Rails.cache) ⇒ Client

Returns a new instance of Client.



10
11
12
13
# File 'lib/gitlab/cache/client.rb', line 10

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

Instance Method Details

#fetch(name, options = nil, labels = {}, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/gitlab/cache/client.rb', line 27

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

#read(name, options = nil, labels = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gitlab/cache/client.rb', line 15

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