Class: BetterAuth::RedisStorage
- Inherits:
-
Object
- Object
- BetterAuth::RedisStorage
- 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
-
#atomic_clear ⇒ Object
readonly
Returns the value of attribute atomic_clear.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#key_prefix ⇒ Object
readonly
Returns the value of attribute key_prefix.
-
#scan_count ⇒ Object
readonly
Returns the value of attribute scan_count.
Class Method Summary collapse
- .build(client:, key_prefix: UNSET, scan_count: UNSET, atomic_clear: false, **options) ⇒ Object
- .extract_key_prefix_camel!(options) ⇒ Object
- .redisStorage(client:, key_prefix: UNSET, scan_count: UNSET, atomic_clear: false, **options) ⇒ Object
- .reject_unknown_keywords!(options) ⇒ Object
- .resolve_key_prefix(key_prefix, key_prefix_camel) ⇒ Object
Instance Method Summary collapse
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #get(key) ⇒ Object
-
#initialize(client:, key_prefix: UNSET, key_prefix_camel: UNSET, scan_count: UNSET, atomic_clear: false, **options) ⇒ RedisStorage
constructor
A new instance of RedisStorage.
- #list_keys ⇒ Object (also: #listKeys)
- #set(key, value, ttl = nil) ⇒ Object
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, **) key_prefix_camel = self.class.extract_key_prefix_camel!() if key_prefix_camel.equal?(UNSET) self.class.reject_unknown_keywords!() @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_clear ⇒ Object (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 |
#client ⇒ Object (readonly)
Returns the value of attribute client.
13 14 15 |
# File 'lib/better_auth/redis_storage.rb', line 13 def client @client end |
#key_prefix ⇒ Object (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_count ⇒ Object (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, **) key_prefix_camel = extract_key_prefix_camel!() reject_unknown_keywords!() 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!() .key?(:keyPrefix) ? .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, **) key_prefix_camel = extract_key_prefix_camel!() reject_unknown_keywords!() 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
44 45 46 47 48 49 50 |
# File 'lib/better_auth/redis_storage.rb', line 44 def self.reject_unknown_keywords!() return if .empty? unknown = .keys.map(&:inspect).join(", ") label = (.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
#clear ⇒ Object
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_keys ⇒ Object 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 |