Class: Gitlab::Redis::HLL

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/redis/hll.rb

Constant Summary collapse

KEY_REGEX =
%r{\A(\w|-|:)*\{\w*\}(\w|-|:)*\z}.freeze
KeyFormatError =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add(params) ⇒ Object


13
14
15
# File 'lib/gitlab/redis/hll.rb', line 13

def self.add(params)
  self.new.add(params)
end

.count(params) ⇒ Object


9
10
11
# File 'lib/gitlab/redis/hll.rb', line 9

def self.count(params)
  self.new.count(params)
end

Instance Method Details

#add(key:, value:, expiry:) ⇒ Object

Check a basic format for the Redis key in order to ensure the keys are in the same hash slot

Examples of keys

project:{1}:set_a
project:{1}:set_b
project:{2}:set_c
2020-216-{project_action}
i_{analytics}_dev_ops_score-2020-32

31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gitlab/redis/hll.rb', line 31

def add(key:, value:, expiry:)
  unless KEY_REGEX.match?(key)
    raise KeyFormatError.new("Invalid key format. #{key} key should have changeable parts in curly braces. See https://docs.gitlab.com/ee/development/redis.html#multi-key-commands")
  end

  Gitlab::Redis::SharedState.with do |redis|
    redis.multi do |multi|
      multi.pfadd(key, value)
      multi.expire(key, expiry)
    end
  end
end

#count(keys:) ⇒ Object


17
18
19
20
21
# File 'lib/gitlab/redis/hll.rb', line 17

def count(keys:)
  Gitlab::Redis::SharedState.with do |redis|
    redis.pfcount(*keys)
  end
end