Class: BetterAuth::RedisStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/better_auth/redis_storage.rb,
lib/better_auth/redis_storage/version.rb

Constant Summary collapse

DEFAULT_KEY_PREFIX =
"better-auth:"
VERSION =
"0.1.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, key_prefix: DEFAULT_KEY_PREFIX) ⇒ RedisStorage

Returns a new instance of RedisStorage.



16
17
18
19
# File 'lib/better_auth/redis_storage.rb', line 16

def initialize(client:, key_prefix: DEFAULT_KEY_PREFIX)
  @client = client
  @key_prefix = key_prefix.to_s
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



10
11
12
# File 'lib/better_auth/redis_storage.rb', line 10

def client
  @client
end

#key_prefixObject (readonly)

Returns the value of attribute key_prefix.



10
11
12
# File 'lib/better_auth/redis_storage.rb', line 10

def key_prefix
  @key_prefix
end

Class Method Details

.build(client:, key_prefix: DEFAULT_KEY_PREFIX) ⇒ Object



12
13
14
# File 'lib/better_auth/redis_storage.rb', line 12

def self.build(client:, key_prefix: DEFAULT_KEY_PREFIX)
  new(client: client, key_prefix: key_prefix)
end

Instance Method Details

#clearObject



42
43
44
45
# File 'lib/better_auth/redis_storage.rb', line 42

def clear
  keys = client.keys("#{key_prefix}*")
  client.del(*keys) unless keys.empty?
end

#delete(key) ⇒ Object



34
35
36
# File 'lib/better_auth/redis_storage.rb', line 34

def delete(key)
  client.del(prefix_key(key))
end

#get(key) ⇒ Object



21
22
23
# File 'lib/better_auth/redis_storage.rb', line 21

def get(key)
  client.get(prefix_key(key))
end

#list_keysObject Also known as: listKeys



38
39
40
# File 'lib/better_auth/redis_storage.rb', line 38

def list_keys
  client.keys("#{key_prefix}*").map { |key| unprefix_key(key) }
end

#set(key, value, ttl = nil) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/better_auth/redis_storage.rb', line 25

def set(key, value, ttl = nil)
  prefixed_key = prefix_key(key)
  if ttl&.to_i&.positive?
    client.setex(prefixed_key, ttl.to_i, value)
  else
    client.set(prefixed_key, value)
  end
end