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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1d04547dbffe8b9129a823f463c65ecf55a5e546d98df580f0b31cfa00be6e49
4
- data.tar.gz: acdd63b88bd20fbd00630c672b8636576df932bb8a5e21556638628b4f4e7e6f
3
+ metadata.gz: 626a515c92546cb68ff4378393f6bdf02af48e022c0cb481990eab35cdcdedbd
4
+ data.tar.gz: e4c2a3d80b9585ab9ae554a6cc470b37f66223f374a09876c5e35bddd0c4aa44
5
5
  SHA512:
6
- metadata.gz: 1a3a323c1d16651cbf4880e35deeae665db6df0aaf88d454a16417a6ea8f076dde4ee3a593a3fc4567e42421343ec313537dd4cc66baf35b6677a81642af8924
7
- data.tar.gz: 7bc076bd89ae1993d58155efbec15b17130e2c689102d10e2b0ce670d61e752f46086b150aa2b6883bc019d1c886e2ac0b12edb5a210d8ecd26f39209bd94a7d
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
- sleep(rand(0.001..0.012)) until [id, nil].include? connection.call("LINDEX", queue, 0)
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("GET", encode_key(key))
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("SET", encode_key(key), JSON.generate(value))
65
+ redis.call("HSET", scope, key, JSON.generate(value))
64
66
  end
65
67
  end
66
68
 
67
69
  def keys
68
- [].tap do |collected|
69
- scan_for("#{scope}#{REDIS_KEY_DELIMITER}*") { |k| collected << decode_key(k) }
70
+ with_connection do |redis|
71
+ redis.call("HKEYS", scope)
70
72
  end
71
73
  end
72
74
 
73
75
  def clear
74
- delete(*keys)
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("DEL", *keys.map { |key| encode_key key })
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("MGET", *read_keys.map { |key| encode_key(key) })
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("MSET", *hash.flat_map { |key, value| [encode_key(key), JSON.generate(value)] })
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
- keys.length.zero?
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
- self.class.connection_pool_for(uri).with do |connection|
122
- yield connection
123
- end
124
- end
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: specwrk-store-redis_adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Westendorf