Class: ChatSDK::State::Memory
Instance Method Summary collapse
- #acquire_lock(key, owner:, ttl:) ⇒ Object
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #force_lock(key, owner:, ttl:) ⇒ Object
- #get(key) ⇒ Object
-
#initialize ⇒ Memory
constructor
A new instance of Memory.
- #release_lock(key, owner:) ⇒ Object
- #set(key, value, ttl: nil) ⇒ Object
- #set_if_absent(key, value, ttl: nil) ⇒ Object
- #subscribe(thread_id) ⇒ Object
- #subscribed?(thread_id) ⇒ Boolean
- #unsubscribe(thread_id) ⇒ Object
Constructor Details
#initialize ⇒ Memory
Returns a new instance of Memory.
6 7 8 9 10 11 12 |
# File 'lib/chat_sdk/state/memory.rb', line 6 def initialize @mutex = Mutex.new @store = {} @expirations = {} @subscriptions = Set.new @locks = {} end |
Instance Method Details
#acquire_lock(key, owner:, ttl:) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/chat_sdk/state/memory.rb', line 26 def acquire_lock(key, owner:, ttl:) @mutex.synchronize do expire_if_needed(key) return false if @locks.key?(key) @locks[key] = {owner: owner, expires_at: Time.now.to_f + ttl} true end end |
#clear ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/chat_sdk/state/memory.rb', line 83 def clear @mutex.synchronize do @store.clear @expirations.clear @subscriptions.clear @locks.clear end end |
#delete(key) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/chat_sdk/state/memory.rb', line 65 def delete(key) @mutex.synchronize do @store.delete(key) @expirations.delete(key) @locks.delete(key) end end |
#force_lock(key, owner:, ttl:) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/chat_sdk/state/memory.rb', line 43 def force_lock(key, owner:, ttl:) @mutex.synchronize do @locks[key] = {owner: owner, expires_at: Time.now.to_f + ttl} true end end |
#get(key) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/chat_sdk/state/memory.rb', line 50 def get(key) @mutex.synchronize do expire_if_needed(key) @store[key] end end |
#release_lock(key, owner:) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/chat_sdk/state/memory.rb', line 35 def release_lock(key, owner:) @mutex.synchronize do return false unless @locks[key] && @locks[key][:owner] == owner @locks.delete(key) true end end |
#set(key, value, ttl: nil) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/chat_sdk/state/memory.rb', line 57 def set(key, value, ttl: nil) @mutex.synchronize do @store[key] = value @expirations[key] = Time.now.to_f + ttl if ttl value end end |
#set_if_absent(key, value, ttl: nil) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/chat_sdk/state/memory.rb', line 73 def set_if_absent(key, value, ttl: nil) @mutex.synchronize do expire_if_needed(key) return false if @store.key?(key) @store[key] = value @expirations[key] = Time.now.to_f + ttl if ttl true end end |
#subscribe(thread_id) ⇒ Object
14 15 16 |
# File 'lib/chat_sdk/state/memory.rb', line 14 def subscribe(thread_id) @mutex.synchronize { @subscriptions.add(thread_id) } end |
#subscribed?(thread_id) ⇒ Boolean
22 23 24 |
# File 'lib/chat_sdk/state/memory.rb', line 22 def subscribed?(thread_id) @mutex.synchronize { @subscriptions.include?(thread_id) } end |
#unsubscribe(thread_id) ⇒ Object
18 19 20 |
# File 'lib/chat_sdk/state/memory.rb', line 18 def unsubscribe(thread_id) @mutex.synchronize { @subscriptions.delete(thread_id) } end |