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:"
SCAN_DEFAULT_COUNT =
100
VERSION =
"0.2.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RedisStorage.



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

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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#key_prefixObject (readonly)

Returns the value of attribute key_prefix.



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

def key_prefix
  @key_prefix
end

#scan_countObject (readonly)

Returns the value of attribute scan_count.



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

def scan_count
  @scan_count
end

Class Method Details

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



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

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

.redisStorage(client:, key_prefix: DEFAULT_KEY_PREFIX, scan_count: nil) ⇒ Object



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

def self.redisStorage(client:, key_prefix: DEFAULT_KEY_PREFIX, scan_count: nil)
  new(client: client, key_prefix: key_prefix, scan_count: scan_count)
end

Instance Method Details

#clearObject



55
56
57
58
59
60
61
# File 'lib/better_auth/redis_storage.rb', line 55

def clear
  keys = storage_keys
  # Upstream calls del(...keys) unconditionally; Ruby keeps this guard to
  # avoid Redis ERR wrong number of arguments when no prefixed keys exist.
  client.del(*keys) unless keys.empty?
  nil
end

#delete(key) ⇒ Object



46
47
48
49
# File 'lib/better_auth/redis_storage.rb', line 46

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

#get(key) ⇒ Object



31
32
33
# File 'lib/better_auth/redis_storage.rb', line 31

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

#list_keysObject Also known as: listKeys



51
52
53
# File 'lib/better_auth/redis_storage.rb', line 51

def list_keys
  storage_keys.map { |key| unprefix_key(key) }
end

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



35
36
37
38
39
40
41
42
43
44
# File 'lib/better_auth/redis_storage.rb', line 35

def set(key, value, ttl = nil)
  prefixed_key = prefix_key(key)
  coerced_ttl = coerce_ttl(ttl)
  if coerced_ttl
    client.setex(prefixed_key, coerced_ttl, value)
  else
    client.set(prefixed_key, value)
  end
  nil
end