Class: ChatSDK::State::Redis
- Inherits:
-
Base
- Object
- Base
- ChatSDK::State::Redis
- Defined in:
- lib/chat_sdk/state/redis.rb
Constant Summary collapse
- LOCK_RELEASE_SCRIPT =
<<~LUA if redis.call("GET", KEYS[1]) == ARGV[1] then return redis.call("DEL", KEYS[1]) else return 0 end LUA
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
-
#acquire_lock(key, owner:, ttl:) ⇒ Object
Locks.
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #force_lock(key, owner:, ttl:) ⇒ Object
-
#get(key) ⇒ Object
Key-value store.
-
#initialize(url: nil, client: nil) ⇒ Redis
constructor
A new instance of Redis.
- #release_lock(key, owner:) ⇒ Object
- #set(key, value, ttl: nil) ⇒ Object
- #set_if_absent(key, value, ttl: nil) ⇒ Object
-
#subscribe(thread_id) ⇒ Object
Subscriptions.
- #subscribed?(thread_id) ⇒ Boolean
- #unsubscribe(thread_id) ⇒ Object
Constructor Details
#initialize(url: nil, client: nil) ⇒ Redis
Returns a new instance of Redis.
17 18 19 |
# File 'lib/chat_sdk/state/redis.rb', line 17 def initialize(url: nil, client: nil) @client = client || RedisClient.config(url: url || ENV["REDIS_URL"] || "redis://localhost:6379").new_client end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
21 22 23 |
# File 'lib/chat_sdk/state/redis.rb', line 21 def client @client end |
Instance Method Details
#acquire_lock(key, owner:, ttl:) ⇒ Object
Locks
37 38 39 40 |
# File 'lib/chat_sdk/state/redis.rb', line 37 def acquire_lock(key, owner:, ttl:) result = @client.call("SET", lock_key(key), owner, "NX", "PX", (ttl * 1000).to_i) result == "OK" end |
#clear ⇒ Object
82 83 84 85 |
# File 'lib/chat_sdk/state/redis.rb', line 82 def clear keys = @client.call("KEYS", "chat_sdk:*") @client.call("DEL", *keys) if keys.any? end |
#delete(key) ⇒ Object
68 69 70 |
# File 'lib/chat_sdk/state/redis.rb', line 68 def delete(key) @client.call("DEL", kv_key(key), lock_key(key)) end |
#force_lock(key, owner:, ttl:) ⇒ Object
47 48 49 50 |
# File 'lib/chat_sdk/state/redis.rb', line 47 def force_lock(key, owner:, ttl:) @client.call("SET", lock_key(key), owner, "PX", (ttl * 1000).to_i) true end |
#get(key) ⇒ Object
Key-value store
53 54 55 56 |
# File 'lib/chat_sdk/state/redis.rb', line 53 def get(key) value = @client.call("GET", kv_key(key)) value ? JSON.parse(value) : nil end |
#release_lock(key, owner:) ⇒ Object
42 43 44 45 |
# File 'lib/chat_sdk/state/redis.rb', line 42 def release_lock(key, owner:) result = @client.call("EVAL", LOCK_RELEASE_SCRIPT, 1, lock_key(key), owner) result == 1 end |
#set(key, value, ttl: nil) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/chat_sdk/state/redis.rb', line 58 def set(key, value, ttl: nil) serialized = JSON.generate(value) if ttl @client.call("SET", kv_key(key), serialized, "PX", (ttl * 1000).to_i) else @client.call("SET", kv_key(key), serialized) end value end |
#set_if_absent(key, value, ttl: nil) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/chat_sdk/state/redis.rb', line 72 def set_if_absent(key, value, ttl: nil) serialized = JSON.generate(value) result = if ttl @client.call("SET", kv_key(key), serialized, "NX", "PX", (ttl * 1000).to_i) else @client.call("SET", kv_key(key), serialized, "NX") end result == "OK" end |
#subscribe(thread_id) ⇒ Object
Subscriptions
24 25 26 |
# File 'lib/chat_sdk/state/redis.rb', line 24 def subscribe(thread_id) @client.call("SADD", subscription_key, thread_id) end |
#subscribed?(thread_id) ⇒ Boolean
32 33 34 |
# File 'lib/chat_sdk/state/redis.rb', line 32 def subscribed?(thread_id) @client.call("SISMEMBER", subscription_key, thread_id) == 1 end |
#unsubscribe(thread_id) ⇒ Object
28 29 30 |
# File 'lib/chat_sdk/state/redis.rb', line 28 def unsubscribe(thread_id) @client.call("SREM", subscription_key, thread_id) end |