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

store.rb « redis « markdown_cache « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: af9098c3300dea7564c3a4ea9239ead7a35cfe62 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen_string_literal: true

module Gitlab
  module MarkdownCache
    module Redis
      class Store
        EXPIRES_IN = 1.day

        def self.bulk_read(subjects)
          results = {}

          data = Gitlab::Redis::Cache.with do |r|
            Gitlab::Instrumentation::RedisClusterValidator.allow_cross_slot_commands do
              Gitlab::Redis::CrossSlot::Pipeline.new(r).pipelined do |pipeline|
                subjects.each do |subject|
                  new(subject).read(pipeline)
                end
              end
            end
          end

          # enumerate data
          data.each_with_index do |elem, idx|
            results[subjects[idx].cache_key] = elem
          end

          results
        end

        def initialize(subject)
          @subject = subject
          @loaded = false
        end

        def save(updates)
          @loaded = false

          with_redis do |r|
            r.mapped_hmset(markdown_cache_key, updates)
            r.expire(markdown_cache_key, EXPIRES_IN)
          end
        end

        def read(pipeline = nil)
          @loaded = true

          if pipeline
            pipeline.mapped_hmget(markdown_cache_key, *fields)
          else
            with_redis do |r|
              r.mapped_hmget(markdown_cache_key, *fields)
            end
          end
        end

        def loaded?
          @loaded
        end

        private

        def fields
          @fields ||= @subject.cached_markdown_fields.html_fields + [:cached_markdown_version]
        end

        def markdown_cache_key
          unless @subject.respond_to?(:cache_key)
            raise Gitlab::MarkdownCache::UnsupportedClassError,
                  "This class has no cache_key to use for caching"
          end

          "markdown_cache:#{@subject.cache_key}"
        end

        def with_redis(&block)
          Gitlab::Redis::Cache.with(&block) # rubocop:disable CodeReuse/ActiveRecord
        end
      end
    end
  end
end