chat_sdk-state-redis 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/chat_sdk/state/redis.rb +39 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b1e592ffab62e9fc105c1f55620c3fdbeddd37ca61027be8a15d476b91f36ebe
|
|
4
|
+
data.tar.gz: 4f37d31f179c82b6df000949d4ea37a4b6f19107ea07b0630e21a94804055188
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b3d6b130dd3c721ee99c6d808b95611bed7284852c4d32d6dde008db7f8396c25079d7bd1d02abf5df1d2ec77d1d9c6fd022eadff1932bd147bafed4fa7f712b
|
|
7
|
+
data.tar.gz: d93816479daea44f257c1be264c119b64c7cb456c562c347b105d8b4cf96da4ae774092f1227f38b221329893c49e188bfcad5cfdb87009daf9307375773bc54
|
data/lib/chat_sdk/state/redis.rb
CHANGED
|
@@ -13,6 +13,18 @@ module ChatSDK
|
|
|
13
13
|
return 0
|
|
14
14
|
end
|
|
15
15
|
LUA
|
|
16
|
+
LOCK_EXTEND_SCRIPT = <<~LUA
|
|
17
|
+
if redis.call("GET", KEYS[1]) == ARGV[1] then
|
|
18
|
+
return redis.call("PEXPIRE", KEYS[1], ARGV[2])
|
|
19
|
+
else
|
|
20
|
+
return 0
|
|
21
|
+
end
|
|
22
|
+
LUA
|
|
23
|
+
QUEUE_DRAIN_SCRIPT = <<~LUA
|
|
24
|
+
local values = redis.call("LRANGE", KEYS[1], 0, -1)
|
|
25
|
+
redis.call("DEL", KEYS[1])
|
|
26
|
+
return values
|
|
27
|
+
LUA
|
|
16
28
|
|
|
17
29
|
def initialize(url: nil, client: nil)
|
|
18
30
|
@client = client || RedisClient.config(url: url || ENV["REDIS_URL"] || "redis://localhost:6379").new_client
|
|
@@ -49,6 +61,10 @@ module ChatSDK
|
|
|
49
61
|
true
|
|
50
62
|
end
|
|
51
63
|
|
|
64
|
+
def extend_lock(key, owner:, ttl:)
|
|
65
|
+
@client.call("EVAL", LOCK_EXTEND_SCRIPT, 1, lock_key(key), owner, (ttl * 1000).to_i) == 1
|
|
66
|
+
end
|
|
67
|
+
|
|
52
68
|
# Key-value store
|
|
53
69
|
def get(key)
|
|
54
70
|
value = @client.call("GET", kv_key(key))
|
|
@@ -79,6 +95,25 @@ module ChatSDK
|
|
|
79
95
|
result == "OK"
|
|
80
96
|
end
|
|
81
97
|
|
|
98
|
+
def enqueue(key, value, max_size:, drop: :drop_oldest)
|
|
99
|
+
qkey = queue_storage_key(key)
|
|
100
|
+
if drop.to_sym == :drop_newest && @client.call("LLEN", qkey) >= max_size
|
|
101
|
+
return @client.call("LLEN", qkey)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
@client.call("RPUSH", qkey, JSON.generate(value))
|
|
105
|
+
@client.call("LTRIM", qkey, -max_size, -1)
|
|
106
|
+
@client.call("LLEN", qkey)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def drain_queue(key)
|
|
110
|
+
@client.call("EVAL", QUEUE_DRAIN_SCRIPT, 1, queue_storage_key(key)).map { |value| JSON.parse(value) }
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def queue_depth(key)
|
|
114
|
+
@client.call("LLEN", queue_storage_key(key))
|
|
115
|
+
end
|
|
116
|
+
|
|
82
117
|
def clear
|
|
83
118
|
keys = @client.call("KEYS", "chat_sdk:*")
|
|
84
119
|
@client.call("DEL", *keys) if keys.any?
|
|
@@ -97,6 +132,10 @@ module ChatSDK
|
|
|
97
132
|
def kv_key(key)
|
|
98
133
|
"chat_sdk:kv:#{key}"
|
|
99
134
|
end
|
|
135
|
+
|
|
136
|
+
def queue_storage_key(key)
|
|
137
|
+
"chat_sdk:queue:#{key}"
|
|
138
|
+
end
|
|
100
139
|
end
|
|
101
140
|
end
|
|
102
141
|
end
|