Class: OAuth2c::Cache::Backends::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/oauth2c/cache/backends/redis.rb

Instance Method Summary collapse

Constructor Details

#initialize(redis, namespace = nil) ⇒ Redis

Returns a new instance of Redis.



23
24
25
26
# File 'lib/oauth2c/cache/backends/redis.rb', line 23

def initialize(redis, namespace = nil)
  @redis = redis
  @namespace = namespace
end

Instance Method Details

#lookup(key) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/oauth2c/cache/backends/redis.rb', line 28

def lookup(key)
  access_token_data, scope_data = @redis.mget("#{fq_key(key)}:access_token", "#{fq_key(key)}:scope")
  return if access_token_data.nil? || scope_data.nil?

  access_token = AccessToken.new(**JSON.load(access_token_data).symbolize_keys)
  Store::Bucket.new(access_token, JSON.load(scope_data))
end

#store(key, bucket) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/oauth2c/cache/backends/redis.rb', line 36

def store(key, bucket)
  access_token = bucket.access_token

  @redis.mset(
    "#{fq_key(key)}:access_token", JSON.dump(access_token.attributes),
    "#{fq_key(key)}:scope", JSON.dump(bucket.scope),
  )

  @redis.expire("#{fq_key(key)}:access_token", access_token.expires_in)
  @redis.expire("#{fq_key(key)}:scope", access_token.expires_in)
end