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
DELETE_CHUNK_SIZE =
500
VERSION =
"0.7.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, atomic_clear: false) ⇒ RedisStorage

Returns a new instance of RedisStorage.



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

def initialize(client:, key_prefix: DEFAULT_KEY_PREFIX, scan_count: nil, atomic_clear: false)
  @client = client
  @key_prefix = key_prefix.nil? ? DEFAULT_KEY_PREFIX : key_prefix.to_s
  if !scan_count.nil? && !(scan_count.is_a?(Integer) && scan_count.positive?)
    raise ArgumentError, "scan_count must be nil or a positive Integer; got #{scan_count.inspect}"
  end
  @scan_count = scan_count
  @atomic_clear = !!atomic_clear
end

Instance Attribute Details

#atomic_clearObject (readonly)

Returns the value of attribute atomic_clear.



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

def atomic_clear
  @atomic_clear
end

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#key_prefixObject (readonly)

Returns the value of attribute key_prefix.



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

def key_prefix
  @key_prefix
end

#scan_countObject (readonly)

Returns the value of attribute scan_count.



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

def scan_count
  @scan_count
end

Class Method Details

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



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

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

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



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

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

Instance Method Details

#clearObject



61
62
63
64
65
66
67
68
# File 'lib/better_auth/redis_storage.rb', line 61

def clear
  if atomic_clear
    clear_current_generation
  else
    delete_matching_keys(storage_prefix)
  end
  nil
end

#delete(key) ⇒ Object



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

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

#get(key) ⇒ Object



36
37
38
# File 'lib/better_auth/redis_storage.rb', line 36

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

#list_keysObject Also known as: listKeys



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

def list_keys
  prefix = storage_prefix
  storage_keys(prefix).map { |key| unprefix_key(key, prefix) }
end

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



40
41
42
43
44
45
46
47
48
49
# File 'lib/better_auth/redis_storage.rb', line 40

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