specwrk-store-redis_adapter 0.0.4 → 0.0.5
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/specwrk/store/redis_adapter.rb +24 -33
- 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: 626a515c92546cb68ff4378393f6bdf02af48e022c0cb481990eab35cdcdedbd
|
4
|
+
data.tar.gz: e4c2a3d80b9585ab9ae554a6cc470b37f66223f374a09876c5e35bddd0c4aa44
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 35cdba9ff17f1c868b0968b17d4ccd1c90842d4a06b0c8cae7dd4ea9c9b2726b3480c056ef505ca9fdf3d961d54721015d4d2a667f6f6e3a91c2d5f61bca3ea8
|
7
|
+
data.tar.gz: d52567dfc1112d99a91809ef3edb754269ef0cccc4d7988293c3a74e1a333ef33578bac596d5074625ec59a0a405bfffd5c992a4954fe4a2151c751716d29d69
|
@@ -9,14 +9,14 @@ require "redis-client"
|
|
9
9
|
module Specwrk
|
10
10
|
class Store
|
11
11
|
class RedisAdapter < Specwrk::Store::BaseAdapter
|
12
|
-
REDIS_KEY_DELIMITER = "||||"
|
13
|
-
|
14
12
|
@connection_pools = {}
|
15
13
|
@mutex = Mutex.new
|
16
14
|
|
17
15
|
class << self
|
18
16
|
def with_lock(uri, key)
|
19
17
|
connection_pool_for(uri).with do |connection|
|
18
|
+
Thread.current[:connection] = connection
|
19
|
+
|
20
20
|
id = SecureRandom.uuid
|
21
21
|
queue = "specwrk-lock-#{key}"
|
22
22
|
connection.pipelined do |pipeline|
|
@@ -25,7 +25,7 @@ module Specwrk
|
|
25
25
|
end
|
26
26
|
|
27
27
|
# wait for our id to be first in line or the queue to expire
|
28
|
-
|
28
|
+
Thread.pass until [id, nil].include? connection.call("LINDEX", queue, 0)
|
29
29
|
|
30
30
|
yield
|
31
31
|
ensure
|
@@ -33,6 +33,8 @@ module Specwrk
|
|
33
33
|
pipeline.call("LPOP", queue)
|
34
34
|
pipeline.call("EXPIRE", queue, 10) # keeps the queue fresh when things are moving
|
35
35
|
end
|
36
|
+
|
37
|
+
Thread.current[:connection] = nil
|
36
38
|
end
|
37
39
|
end
|
38
40
|
|
@@ -41,7 +43,7 @@ module Specwrk
|
|
41
43
|
|
42
44
|
@mutex.synchronize do
|
43
45
|
@connection_pools[uri] ||= RedisClient.config(url: uri).new_pool(
|
44
|
-
size: ENV.fetch("SPECWRK_THREAD_COUNT", "4").to_i
|
46
|
+
size: ENV.fetch("SPECWRK_THREAD_COUNT", ENV.fetch("SPECWRK_COUNT", "4")).to_i
|
45
47
|
)
|
46
48
|
end
|
47
49
|
end
|
@@ -53,32 +55,34 @@ module Specwrk
|
|
53
55
|
|
54
56
|
def [](key)
|
55
57
|
with_connection do |redis|
|
56
|
-
value = redis.call("
|
58
|
+
value = redis.call("HGET", scope, key)
|
57
59
|
JSON.parse(value, symbolize_names: true) if value
|
58
60
|
end
|
59
61
|
end
|
60
62
|
|
61
63
|
def []=(key, value)
|
62
64
|
with_connection do |redis|
|
63
|
-
redis.call("
|
65
|
+
redis.call("HSET", scope, key, JSON.generate(value))
|
64
66
|
end
|
65
67
|
end
|
66
68
|
|
67
69
|
def keys
|
68
|
-
|
69
|
-
|
70
|
+
with_connection do |redis|
|
71
|
+
redis.call("HKEYS", scope)
|
70
72
|
end
|
71
73
|
end
|
72
74
|
|
73
75
|
def clear
|
74
|
-
|
76
|
+
with_connection do |redis|
|
77
|
+
redis.call("DEL", scope)
|
78
|
+
end
|
75
79
|
end
|
76
80
|
|
77
81
|
def delete(*keys)
|
78
82
|
return if keys.length.zero?
|
79
83
|
|
80
84
|
with_connection do |redis|
|
81
|
-
redis.call("
|
85
|
+
redis.call("HDEL", scope, *keys)
|
82
86
|
end
|
83
87
|
end
|
84
88
|
|
@@ -90,7 +94,7 @@ module Specwrk
|
|
90
94
|
return {} if read_keys.length.zero?
|
91
95
|
|
92
96
|
values = with_connection do |redis|
|
93
|
-
redis.call("
|
97
|
+
redis.call("HMGET", scope, *read_keys)
|
94
98
|
end
|
95
99
|
|
96
100
|
result = {}
|
@@ -107,37 +111,24 @@ module Specwrk
|
|
107
111
|
return if hash.nil? || hash.length.zero?
|
108
112
|
|
109
113
|
with_connection do |redis|
|
110
|
-
redis.call("
|
114
|
+
redis.call("HMSET", scope, *hash.flat_map { |key, value| [key, JSON.generate(value)] })
|
111
115
|
end
|
112
116
|
end
|
113
117
|
|
114
118
|
def empty?
|
115
|
-
|
119
|
+
with_connection do |redis|
|
120
|
+
redis.call("HLEN", scope).zero?
|
121
|
+
end
|
116
122
|
end
|
117
123
|
|
118
124
|
private
|
119
125
|
|
120
126
|
def with_connection
|
121
|
-
|
122
|
-
yield connection
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
def encode_key(key)
|
127
|
-
[scope, REDIS_KEY_DELIMITER, key].join
|
128
|
-
end
|
129
|
-
|
130
|
-
def decode_key(key)
|
131
|
-
key.split(REDIS_KEY_DELIMITER).last
|
132
|
-
end
|
133
|
-
|
134
|
-
def scan_for(match)
|
135
|
-
with_connection do |redis|
|
136
|
-
cursor = "0"
|
137
|
-
loop do
|
138
|
-
cursor, batch = redis.call("SCAN", cursor, "MATCH", match, "COUNT", 5_000)
|
139
|
-
batch.each { |k| yield k }
|
140
|
-
break if cursor == "0"
|
127
|
+
if Thread.current[:connection]
|
128
|
+
yield Thread.current[:connection]
|
129
|
+
else
|
130
|
+
self.class.connection_pool_for(uri).with do |connection|
|
131
|
+
yield connection
|
141
132
|
end
|
142
133
|
end
|
143
134
|
end
|