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

UNSET =
Object.new.freeze
DEFAULT_KEY_PREFIX =
"better-auth:"
SCAN_DEFAULT_COUNT =
100
DELETE_CHUNK_SIZE =
500
VERSION =
"0.10.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, key_prefix: UNSET, key_prefix_camel: UNSET, scan_count: UNSET, atomic_clear: false, **options) ⇒ RedisStorage

Returns a new instance of RedisStorage.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/better_auth/redis_storage.rb', line 27

def initialize(client:, key_prefix: UNSET, key_prefix_camel: UNSET, scan_count: UNSET, atomic_clear: false, **options)
  key_prefix_camel = self.class.extract_key_prefix_camel!(options) if key_prefix_camel.equal?(UNSET)
  self.class.reject_unknown_keywords!(options)
  @client = client
  @key_prefix = self.class.resolve_key_prefix(key_prefix, key_prefix_camel)
  scan_count = SCAN_DEFAULT_COUNT if scan_count.equal?(UNSET)
  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.



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

def atomic_clear
  @atomic_clear
end

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#key_prefixObject (readonly)

Returns the value of attribute key_prefix.



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

def key_prefix
  @key_prefix
end

#scan_countObject (readonly)

Returns the value of attribute scan_count.



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

def scan_count
  @scan_count
end

Class Method Details

.build(client:, key_prefix: UNSET, scan_count: UNSET, atomic_clear: false, **options) ⇒ Object



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

def self.build(client:, key_prefix: UNSET, scan_count: UNSET, atomic_clear: false, **options)
  key_prefix_camel = extract_key_prefix_camel!(options)
  reject_unknown_keywords!(options)
  new(client: client, key_prefix: key_prefix, key_prefix_camel: key_prefix_camel, scan_count: scan_count, atomic_clear: atomic_clear)
end

.extract_key_prefix_camel!(options) ⇒ Object



40
41
42
# File 'lib/better_auth/redis_storage.rb', line 40

def self.extract_key_prefix_camel!(options)
  options.key?(:keyPrefix) ? options.delete(:keyPrefix) : UNSET
end

.redisStorage(client:, key_prefix: UNSET, scan_count: UNSET, atomic_clear: false, **options) ⇒ Object



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

def self.redisStorage(client:, key_prefix: UNSET, scan_count: UNSET, atomic_clear: false, **options)
  key_prefix_camel = extract_key_prefix_camel!(options)
  reject_unknown_keywords!(options)
  new(client: client, key_prefix: key_prefix, key_prefix_camel: key_prefix_camel, scan_count: scan_count, atomic_clear: atomic_clear)
end

.reject_unknown_keywords!(options) ⇒ Object

Raises:

  • (ArgumentError)


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

def self.reject_unknown_keywords!(options)
  return if options.empty?

  unknown = options.keys.map(&:inspect).join(", ")
  label = (options.length == 1) ? "keyword" : "keywords"
  raise ArgumentError, "unknown #{label}: #{unknown}"
end

.resolve_key_prefix(key_prefix, key_prefix_camel) ⇒ Object



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

def self.resolve_key_prefix(key_prefix, key_prefix_camel)
  if !key_prefix.equal?(UNSET) && !key_prefix_camel.equal?(UNSET) && key_prefix != key_prefix_camel
    raise ArgumentError, "key_prefix and keyPrefix cannot both be provided with different values"
  end

  selected = key_prefix.equal?(UNSET) ? key_prefix_camel : key_prefix
  selected = DEFAULT_KEY_PREFIX if selected.equal?(UNSET) || selected.nil?
  selected.to_s
end

Instance Method Details

#clearObject



87
88
89
90
91
92
93
94
# File 'lib/better_auth/redis_storage.rb', line 87

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

#delete(key) ⇒ Object



77
78
79
80
# File 'lib/better_auth/redis_storage.rb', line 77

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

#get(key) ⇒ Object



62
63
64
# File 'lib/better_auth/redis_storage.rb', line 62

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

#list_keysObject Also known as: listKeys



82
83
84
85
# File 'lib/better_auth/redis_storage.rb', line 82

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

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



66
67
68
69
70
71
72
73
74
75
# File 'lib/better_auth/redis_storage.rb', line 66

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