Class: AtprotoAuth::Storage::Memory
- Defined in:
- lib/atproto_auth/storage/memory.rb
Overview
Thread-safe in-memory storage implementation
Instance Method Summary collapse
- #acquire_lock(key, ttl:) ⇒ Object
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #exists?(key) ⇒ Boolean
- #get(key) ⇒ Object
-
#initialize ⇒ Memory
constructor
A new instance of Memory.
- #multi_get(keys) ⇒ Object
- #multi_set(hash, ttl: nil) ⇒ Object
- #release_lock(key) ⇒ Object
- #set(key, value, ttl: nil) ⇒ Object
- #with_lock(key, ttl: 30) ⇒ Object
Constructor Details
#initialize ⇒ Memory
Returns a new instance of Memory.
9 10 11 12 13 14 15 16 17 |
# File 'lib/atproto_auth/storage/memory.rb', line 9 def initialize super @data = {} @locks = {} @expirations = {} @monitor = Monitor.new @cleanup_interval = 60 # 1 minute start_cleanup_thread end |
Instance Method Details
#acquire_lock(key, ttl:) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/atproto_auth/storage/memory.rb', line 86 def acquire_lock(key, ttl:) validate_key!(key) validate_ttl!(ttl) lock_key = "lock:#{key}" @monitor.synchronize do return false if @locks[lock_key] && !lock_expired?(lock_key) @locks[lock_key] = Time.now.to_i + ttl true end end |
#clear ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/atproto_auth/storage/memory.rb', line 122 def clear @monitor.synchronize do @data.clear @expirations.clear @locks.clear end end |
#delete(key) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/atproto_auth/storage/memory.rb', line 40 def delete(key) validate_key!(key) @monitor.synchronize do @data.delete(key) @expirations.delete(key) true end end |
#exists?(key) ⇒ Boolean
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/atproto_auth/storage/memory.rb', line 50 def exists?(key) validate_key!(key) @monitor.synchronize do return false unless @data.key?(key) return false if expired?(key) true end end |
#get(key) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/atproto_auth/storage/memory.rb', line 30 def get(key) validate_key!(key) @monitor.synchronize do return nil if expired?(key) @data[key] end end |
#multi_get(keys) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/atproto_auth/storage/memory.rb', line 61 def multi_get(keys) keys.each { |key| validate_key!(key) } @monitor.synchronize do result = {} keys.each do |key| result[key] = @data[key] if @data.key?(key) && !expired?(key) end result end end |
#multi_set(hash, ttl: nil) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/atproto_auth/storage/memory.rb', line 73 def multi_set(hash, ttl: nil) hash.each_key { |key| validate_key!(key) } validate_ttl!(ttl) @monitor.synchronize do hash.each do |key, value| @data[key] = value set_expiration(key, ttl) if ttl end true end end |
#release_lock(key) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/atproto_auth/storage/memory.rb', line 99 def release_lock(key) validate_key!(key) lock_key = "lock:#{key}" @monitor.synchronize do @locks.delete(lock_key) true end end |
#set(key, value, ttl: nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/atproto_auth/storage/memory.rb', line 19 def set(key, value, ttl: nil) validate_key!(key) validate_ttl!(ttl) @monitor.synchronize do @data[key] = value set_expiration(key, ttl) if ttl true end end |
#with_lock(key, ttl: 30) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/atproto_auth/storage/memory.rb', line 109 def with_lock(key, ttl: 30) raise ArgumentError, "Block required" unless block_given? acquired = acquire_lock(key, ttl: ttl) raise LockError, "Failed to acquire lock" unless acquired begin yield ensure release_lock(key) end end |