redis 4.5.1 → 5.0.6

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