debian-mirror-gitlab/lib/gitlab/external_authorization/cache.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

68 lines
1.5 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
module Gitlab
module ExternalAuthorization
class Cache
VALIDITY_TIME = 6.hours
def initialize(user, label)
2021-04-29 21:17:54 +05:30
@user = user
@label = label
2019-07-07 11:18:12 +05:30
end
def load
2023-01-13 00:05:48 +05:30
@access, @reason, @refreshed_at = with_redis do |redis|
2019-07-07 11:18:12 +05:30
redis.hmget(cache_key, :access, :reason, :refreshed_at)
end
[access, reason, refreshed_at]
end
def store(new_access, new_reason, new_refreshed_at)
2023-01-13 00:05:48 +05:30
with_redis do |redis|
2022-10-11 01:57:18 +05:30
redis.pipelined do |pipeline|
pipeline.mapped_hmset(
2019-07-07 11:18:12 +05:30
cache_key,
{
access: new_access.to_s,
reason: new_reason.to_s,
refreshed_at: new_refreshed_at.to_s
}
)
2022-10-11 01:57:18 +05:30
pipeline.expire(cache_key, VALIDITY_TIME)
2019-07-07 11:18:12 +05:30
end
end
end
private
def access
::Gitlab::Utils.to_boolean(@access)
end
def reason
# `nil` if the cached value was an empty string
return unless @reason.present?
@reason
end
def refreshed_at
# Don't try to parse a time if there was no cache
return unless @refreshed_at.present?
Time.parse(@refreshed_at)
end
def cache_key
"external_authorization:user-#{@user.id}:label-#{@label}"
end
2023-01-13 00:05:48 +05:30
def with_redis(&block)
::Gitlab::Redis::Cache.with(&block) # rubocop:disable CodeReuse/ActiveRecord
end
2019-07-07 11:18:12 +05:30
end
end
end