Skip to content

Commit 370ef8c

Browse files
committed
101 -- Add KEYS command
1 parent 6c4ca52 commit 370ef8c

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

Sources/RediStack/Commands/KeyCommands.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the RediStack open source project
44
//
5-
// Copyright (c) 2020 RediStack project authors
5+
// Copyright (c) 2020-2022 RediStack project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -67,6 +67,12 @@ extension RedisCommand {
6767
return .init(milliseconds: try $0.map())
6868
}
6969
}
70+
71+
/// [KEYS](https://redis.io/commands/keys)
72+
/// - Parameter pattern: The key pattern to search for matching keys that exist in Redis.
73+
public static func keys(matching pattern: String) -> RedisCommand<[String]> {
74+
return .init(keyword: "KEYS", arguments: [pattern.convertedToRESPValue()])
75+
}
7076

7177
/// [SCAN](https://redis.io/commands/scan)
7278
/// - Parameters:
@@ -115,6 +121,15 @@ extension RedisClient {
115121
return self.send(.expire(key, after: timeout))
116122
}
117123

124+
/// Searches the keys in the database that match the given pattern.
125+
///
126+
/// See ``RedisCommand/keys(matching:)``
127+
/// - Parameter pattern: The key pattern to search for matching keys that exist in Redis.
128+
/// - Returns: A list of keys that matched the provided pattern.
129+
public func listKeys(matching pattern: String) -> EventLoopFuture<[String]> {
130+
return self.send(.keys(matching: pattern))
131+
}
132+
118133
/// Incrementally iterates over all keys in the currently selected database.
119134
///
120135
/// See `RedisCommand.scan(startingFrom:matching:count:)`

Tests/RediStackIntegrationTests/Commands/KeyCommandsTests.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the RediStack open source project
44
//
5-
// Copyright (c) 2020 RediStack project authors
5+
// Copyright (c) 2020-2022 RediStack project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -148,4 +148,14 @@ final class KeyCommandsTests: RediStackIntegrationTestCase {
148148
XCTAssertGreaterThanOrEqual(keys.count, 1)
149149
XCTAssertLessThanOrEqual(keys.count, 7)
150150
}
151+
152+
func test_keys() throws {
153+
let range = Range(0...3)
154+
try range.forEach {
155+
try self.connection.set("\(#function)_\($0)", to: $0).wait()
156+
}
157+
let keys = try self.connection.listKeys(matching: "\(#function)*").wait()
158+
XCTAssertEqual(keys.count, range.count)
159+
XCTAssertTrue(keys.allSatisfy({ $0.contains(#function) }))
160+
}
151161
}

0 commit comments

Comments
 (0)