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

highlight_cache.rb « discussions_diff « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 14cb773251b2861d6896e5c22186dac3a44301f2 (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
82
83
84
85
86
87
88
89
90
# frozen_string_literal: true
#
module Gitlab
  module DiscussionsDiff
    class HighlightCache
      extend Gitlab::Utils::Gzip

      class << self
        VERSION = 1
        EXPIRATION = 1.week

        # Sets multiple keys to a given value. The value
        # is serialized as JSON.
        #
        # mapping - Write multiple cache values at once
        def write_multiple(mapping)
          with_redis do |redis|
            Gitlab::Instrumentation::RedisClusterValidator.allow_cross_slot_commands do
              redis.multi do |multi|
                mapping.each do |raw_key, value|
                  key = cache_key_for(raw_key)

                  multi.set(key, gzip_compress(value.to_json), ex: EXPIRATION)
                end
              end
            end
          end
        end

        # Reads multiple cache keys at once.
        #
        # raw_keys - An Array of unique cache keys, without namespaces.
        #
        # It returns a list of deserialized diff lines. Ex.:
        # [[Gitlab::Diff::Line, ...], [Gitlab::Diff::Line]]
        def read_multiple(raw_keys)
          return [] if raw_keys.empty?

          keys = raw_keys.map { |id| cache_key_for(id) }

          content =
            with_redis do |redis|
              Gitlab::Instrumentation::RedisClusterValidator.allow_cross_slot_commands do
                redis.mget(keys)
              end
            end

          content.map! do |lines|
            next unless lines

            Gitlab::Json.parse(gzip_decompress(lines)).map! do |line|
              Gitlab::Diff::Line.safe_init_from_hash(line)
            end
          end
        end

        # Clears multiple cache keys at once.
        #
        # raw_keys - An Array of unique cache keys, without namespaces.
        #
        # It returns the number of cache keys cleared. Ex.: 42
        def clear_multiple(raw_keys)
          return [] if raw_keys.empty?

          keys = raw_keys.map { |id| cache_key_for(id) }

          with_redis do |redis|
            Gitlab::Instrumentation::RedisClusterValidator.allow_cross_slot_commands do
              redis.del(keys)
            end
          end
        end

        def cache_key_for(raw_key)
          "#{cache_key_prefix}:#{raw_key}"
        end

        private

        def cache_key_prefix
          "#{Redis::Cache::CACHE_NAMESPACE}:#{VERSION}:discussion-highlight"
        end

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