diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-24 09:08:51 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-24 09:08:51 +0000 |
commit | 38149afcf95e7669a7a99828c579d185b70c04dc (patch) | |
tree | 3a90504bd926407c0cc60f44e20dba08217b928b /lib | |
parent | be660fe1d28a65ad61be24c71e66ae90f6488dc4 (diff) | |
download | gitlab-ce-38149afcf95e7669a7a99828c579d185b70c04dc.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/omniauth_logging/json_formatter.rb | 13 | ||||
-rw-r--r-- | lib/gitlab/reactive_cache_set_cache.rb | 30 | ||||
-rw-r--r-- | lib/gitlab/repository_set_cache.rb | 24 | ||||
-rw-r--r-- | lib/gitlab/set_cache.rb | 55 |
4 files changed, 99 insertions, 23 deletions
diff --git a/lib/gitlab/omniauth_logging/json_formatter.rb b/lib/gitlab/omniauth_logging/json_formatter.rb new file mode 100644 index 00000000000..cdd4da31803 --- /dev/null +++ b/lib/gitlab/omniauth_logging/json_formatter.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require 'json' + +module Gitlab + module OmniauthLogging + class JSONFormatter + def call(severity, datetime, progname, msg) + { severity: severity, timestamp: datetime.utc.iso8601(3), pid: $$, progname: progname, message: msg }.to_json << "\n" + end + end + end +end diff --git a/lib/gitlab/reactive_cache_set_cache.rb b/lib/gitlab/reactive_cache_set_cache.rb new file mode 100644 index 00000000000..14d008c3dfb --- /dev/null +++ b/lib/gitlab/reactive_cache_set_cache.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +# Interface to the Redis-backed cache store to keep track of complete cache keys +# for a ReactiveCache resource. +module Gitlab + class ReactiveCacheSetCache < Gitlab::SetCache + attr_reader :expires_in + + def initialize(expires_in: 10.minutes) + @expires_in = expires_in + end + + def clear_cache!(key) + with do |redis| + keys = read(key).map { |value| "#{cache_type}#{value}" } + keys << cache_key(key) + + redis.pipelined do + keys.each_slice(1000) { |subset| redis.del(*subset) } + end + end + end + + private + + def cache_type + "#{Gitlab::Redis::Cache::CACHE_NAMESPACE}:" + end + end +end diff --git a/lib/gitlab/repository_set_cache.rb b/lib/gitlab/repository_set_cache.rb index 4797ec0b116..1e2d86b7ad2 100644 --- a/lib/gitlab/repository_set_cache.rb +++ b/lib/gitlab/repository_set_cache.rb @@ -2,7 +2,7 @@ # Interface to the Redis-backed cache store for keys that use a Redis set module Gitlab - class RepositorySetCache + class RepositorySetCache < Gitlab::SetCache attr_reader :repository, :namespace, :expires_in def initialize(repository, extra_namespace: nil, expires_in: 2.weeks) @@ -17,18 +17,6 @@ module Gitlab "#{type}:#{namespace}:set" end - def expire(key) - with { |redis| redis.del(cache_key(key)) } - end - - def exist?(key) - with { |redis| redis.exists(cache_key(key)) } - end - - def read(key) - with { |redis| redis.smembers(cache_key(key)) } - end - def write(key, value) full_key = cache_key(key) @@ -54,15 +42,5 @@ module Gitlab write(key, yield) end end - - def include?(key, value) - with { |redis| redis.sismember(cache_key(key), value) } - end - - private - - def with(&blk) - Gitlab::Redis::Cache.with(&blk) # rubocop:disable CodeReuse/ActiveRecord - end end end diff --git a/lib/gitlab/set_cache.rb b/lib/gitlab/set_cache.rb new file mode 100644 index 00000000000..0927a3e64bb --- /dev/null +++ b/lib/gitlab/set_cache.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +# Interface to the Redis-backed cache store to keep track of complete cache keys +# for a ReactiveCache resource. +module Gitlab + class SetCache + attr_reader :expires_in + + def initialize(expires_in: 2.weeks) + @expires_in = expires_in + end + + def cache_key(key) + "#{key}:set" + end + + def expire(key) + with { |redis| redis.del(cache_key(key)) } + end + + def exist?(key) + with { |redis| redis.exists(cache_key(key)) } + end + + def write(key, value) + with do |redis| + redis.pipelined do + redis.sadd(cache_key(key), value) + + redis.expire(cache_key(key), expires_in) + end + end + + value + end + + def read(key) + with { |redis| redis.smembers(cache_key(key)) } + end + + def include?(key, value) + with { |redis| redis.sismember(cache_key(key), value) } + end + + def ttl(key) + with { |redis| redis.ttl(cache_key(key)) } + end + + private + + def with(&blk) + Gitlab::Redis::Cache.with(&blk) # rubocop:disable CodeReuse/ActiveRecord + end + end +end |