diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-20 18:42:06 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-20 18:42:06 +0000 |
commit | 6e4e1050d9dba2b7b2523fdd1768823ab85feef4 (patch) | |
tree | 78be5963ec075d80116a932011d695dd33910b4e /lib/gitlab/redis | |
parent | 1ce776de4ae122aba3f349c02c17cebeaa8ecf07 (diff) | |
download | gitlab-ce-6e4e1050d9dba2b7b2523fdd1768823ab85feef4.tar.gz |
Add latest changes from gitlab-org/gitlab@13-3-stable-ee
Diffstat (limited to 'lib/gitlab/redis')
-rw-r--r-- | lib/gitlab/redis/hll.rb | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/gitlab/redis/hll.rb b/lib/gitlab/redis/hll.rb new file mode 100644 index 00000000000..ff5754675e2 --- /dev/null +++ b/lib/gitlab/redis/hll.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +module Gitlab + module Redis + class HLL + KEY_REGEX = %r{\A(\w|-|:)*\{\w*\}(\w|-|:)*\z}.freeze + KeyFormatError = Class.new(StandardError) + + def self.count(params) + self.new.count(params) + end + + def self.add(params) + self.new.add(params) + end + + def count(keys:) + Gitlab::Redis::SharedState.with do |redis| + redis.pfcount(*keys) + end + end + + # 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 + 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 + end + end +end |