Skip to content

Commit 123d9c9

Browse files
glbrnttMordil
authored andcommitted
Add EXISTS command
Motivation: The EXISTS command was missing. Modifications: - Add 'EXISTS' to basic commands - Add integration tests Result: The existence of a key can be checked.
1 parent 2211dbf commit 123d9c9

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

Sources/RediStack/Commands/BasicCommands.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,28 @@ extension RedisClient {
101101
return self.delete(keys)
102102
}
103103

104+
/// Checks the existence of the provided keys in the database.
105+
///
106+
/// [https://redis.io/commands/exists](https://redis.io/commands/exists)
107+
/// - Parameter keys: A list of keys whose existence will be checked for in the database.
108+
/// - Returns: The number of provided keys which exist in the database.
109+
public func exists(_ keys: [RedisKey]) -> EventLoopFuture<Int> {
110+
let args: [RESPValue] = keys.map {
111+
RESPValue(from: $0)
112+
}
113+
return self.send(command: "EXISTS", with: args)
114+
.map(to: Int.self)
115+
}
116+
117+
/// Checks the existence of the provided keys in the database.
118+
///
119+
/// [https://redis.io/commands/exists](https://redis.io/commands/exists)
120+
/// - Parameter keys: A list of keys whose existence will be checked for in the database.
121+
/// - Returns: The number of provided keys which exist in the database.
122+
public func exists(_ keys: RedisKey...) -> EventLoopFuture<Int> {
123+
return self.exists(keys)
124+
}
125+
104126
/// Sets a timeout on key. After the timeout has expired, the key will automatically be deleted.
105127
/// - Note: A key with an associated timeout is often said to be "volatile" in Redis terminology.
106128
///

Tests/RediStackIntegrationTests/Commands/BasicCommandsTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ final class BasicCommandsTests: RediStackIntegrationTestCase {
3737
XCTAssertEqual(third, 2)
3838
}
3939

40+
func test_exists() throws {
41+
try self.connection.set("first", to: "1").wait()
42+
let first = try connection.exists("first").wait()
43+
XCTAssertEqual(first, 1)
44+
45+
try self.connection.set("second", to: "2").wait()
46+
let firstAndSecond = try connection.exists("first", "second").wait()
47+
XCTAssertEqual(firstAndSecond, 2)
48+
49+
let secondAndThird = try connection.exists("second", "third").wait()
50+
XCTAssertEqual(secondAndThird, 1)
51+
52+
let third = try connection.exists("third").wait()
53+
XCTAssertEqual(third, 0)
54+
}
55+
4056
func test_expire() throws {
4157
try connection.set(#function, to: "value").wait()
4258
XCTAssertNotNil(try connection.get(#function).wait())

Tests/RediStackIntegrationTests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extension BasicCommandsTests {
88
static let __allTests__BasicCommandsTests = [
99
("test_delete", test_delete),
1010
("test_echo", test_echo),
11+
("test_exists", test_exists),
1112
("test_expire", test_expire),
1213
("test_ping", test_ping),
1314
("test_select", test_select),

0 commit comments

Comments
 (0)