@@ -26,7 +26,7 @@ extension RedisClient {
26
26
let args = [ RESPValue ( from: key) ]
27
27
return self . send ( command: " GET " , with: args)
28
28
}
29
-
29
+
30
30
/// Get the value of a key, converting it to the desired type.
31
31
///
32
32
/// [https://redis.io/commands/get](https://redis.io/commands/get)
@@ -55,7 +55,7 @@ extension RedisClient {
55
55
return send ( command: " MGET " , with: args)
56
56
. map ( )
57
57
}
58
-
58
+
59
59
/// Gets the values of all specified keys, using `.null` to represent non-existant values.
60
60
///
61
61
/// See [https://redis.io/commands/mget](https://redis.io/commands/mget)
@@ -68,7 +68,7 @@ extension RedisClient {
68
68
return self . mget ( keys)
69
69
. map { return $0. map ( Value . init ( fromRESP: ) ) }
70
70
}
71
-
71
+
72
72
/// Gets the values of all specified keys, using `.null` to represent non-existant values.
73
73
///
74
74
/// See [https://redis.io/commands/mget](https://redis.io/commands/mget)
@@ -77,7 +77,7 @@ extension RedisClient {
77
77
public func mget( _ keys: RedisKey ... ) -> EventLoopFuture < [ RESPValue ] > {
78
78
return self . mget ( keys)
79
79
}
80
-
80
+
81
81
/// Gets the values of all specified keys, using `.null` to represent non-existant values.
82
82
///
83
83
/// See [https://redis.io/commands/mget](https://redis.io/commands/mget)
@@ -111,7 +111,7 @@ extension RedisClient {
111
111
return send ( command: " APPEND " , with: args)
112
112
. map ( )
113
113
}
114
-
114
+
115
115
/// Sets the value stored in the key provided, overwriting the previous value.
116
116
///
117
117
/// Any previous expiration set on the key is discarded if the SET operation was successful.
@@ -133,6 +133,27 @@ extension RedisClient {
133
133
. map { _ in ( ) }
134
134
}
135
135
136
+ /// Sets the key to the provided value if the key does not exist.
137
+ ///
138
+ /// [https://redis.io/commands/setnx](https://redis.io/commands/setnx)
139
+ /// - Important: Regardless of the type of data stored at the key, it will be overwritten to a "string" data type.
140
+ ///
141
+ /// ie. If the key is a reference to a Sorted Set, its value will be overwritten to be a "string" data type.
142
+ /// - Parameters:
143
+ /// - key: The key to use to uniquely identify this value.
144
+ /// - value: The value to set the key to.
145
+ /// - Returns: `true` if the operation successfully completed.
146
+ @inlinable
147
+ public func setnx< Value: RESPValueConvertible > ( _ key: RedisKey , to value: Value ) -> EventLoopFuture < Bool > {
148
+ let args : [ RESPValue ] = [
149
+ . init( from: key) ,
150
+ value. convertedToRESPValue ( )
151
+ ]
152
+ return self . send ( command: " SETNX " , with: args)
153
+ . map ( to: Int . self)
154
+ . map { $0 == 1 }
155
+ }
156
+
136
157
/// Sets each key to their respective new value, overwriting existing values.
137
158
/// - Note: Use `msetnx(_:)` if you don't want to overwrite values.
138
159
///
@@ -157,7 +178,7 @@ extension RedisClient {
157
178
. map ( to: Int . self)
158
179
. map { return $0 == 1 }
159
180
}
160
-
181
+
161
182
@usableFromInline
162
183
func _mset< Value: RESPValueConvertible > (
163
184
command: String ,
0 commit comments