@@ -7,6 +7,7 @@ extension RedisCommandExecutor {
7
7
/// See [https://redis.io/commands/echo](https://redis.io/commands/echo)
8
8
/// - Parameter message: The message to echo.
9
9
/// - Returns: The message sent with the command.
10
+ @inlinable
10
11
public func echo( _ message: String ) -> EventLoopFuture < String > {
11
12
return send ( command: " ECHO " , with: [ message] )
12
13
. mapFromRESP ( )
@@ -15,8 +16,9 @@ extension RedisCommandExecutor {
15
16
/// Pings the server, which will respond with a message.
16
17
///
17
18
/// See [https://redis.io/commands/ping](https://redis.io/commands/ping)
18
- /// - Parameter with : The optional message that the server should respond with.
19
+ /// - Parameter message : The optional message that the server should respond with.
19
20
/// - Returns: The provided message or Redis' default response of `"PONG"`.
21
+ @inlinable
20
22
public func ping( with message: String ? = nil ) -> EventLoopFuture < String > {
21
23
let arg = message != nil ? [ message] : [ ]
22
24
return send ( command: " PING " , with: arg)
@@ -26,196 +28,102 @@ extension RedisCommandExecutor {
26
28
/// Request for authentication in a password-protected Redis server.
27
29
///
28
30
/// [https://redis.io/commands/auth](https://redis.io/commands/auth)
31
+ /// - Parameter password: The password being used to access the Redis server.
32
+ /// - Returns: An `EventLoopFuture` that resolves when the connection has been authorized, or fails with a `RedisError`.
33
+ @inlinable
29
34
public func authorize( with password: String ) -> EventLoopFuture < Void > {
30
35
return send ( command: " AUTH " , with: [ password] )
31
36
. map { _ in return ( ) }
32
37
}
33
38
34
39
/// Select the Redis logical database having the specified zero-based numeric index.
35
- /// New connections always use the database `0`.
40
+ /// - Note: New connections always use the database `0`.
36
41
///
37
42
/// [https://redis.io/commands/select](https://redis.io/commands/select)
38
- public func select( database id: Int ) -> EventLoopFuture < Void > {
39
- return send ( command: " SELECT " , with: [ id. description] )
43
+ /// - Parameter index: The 0-based index of the database that will receive later commands.
44
+ /// - Returns: An `EventLoopFuture` that resolves when the operation has succeeded, or fails with a `RedisError`.
45
+ @inlinable
46
+ public func select( database index: Int ) -> EventLoopFuture < Void > {
47
+ return send ( command: " SELECT " , with: [ index] )
40
48
. map { _ in return ( ) }
41
49
}
42
50
43
- /// Swaps the data of two Redis database by their index ID .
51
+ /// Swaps the data of two Redis databases by their index IDs .
44
52
///
45
53
/// See [https://redis.io/commands/swapdb](https://redis.io/commands/swapdb)
46
54
/// - Parameters:
47
- /// - firstIndex : The index of the first database.
48
- /// - secondIndex : The index of the second database.
55
+ /// - first : The index of the first database.
56
+ /// - second : The index of the second database.
49
57
/// - Returns: `true` if the swap was successful.
50
- public func swapdb( firstIndex: Int , secondIndex: Int ) -> EventLoopFuture < Bool > {
51
- return send ( command: " SWAPDB " , with: [ firstIndex, secondIndex] )
58
+ @inlinable
59
+ public func swapDatabase( _ first: Int , with second: Int ) -> EventLoopFuture < Bool > {
60
+ /// connection.swapDatabase(index: 0, withIndex: 10)
61
+ return send ( command: " SWAPDB " , with: [ first, second] )
52
62
. mapFromRESP ( to: String . self)
53
63
. map { return $0 == " OK " }
54
64
}
55
- }
56
65
57
- extension RedisCommandExecutor {
58
66
/// Removes the specified keys. A key is ignored if it does not exist.
59
67
///
60
68
/// [https://redis.io/commands/del](https://redis.io/commands/del)
61
- /// - Returns: A future number of keys that were removed.
62
- public func delete( _ keys: String ... ) -> EventLoopFuture < Int > {
69
+ /// - Parameter keys: A list of keys to delete from the database.
70
+ /// - Returns: The number of keys deleted from the database.
71
+ @inlinable
72
+ public func delete( _ keys: [ String ] ) -> EventLoopFuture < Int > {
73
+ guard keys. count > 0 else { return self . eventLoop. makeSucceededFuture ( 0 ) }
74
+
63
75
return send ( command: " DEL " , with: keys)
64
76
. mapFromRESP ( )
65
77
}
66
78
67
- /// Set a timeout on key. After the timeout has expired, the key will automatically be deleted.
68
- /// A key with an associated timeout is often said to be volatile in Redis terminology.
79
+ /// Sets a timeout on key. After the timeout has expired, the key will automatically be deleted.
80
+ /// - Note: A key with an associated timeout is often said to be " volatile" in Redis terminology.
69
81
///
70
82
/// [https://redis.io/commands/expire](https://redis.io/commands/expire)
71
83
/// - Parameters:
72
- /// - after: The lifetime (in seconds) the key will expirate at.
73
- /// - Returns: A future bool indicating if the expiration was set or not.
74
- public func expire( _ key: String , after deadline: Int ) -> EventLoopFuture < Bool > {
75
- return send ( command: " EXPIRE " , with: [ key, deadline. description] )
76
- . mapFromRESP ( to: Int . self)
77
- . map { return $0 == 1 }
78
- }
79
-
80
- /// Get the value of a key.
81
- /// If the key does not exist the value will be `nil`.
82
- /// An error is resolved if the value stored at key is not a string, because GET only handles string values.
83
- ///
84
- /// [https://redis.io/commands/get](https://redis.io/commands/get)
85
- public func get( _ key: String ) -> EventLoopFuture < String ? > {
86
- return send ( command: " GET " , with: [ key] )
87
- . map { return $0. string }
88
- }
89
-
90
- /// Returns the values of all specified keys, using `.null` to represent non-existant values.
91
- ///
92
- /// See [https://redis.io/commands/mget](https://redis.io/commands/mget)
93
- public func mget( _ keys: [ String ] ) -> EventLoopFuture < [ RESPValue ] > {
94
- assert ( keys. count > 0 , " At least 1 key should be provided. " )
95
-
96
- return send ( command: " MGET " , with: keys)
97
- . mapFromRESP ( )
98
- }
99
-
100
- /// Set key to hold the string value.
101
- /// If key already holds a value, it is overwritten, regardless of its type.
102
- /// Any previous time to live associated with the key is discarded on successful SET operation.
103
- ///
104
- /// [https://redis.io/commands/set](https://redis.io/commands/set)
105
- public func set( _ key: String , to value: String ) -> EventLoopFuture < Void > {
106
- return send ( command: " SET " , with: [ key, value] )
107
- . map { _ in return ( ) }
108
- }
109
-
110
- /// Sets each key to the respective new value, overwriting existing values.
111
- ///
112
- /// - Note: Use `msetnx` if you don't want to overwrite values.
113
- ///
114
- /// See [https://redis.io/commands/mset](https://redis.io/commands/mset)
115
- public func mset( _ operations: [ String : RESPValueConvertible ] ) -> EventLoopFuture < Void > {
116
- assert ( operations. count > 0 , " At least 1 key-value pair should be provided. " )
117
-
118
- let args = _convertMSET ( operations)
119
- return send ( command: " MSET " , with: args)
120
- . map { _ in return ( ) }
121
- }
122
-
123
- /// If every key does not exist, sets each key to the respective new value.
124
- ///
125
- /// See [https://redis.io/commands/msetnx](https://redis.io/commands/msetnx)
126
- public func msetnx( _ operations: [ String : RESPValueConvertible ] ) -> EventLoopFuture < Bool > {
127
- assert ( operations. count > 0 , " At least 1 key-value pair should be provided. " )
128
-
129
- let args = _convertMSET ( operations)
130
- return send ( command: " MSETNX " , with: args)
84
+ /// - key: The key to set the expiration on.
85
+ /// - deadline: The time from now the key will expire at.
86
+ /// - Returns: `true` if the expiration was set.
87
+ @inlinable
88
+ public func expire( _ key: String , after deadline: TimeAmount ) -> EventLoopFuture < Bool > {
89
+ let amount = deadline. nanoseconds / 1_000_000_000
90
+ return send ( command: " EXPIRE " , with: [ key, amount] )
131
91
. mapFromRESP ( to: Int . self)
132
92
. map { return $0 == 1 }
133
93
}
134
-
135
- @inline ( __always)
136
- private func _convertMSET( _ source: [ String : RESPValueConvertible ] ) -> [ RESPValueConvertible ] {
137
- return source. reduce ( into: [ RESPValueConvertible] ( ) , { ( result, element) in
138
- result. append ( element. key)
139
- result. append ( element. value)
140
- } )
141
- }
142
94
}
143
95
144
- extension RedisCommandExecutor {
145
- /// Increments the stored value by 1 and returns the new value.
146
- ///
147
- /// See [https://redis.io/commands/incr](https://redis.io/commands/incr)
148
- /// - Returns: The new value after the operation.
149
- public func increment( _ key: String ) -> EventLoopFuture < Int > {
150
- return send ( command: " INCR " , with: [ key] )
151
- . mapFromRESP ( )
152
- }
153
-
154
- /// Increments the stored value by the amount desired and returns the new value.
155
- ///
156
- /// See [https://redis.io/commands/incrby](https://redis.io/commands/incrby)
157
- /// - Returns: The new value after the operation.
158
- public func increment( _ key: String , by count: Int ) -> EventLoopFuture < Int > {
159
- return send ( command: " INCRBY " , with: [ key, count] )
160
- . mapFromRESP ( )
161
- }
162
-
163
- /// Increments the stored value by the amount desired and returns the new value.
164
- ///
165
- /// See [https://redis.io/commands/incrbyfloat](https://redis.io/commands/incrbyfloat)
166
- /// - Returns: The new value after the operation.
167
- public func increment< T: BinaryFloatingPoint > ( _ key: String , by count: T ) -> EventLoopFuture < T >
168
- where T: RESPValueConvertible
169
- {
170
- return send ( command: " INCRBYFLOAT " , with: [ key, count] )
171
- . mapFromRESP ( )
172
- }
173
-
174
- /// Decrements the stored value by 1 and returns the new value.
175
- ///
176
- /// See [https://redis.io/commands/decr](https://redis.io/commands/decr)
177
- /// - Returns: The new value after the operation.
178
- public func decrement( _ key: String ) -> EventLoopFuture < Int > {
179
- return send ( command: " DECR " , with: [ key] )
180
- . mapFromRESP ( )
181
- }
182
-
183
- /// Decrements the stored valye by the amount desired and returns the new value.
184
- ///
185
- /// See [https://redis.io/commands/decrby](https://redis.io/commands/decrby)
186
- /// - Returns: The new value after the operation.
187
- public func decrement( _ key: String , by count: Int ) -> EventLoopFuture < Int > {
188
- return send ( command: " DECRBY " , with: [ key, count] )
189
- . mapFromRESP ( )
190
- }
191
- }
96
+ // MARK: Scan
192
97
193
98
extension RedisCommandExecutor {
194
99
/// Incrementally iterates over all keys in the currently selected database.
195
100
///
196
101
/// [https://redis.io/commands/scan](https://redis.io/commands/scan)
197
102
/// - Parameters:
198
- /// - startingFrom : The cursor position to start from.
103
+ /// - position : The cursor position to start from.
199
104
/// - count: The number of elements to advance by. Redis default is 10.
200
- /// - matching : A glob-style pattern to filter values to be selected from the result set.
201
- /// - Returns: A cursor position for additional invocations with a limited collection of keys stored in the database.
105
+ /// - match : A glob-style pattern to filter values to be selected from the result set.
106
+ /// - Returns: A cursor position for additional invocations with a limited collection of keys found in the database.
202
107
@inlinable
203
108
public func scan(
204
- startingFrom pos : Int = 0 ,
109
+ startingFrom position : Int = 0 ,
205
110
count: Int ? = nil ,
206
- matching match: String ? = nil ) -> EventLoopFuture < ( Int , [ String ] ) >
207
- {
208
- return _scan ( command: " SCAN " , resultType : [ String ] . self , nil , pos , count, match)
111
+ matching match: String ? = nil
112
+ ) -> EventLoopFuture < ( Int , [ String ] ) > {
113
+ return _scan ( command: " SCAN " , nil , position , count, match)
209
114
}
210
115
211
- @inline ( __always )
212
- @ usableFromInline func _scan< T: RESPValueConvertible > (
116
+ @usableFromInline
117
+ func _scan< T> (
213
118
command: String ,
214
- resultType: T . Type ,
119
+ resultType: T . Type = T . self ,
215
120
_ key: String ? ,
216
121
_ pos: Int ,
217
122
_ count: Int ? ,
218
- _ match: String ? ) -> EventLoopFuture < ( Int , T ) >
123
+ _ match: String ?
124
+ ) -> EventLoopFuture < ( Int , T ) >
125
+ where
126
+ T: RESPValueConvertible
219
127
{
220
128
var args : [ RESPValueConvertible ] = [ pos]
221
129
@@ -237,7 +145,12 @@ extension RedisCommandExecutor {
237
145
guard
238
146
let value = result [ 0 ] . string,
239
147
let position = Int ( value)
240
- else { throw RedisError ( identifier: #function, reason: " Unexpected value in response: \( result [ 0 ] ) " ) }
148
+ else {
149
+ throw RedisError (
150
+ identifier: #function,
151
+ reason: " Unexpected value in response: \( result [ 0 ] ) "
152
+ )
153
+ }
241
154
return position
242
155
}
243
156
let elements = response
0 commit comments