redis 4.4.0 → 4.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +96 -0
- data/README.md +25 -10
- data/lib/redis/client.rb +31 -25
- data/lib/redis/cluster/command.rb +4 -6
- data/lib/redis/cluster/command_loader.rb +8 -9
- data/lib/redis/cluster/node.rb +12 -0
- data/lib/redis/cluster/node_loader.rb +8 -11
- data/lib/redis/cluster/option.rb +10 -3
- data/lib/redis/cluster/slot_loader.rb +9 -12
- data/lib/redis/cluster.rb +24 -0
- data/lib/redis/commands/bitmaps.rb +63 -0
- data/lib/redis/commands/cluster.rb +45 -0
- data/lib/redis/commands/connection.rb +58 -0
- data/lib/redis/commands/geo.rb +84 -0
- data/lib/redis/commands/hashes.rb +251 -0
- data/lib/redis/commands/hyper_log_log.rb +37 -0
- data/lib/redis/commands/keys.rb +455 -0
- data/lib/redis/commands/lists.rb +290 -0
- data/lib/redis/commands/pubsub.rb +72 -0
- data/lib/redis/commands/scripting.rb +114 -0
- data/lib/redis/commands/server.rb +188 -0
- data/lib/redis/commands/sets.rb +223 -0
- data/lib/redis/commands/sorted_sets.rb +812 -0
- data/lib/redis/commands/streams.rb +382 -0
- data/lib/redis/commands/strings.rb +313 -0
- data/lib/redis/commands/transactions.rb +139 -0
- data/lib/redis/commands.rb +240 -0
- data/lib/redis/connection/command_helper.rb +2 -0
- data/lib/redis/connection/hiredis.rb +3 -2
- data/lib/redis/connection/ruby.rb +19 -9
- data/lib/redis/connection/synchrony.rb +10 -8
- data/lib/redis/connection.rb +1 -1
- data/lib/redis/distributed.rb +111 -23
- data/lib/redis/errors.rb +9 -0
- data/lib/redis/pipeline.rb +128 -3
- data/lib/redis/version.rb +1 -1
- data/lib/redis.rb +138 -3482
- metadata +22 -5
@@ -0,0 +1,223 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Redis
|
4
|
+
module Commands
|
5
|
+
module Sets
|
6
|
+
# Get the number of members in a set.
|
7
|
+
#
|
8
|
+
# @param [String] key
|
9
|
+
# @return [Integer]
|
10
|
+
def scard(key)
|
11
|
+
send_command([:scard, key])
|
12
|
+
end
|
13
|
+
|
14
|
+
# Add one or more members to a set.
|
15
|
+
#
|
16
|
+
# @param [String] key
|
17
|
+
# @param [String, Array<String>] member one member, or array of members
|
18
|
+
# @return [Boolean, Integer] `Boolean` when a single member is specified,
|
19
|
+
# holding whether or not adding the member succeeded, or `Integer` when an
|
20
|
+
# array of members is specified, holding the number of members that were
|
21
|
+
# successfully added
|
22
|
+
def sadd(key, member)
|
23
|
+
block = if Redis.sadd_returns_boolean && !member.is_a?(Array)
|
24
|
+
::Redis.deprecate!(
|
25
|
+
"Redis#sadd will always return an Integer in Redis 5.0.0. Use Redis#sadd? instead." \
|
26
|
+
"(called from: #{caller(1, 1).first})"
|
27
|
+
)
|
28
|
+
Boolify
|
29
|
+
end
|
30
|
+
send_command([:sadd, key, member], &block)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Add one or more members to a set.
|
34
|
+
#
|
35
|
+
# @param [String] key
|
36
|
+
# @param [String, Array<String>] member one member, or array of members
|
37
|
+
# @return [Boolean] Whether or not at least one member was added.
|
38
|
+
def sadd?(key, member)
|
39
|
+
send_command([:sadd, key, member], &Boolify)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Remove one or more members from a set.
|
43
|
+
#
|
44
|
+
# @param [String] key
|
45
|
+
# @param [String, Array<String>] member one member, or array of members
|
46
|
+
# @return [Boolean, Integer] `Boolean` when a single member is specified,
|
47
|
+
# holding whether or not removing the member succeeded, or `Integer` when an
|
48
|
+
# array of members is specified, holding the number of members that were
|
49
|
+
# successfully removed
|
50
|
+
def srem(key, member)
|
51
|
+
block = if Redis.sadd_returns_boolean && !member.is_a?(Array)
|
52
|
+
::Redis.deprecate!(
|
53
|
+
"Redis#srem will always return an Integer in Redis 5.0.0. Use Redis#srem? instead." \
|
54
|
+
"(called from: #{caller(1, 1).first})"
|
55
|
+
)
|
56
|
+
Boolify
|
57
|
+
end
|
58
|
+
send_command([:srem, key, member], &block)
|
59
|
+
end
|
60
|
+
|
61
|
+
# Remove one or more members from a set.
|
62
|
+
#
|
63
|
+
# @param [String] key
|
64
|
+
# @param [String, Array<String>] member one member, or array of members
|
65
|
+
# @return [Boolean] `Boolean` Whether or not a member was removed.
|
66
|
+
def srem?(key, member)
|
67
|
+
send_command([:srem, key, member], &Boolify)
|
68
|
+
end
|
69
|
+
|
70
|
+
# Remove and return one or more random member from a set.
|
71
|
+
#
|
72
|
+
# @param [String] key
|
73
|
+
# @return [String]
|
74
|
+
# @param [Integer] count
|
75
|
+
def spop(key, count = nil)
|
76
|
+
if count.nil?
|
77
|
+
send_command([:spop, key])
|
78
|
+
else
|
79
|
+
send_command([:spop, key, count])
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# Get one or more random members from a set.
|
84
|
+
#
|
85
|
+
# @param [String] key
|
86
|
+
# @param [Integer] count
|
87
|
+
# @return [String]
|
88
|
+
def srandmember(key, count = nil)
|
89
|
+
if count.nil?
|
90
|
+
send_command([:srandmember, key])
|
91
|
+
else
|
92
|
+
send_command([:srandmember, key, count])
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# Move a member from one set to another.
|
97
|
+
#
|
98
|
+
# @param [String] source source key
|
99
|
+
# @param [String] destination destination key
|
100
|
+
# @param [String] member member to move from `source` to `destination`
|
101
|
+
# @return [Boolean]
|
102
|
+
def smove(source, destination, member)
|
103
|
+
send_command([:smove, source, destination, member], &Boolify)
|
104
|
+
end
|
105
|
+
|
106
|
+
# Determine if a given value is a member of a set.
|
107
|
+
#
|
108
|
+
# @param [String] key
|
109
|
+
# @param [String] member
|
110
|
+
# @return [Boolean]
|
111
|
+
def sismember(key, member)
|
112
|
+
send_command([:sismember, key, member], &Boolify)
|
113
|
+
end
|
114
|
+
|
115
|
+
# Determine if multiple values are members of a set.
|
116
|
+
#
|
117
|
+
# @param [String] key
|
118
|
+
# @param [String, Array<String>] members
|
119
|
+
# @return [Array<Boolean>]
|
120
|
+
def smismember(key, *members)
|
121
|
+
send_command([:smismember, key, *members]) do |reply|
|
122
|
+
reply.map(&Boolify)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
# Get all the members in a set.
|
127
|
+
#
|
128
|
+
# @param [String] key
|
129
|
+
# @return [Array<String>]
|
130
|
+
def smembers(key)
|
131
|
+
send_command([:smembers, key])
|
132
|
+
end
|
133
|
+
|
134
|
+
# Subtract multiple sets.
|
135
|
+
#
|
136
|
+
# @param [String, Array<String>] keys keys pointing to sets to subtract
|
137
|
+
# @return [Array<String>] members in the difference
|
138
|
+
def sdiff(*keys)
|
139
|
+
send_command([:sdiff, *keys])
|
140
|
+
end
|
141
|
+
|
142
|
+
# Subtract multiple sets and store the resulting set in a key.
|
143
|
+
#
|
144
|
+
# @param [String] destination destination key
|
145
|
+
# @param [String, Array<String>] keys keys pointing to sets to subtract
|
146
|
+
# @return [Integer] number of elements in the resulting set
|
147
|
+
def sdiffstore(destination, *keys)
|
148
|
+
send_command([:sdiffstore, destination, *keys])
|
149
|
+
end
|
150
|
+
|
151
|
+
# Intersect multiple sets.
|
152
|
+
#
|
153
|
+
# @param [String, Array<String>] keys keys pointing to sets to intersect
|
154
|
+
# @return [Array<String>] members in the intersection
|
155
|
+
def sinter(*keys)
|
156
|
+
send_command([:sinter, *keys])
|
157
|
+
end
|
158
|
+
|
159
|
+
# Intersect multiple sets and store the resulting set in a key.
|
160
|
+
#
|
161
|
+
# @param [String] destination destination key
|
162
|
+
# @param [String, Array<String>] keys keys pointing to sets to intersect
|
163
|
+
# @return [Integer] number of elements in the resulting set
|
164
|
+
def sinterstore(destination, *keys)
|
165
|
+
send_command([:sinterstore, destination, *keys])
|
166
|
+
end
|
167
|
+
|
168
|
+
# Add multiple sets.
|
169
|
+
#
|
170
|
+
# @param [String, Array<String>] keys keys pointing to sets to unify
|
171
|
+
# @return [Array<String>] members in the union
|
172
|
+
def sunion(*keys)
|
173
|
+
send_command([:sunion, *keys])
|
174
|
+
end
|
175
|
+
|
176
|
+
# Add multiple sets and store the resulting set in a key.
|
177
|
+
#
|
178
|
+
# @param [String] destination destination key
|
179
|
+
# @param [String, Array<String>] keys keys pointing to sets to unify
|
180
|
+
# @return [Integer] number of elements in the resulting set
|
181
|
+
def sunionstore(destination, *keys)
|
182
|
+
send_command([:sunionstore, destination, *keys])
|
183
|
+
end
|
184
|
+
|
185
|
+
# Scan a set
|
186
|
+
#
|
187
|
+
# @example Retrieve the first batch of keys in a set
|
188
|
+
# redis.sscan("set", 0)
|
189
|
+
#
|
190
|
+
# @param [String, Integer] cursor the cursor of the iteration
|
191
|
+
# @param [Hash] options
|
192
|
+
# - `:match => String`: only return keys matching the pattern
|
193
|
+
# - `:count => Integer`: return count keys at most per iteration
|
194
|
+
#
|
195
|
+
# @return [String, Array<String>] the next cursor and all found members
|
196
|
+
def sscan(key, cursor, **options)
|
197
|
+
_scan(:sscan, cursor, [key], **options)
|
198
|
+
end
|
199
|
+
|
200
|
+
# Scan a set
|
201
|
+
#
|
202
|
+
# @example Retrieve all of the keys in a set
|
203
|
+
# redis.sscan_each("set").to_a
|
204
|
+
# # => ["key1", "key2", "key3"]
|
205
|
+
#
|
206
|
+
# @param [Hash] options
|
207
|
+
# - `:match => String`: only return keys matching the pattern
|
208
|
+
# - `:count => Integer`: return count keys at most per iteration
|
209
|
+
#
|
210
|
+
# @return [Enumerator] an enumerator for all keys in the set
|
211
|
+
def sscan_each(key, **options, &block)
|
212
|
+
return to_enum(:sscan_each, key, **options) unless block_given?
|
213
|
+
|
214
|
+
cursor = 0
|
215
|
+
loop do
|
216
|
+
cursor, keys = sscan(key, cursor, **options)
|
217
|
+
keys.each(&block)
|
218
|
+
break if cursor == "0"
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|