@@ -18,7 +18,7 @@ import NIO
18
18
19
19
extension RedisClient {
20
20
@usableFromInline
21
- static func _mapHashResponse( _ values: [ String ] ) -> [ String : String ] {
21
+ internal static func _mapHashResponse( _ values: [ String ] ) -> [ String : String ] {
22
22
guard values. count > 0 else { return [ : ] }
23
23
24
24
var result : [ String : String ] = [ : ]
@@ -48,8 +48,11 @@ extension RedisClient {
48
48
@inlinable
49
49
public func hdel( _ fields: [ String ] , from key: String ) -> EventLoopFuture < Int > {
50
50
guard fields. count > 0 else { return self . eventLoop. makeSucceededFuture ( 0 ) }
51
+
52
+ var args : [ RESPValue ] = [ . init( bulk: key) ]
53
+ args. append ( convertingContentsOf: fields)
51
54
52
- return send ( command: " HDEL " , with: [ key ] + fields )
55
+ return send ( command: " HDEL " , with: args )
53
56
. convertFromRESPValue ( )
54
57
}
55
58
@@ -62,7 +65,11 @@ extension RedisClient {
62
65
/// - Returns: `true` if the hash contains the field, `false` if either the key or field do not exist.
63
66
@inlinable
64
67
public func hexists( _ field: String , in key: String ) -> EventLoopFuture < Bool > {
65
- return send ( command: " HEXISTS " , with: [ key, field] )
68
+ let args : [ RESPValue ] = [
69
+ . init( bulk: key) ,
70
+ . init( bulk: field)
71
+ ]
72
+ return send ( command: " HEXISTS " , with: args)
66
73
. convertFromRESPValue ( to: Int . self)
67
74
. map { return $0 == 1 }
68
75
}
@@ -74,7 +81,8 @@ extension RedisClient {
74
81
/// - Returns: The number of fields in the hash, or `0` if the key doesn't exist.
75
82
@inlinable
76
83
public func hlen( of key: String ) -> EventLoopFuture < Int > {
77
- return send ( command: " HLEN " , with: [ key] )
84
+ let args = [ RESPValue ( bulk: key) ]
85
+ return send ( command: " HLEN " , with: args)
78
86
. convertFromRESPValue ( )
79
87
}
80
88
@@ -87,7 +95,11 @@ extension RedisClient {
87
95
/// - Returns: The string length of the hash field's value, or `0` if the field or hash do not exist.
88
96
@inlinable
89
97
public func hstrlen( of field: String , in key: String ) -> EventLoopFuture < Int > {
90
- return send ( command: " HSTRLEN " , with: [ key, field] )
98
+ let args : [ RESPValue ] = [
99
+ . init( bulk: key) ,
100
+ . init( bulk: field)
101
+ ]
102
+ return send ( command: " HSTRLEN " , with: args)
91
103
. convertFromRESPValue ( )
92
104
}
93
105
@@ -98,7 +110,8 @@ extension RedisClient {
98
110
/// - Returns: A list of field names stored within the hash.
99
111
@inlinable
100
112
public func hkeys( in key: String ) -> EventLoopFuture < [ String ] > {
101
- return send ( command: " HKEYS " , with: [ key] )
113
+ let args = [ RESPValue ( bulk: key) ]
114
+ return send ( command: " HKEYS " , with: args)
102
115
. convertFromRESPValue ( )
103
116
}
104
117
@@ -109,7 +122,8 @@ extension RedisClient {
109
122
/// - Returns: A list of all values stored in a hash.
110
123
@inlinable
111
124
public func hvals( in key: String ) -> EventLoopFuture < [ RESPValue ] > {
112
- return send ( command: " HVALS " , with: [ key] )
125
+ let args = [ RESPValue ( bulk: key) ]
126
+ return send ( command: " HVALS " , with: args)
113
127
. convertFromRESPValue ( )
114
128
}
115
129
@@ -150,12 +164,17 @@ extension RedisClient {
150
164
/// - key: The key that holds the hash.
151
165
/// - Returns: `true` if the hash was created, `false` if it was updated.
152
166
@inlinable
153
- public func hset(
167
+ public func hset< Value : RESPValueConvertible > (
154
168
_ field: String ,
155
- to value: RESPValueConvertible ,
169
+ to value: Value ,
156
170
in key: String
157
171
) -> EventLoopFuture < Bool > {
158
- return send ( command: " HSET " , with: [ key, field, value] )
172
+ let args : [ RESPValue ] = [
173
+ . init( bulk: key) ,
174
+ . init( bulk: field) ,
175
+ value. convertedToRESPValue ( )
176
+ ]
177
+ return send ( command: " HSET " , with: args)
159
178
. convertFromRESPValue ( to: Int . self)
160
179
. map { return $0 == 1 }
161
180
}
@@ -170,12 +189,17 @@ extension RedisClient {
170
189
/// - key: The key that holds the hash.
171
190
/// - Returns: `true` if the hash was created.
172
191
@inlinable
173
- public func hsetnx(
192
+ public func hsetnx< Value : RESPValueConvertible > (
174
193
_ field: String ,
175
- to value: RESPValueConvertible ,
194
+ to value: Value ,
176
195
in key: String
177
196
) -> EventLoopFuture < Bool > {
178
- return send ( command: " HSETNX " , with: [ key, field, value] )
197
+ let args : [ RESPValue ] = [
198
+ . init( bulk: key) ,
199
+ . init( bulk: field) ,
200
+ value. convertedToRESPValue ( )
201
+ ]
202
+ return send ( command: " HSETNX " , with: args)
179
203
. convertFromRESPValue ( to: Int . self)
180
204
. map { return $0 == 1 }
181
205
}
@@ -188,18 +212,19 @@ extension RedisClient {
188
212
/// - key: The key that holds the hash.
189
213
/// - Returns: An `EventLoopFuture` that resolves when the operation has succeeded, or fails with a `RedisError`.
190
214
@inlinable
191
- public func hmset(
192
- _ fields: [ String : RESPValueConvertible ] ,
215
+ public func hmset< Value : RESPValueConvertible > (
216
+ _ fields: [ String : Value ] ,
193
217
in key: String
194
218
) -> EventLoopFuture < Void > {
195
219
assert ( fields. count > 0 , " At least 1 key-value pair should be specified " )
196
220
197
- let args : [ RESPValueConvertible ] = fields. reduce ( into: [ ] , { ( result, element) in
198
- result. append ( element. key)
199
- result. append ( element. value)
200
- } )
221
+ var args : [ RESPValue ] = [ . init( bulk: key) ]
222
+ args. add ( contentsOf: fields, overestimatedCountBeingAdded: fields. count * 2 ) { ( array, element) in
223
+ array. append ( . init( bulk: element. key) )
224
+ array. append ( element. value. convertedToRESPValue ( ) )
225
+ }
201
226
202
- return send ( command: " HMSET " , with: [ key ] + args)
227
+ return send ( command: " HMSET " , with: args)
203
228
. map { _ in ( ) }
204
229
}
205
230
}
@@ -216,7 +241,11 @@ extension RedisClient {
216
241
/// - Returns: The value of the hash field, or `nil` if either the key or field does not exist.
217
242
@inlinable
218
243
public func hget( _ field: String , from key: String ) -> EventLoopFuture < String ? > {
219
- return send ( command: " HGET " , with: [ key, field] )
244
+ let args : [ RESPValue ] = [
245
+ . init( bulk: key) ,
246
+ . init( bulk: field)
247
+ ]
248
+ return send ( command: " HGET " , with: args)
220
249
. map { return String ( fromRESP: $0) }
221
250
}
222
251
@@ -230,8 +259,11 @@ extension RedisClient {
230
259
@inlinable
231
260
public func hmget( _ fields: [ String ] , from key: String ) -> EventLoopFuture < [ String ? ] > {
232
261
guard fields. count > 0 else { return self . eventLoop. makeSucceededFuture ( [ ] ) }
262
+
263
+ var args : [ RESPValue ] = [ . init( bulk: key) ]
264
+ args. append ( convertingContentsOf: fields)
233
265
234
- return send ( command: " HMGET " , with: [ key ] + fields )
266
+ return send ( command: " HMGET " , with: args )
235
267
. convertFromRESPValue ( to: [ RESPValue ] . self)
236
268
. map { return $0. map ( String . init) }
237
269
}
@@ -243,7 +275,8 @@ extension RedisClient {
243
275
/// - Returns: A key-value pair list of fields and their values.
244
276
@inlinable
245
277
public func hgetall( from key: String ) -> EventLoopFuture < [ String : String ] > {
246
- return send ( command: " HGETALL " , with: [ key] )
278
+ let args = [ RESPValue ( bulk: key) ]
279
+ return send ( command: " HGETALL " , with: args)
247
280
. convertFromRESPValue ( to: [ String ] . self)
248
281
. map ( Self . _mapHashResponse)
249
282
}
@@ -262,9 +295,7 @@ extension RedisClient {
262
295
/// - Returns: The new value of the hash field.
263
296
@inlinable
264
297
public func hincrby( _ amount: Int , field: String , in key: String ) -> EventLoopFuture < Int > {
265
- /// connection.hincrby(20, field: "foo", in: "key")
266
- return send ( command: " HINCRBY " , with: [ key, field, amount] )
267
- . convertFromRESPValue ( )
298
+ return _hincr ( command: " HINCRBY " , amount, field, key)
268
299
}
269
300
270
301
/// Increments a hash field's value and returns the new value.
@@ -276,12 +307,27 @@ extension RedisClient {
276
307
/// - key: The key of the hash the field is stored in.
277
308
/// - Returns: The new value of the hash field.
278
309
@inlinable
279
- public func hincrbyfloat< T > ( _ amount: T , field: String , in key: String ) -> EventLoopFuture < T >
310
+ public func hincrbyfloat< Value > ( _ amount: Value , field: String , in key: String ) -> EventLoopFuture < Value >
280
311
where
281
- T : BinaryFloatingPoint ,
282
- T : RESPValueConvertible
312
+ Value : BinaryFloatingPoint ,
313
+ Value : RESPValueConvertible
283
314
{
284
- return send ( command: " HINCRBYFLOAT " , with: [ key, field, amount] )
315
+ return _hincr ( command: " HINCRBYFLOAT " , amount, field, key)
316
+ }
317
+
318
+ @usableFromInline
319
+ internal func _hincr< Value: RESPValueConvertible > (
320
+ command: String ,
321
+ _ amount: Value ,
322
+ _ field: String ,
323
+ _ key: String
324
+ ) -> EventLoopFuture < Value > {
325
+ let args : [ RESPValue ] = [
326
+ . init( bulk: key) ,
327
+ . init( bulk: field) ,
328
+ amount. convertedToRESPValue ( )
329
+ ]
330
+ return send ( command: command, with: args)
285
331
. convertFromRESPValue ( )
286
332
}
287
333
}
0 commit comments