chat_sdk-state-redis 0.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 +7 -0
- data/lib/chat_sdk/state/redis.rb +103 -0
- metadata +70 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: bf00ab776bc8282228a51a1fe054a8d620a858c78aeb8b3c63237126140220e3
|
|
4
|
+
data.tar.gz: '068c11e1a0b3c77194230c3f00aa6f88cb137018827e386ef66eaf0a90d1cafe'
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: e23084f2c837d4b59d33c4767c6042b3484b598c8ac90ceedc22505b1b796badd41ee7a16108b1c437d4ac238c24c9d93eea30571f74c71c55501e7d3f59acda
|
|
7
|
+
data.tar.gz: 42d7b384a71b8b046775228a7562efd15f24264abb47ccc1d8f50b508f27e39b685335eb6394efebf7c7dbd65e1f56730231bb21444fef7010040cd931511a85
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "chat_sdk"
|
|
4
|
+
require "redis-client"
|
|
5
|
+
|
|
6
|
+
module ChatSDK
|
|
7
|
+
module State
|
|
8
|
+
class Redis < Base
|
|
9
|
+
LOCK_RELEASE_SCRIPT = <<~LUA
|
|
10
|
+
if redis.call("GET", KEYS[1]) == ARGV[1] then
|
|
11
|
+
return redis.call("DEL", KEYS[1])
|
|
12
|
+
else
|
|
13
|
+
return 0
|
|
14
|
+
end
|
|
15
|
+
LUA
|
|
16
|
+
|
|
17
|
+
def initialize(url: nil, client: nil)
|
|
18
|
+
@client = client || RedisClient.config(url: url || ENV["REDIS_URL"] || "redis://localhost:6379").new_client
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
attr_reader :client
|
|
22
|
+
|
|
23
|
+
# Subscriptions
|
|
24
|
+
def subscribe(thread_id)
|
|
25
|
+
@client.call("SADD", subscription_key, thread_id)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def unsubscribe(thread_id)
|
|
29
|
+
@client.call("SREM", subscription_key, thread_id)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def subscribed?(thread_id)
|
|
33
|
+
@client.call("SISMEMBER", subscription_key, thread_id) == 1
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Locks
|
|
37
|
+
def acquire_lock(key, owner:, ttl:)
|
|
38
|
+
result = @client.call("SET", lock_key(key), owner, "NX", "PX", (ttl * 1000).to_i)
|
|
39
|
+
result == "OK"
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def release_lock(key, owner:)
|
|
43
|
+
result = @client.call("EVAL", LOCK_RELEASE_SCRIPT, 1, lock_key(key), owner)
|
|
44
|
+
result == 1
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def force_lock(key, owner:, ttl:)
|
|
48
|
+
@client.call("SET", lock_key(key), owner, "PX", (ttl * 1000).to_i)
|
|
49
|
+
true
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Key-value store
|
|
53
|
+
def get(key)
|
|
54
|
+
value = @client.call("GET", kv_key(key))
|
|
55
|
+
value ? JSON.parse(value) : nil
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def set(key, value, ttl: nil)
|
|
59
|
+
serialized = JSON.generate(value)
|
|
60
|
+
if ttl
|
|
61
|
+
@client.call("SET", kv_key(key), serialized, "PX", (ttl * 1000).to_i)
|
|
62
|
+
else
|
|
63
|
+
@client.call("SET", kv_key(key), serialized)
|
|
64
|
+
end
|
|
65
|
+
value
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def delete(key)
|
|
69
|
+
@client.call("DEL", kv_key(key))
|
|
70
|
+
@client.call("DEL", lock_key(key))
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def set_if_absent(key, value, ttl: nil)
|
|
74
|
+
serialized = JSON.generate(value)
|
|
75
|
+
result = if ttl
|
|
76
|
+
@client.call("SET", kv_key(key), serialized, "NX", "PX", (ttl * 1000).to_i)
|
|
77
|
+
else
|
|
78
|
+
@client.call("SET", kv_key(key), serialized, "NX")
|
|
79
|
+
end
|
|
80
|
+
result == "OK"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def clear
|
|
84
|
+
keys = @client.call("KEYS", "chat_sdk:*")
|
|
85
|
+
@client.call("DEL", *keys) if keys.any?
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
private
|
|
89
|
+
|
|
90
|
+
def subscription_key
|
|
91
|
+
"chat_sdk:subscriptions"
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def lock_key(key)
|
|
95
|
+
"chat_sdk:lock:#{key}"
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def kv_key(key)
|
|
99
|
+
"chat_sdk:kv:#{key}"
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: chat_sdk-state-redis
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Rootly
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: chat_sdk
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0.1'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0.1'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: redis-client
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0.19'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0.19'
|
|
40
|
+
description: Redis-backed conversation state storage for the ChatSDK framework
|
|
41
|
+
email:
|
|
42
|
+
- eng@rootly.com
|
|
43
|
+
executables: []
|
|
44
|
+
extensions: []
|
|
45
|
+
extra_rdoc_files: []
|
|
46
|
+
files:
|
|
47
|
+
- lib/chat_sdk/state/redis.rb
|
|
48
|
+
homepage: https://github.com/rootlyhq/rootly-chat-sdk
|
|
49
|
+
licenses:
|
|
50
|
+
- MIT
|
|
51
|
+
metadata:
|
|
52
|
+
rubygems_mfa_required: 'true'
|
|
53
|
+
rdoc_options: []
|
|
54
|
+
require_paths:
|
|
55
|
+
- lib
|
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: 3.2.0
|
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
|
+
requirements:
|
|
63
|
+
- - ">="
|
|
64
|
+
- !ruby/object:Gem::Version
|
|
65
|
+
version: '0'
|
|
66
|
+
requirements: []
|
|
67
|
+
rubygems_version: 4.0.16
|
|
68
|
+
specification_version: 4
|
|
69
|
+
summary: Redis state backend for ChatSDK
|
|
70
|
+
test_files: []
|