Skip to content

Commit 9da5773

Browse files
committed
92 -- Accept event loop and logger in RedisClient commands
There are many times that developers want exact control over which EventLoop will be executing their chained EventLoopFuture callbacks and which Logger will do the logging in calls deep within RediStack. All commands will now accept an optional EventLoop and Logger to hop to, and using the logger for desired logs.
1 parent a4aec72 commit 9da5773

20 files changed

+666
-394
lines changed

Sources/RediStack/Commands/ConnectionCommands.swift

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import struct Logging.Logger
1516
import NIO
1617

1718
// MARK: Connection
@@ -61,28 +62,49 @@ extension RedisClient {
6162
/// Pings the server, which will respond with a message.
6263
///
6364
/// See ``RedisCommand/ping(with:)``
64-
/// - Parameter message: The optional message that the server should respond with instead of the default.
65+
/// - Parameters:
66+
/// - message: The optional message that the server should respond with instead of the default.
67+
/// - eventLoop: An optional event loop to hop to for any further chaining on the returned event loop future.
68+
/// - logger: An optional logger instance to use for logs generated from this command.
6569
/// - Returns: A `NIO.EventLoopFuture` that resolves the given `message` or Redis' default response of `PONG`.
66-
public func ping(with message: String? = nil) -> EventLoopFuture<String> {
67-
return self.send(.ping(with: message))
70+
public func ping(
71+
with message: String? = nil,
72+
eventLoop: EventLoop? = nil,
73+
logger: Logger? = nil
74+
) -> EventLoopFuture<String> {
75+
return self.send(.ping(with: message), eventLoop: eventLoop, logger: logger)
6876
}
6977

7078
/// Requests the client to authenticate with Redis to allow other commands to be executed.
7179
///
7280
/// See ``RedisCommand/auth(with:)``
73-
/// - Parameter password: The password to authenticate with.
81+
/// - Parameters:
82+
/// - password: The password to authenticate with.
83+
/// - eventLoop: An optional event loop to hop to for any further chaining on the returned event loop future.
84+
/// - logger: An optional logger instance to use for logs generated from this command.
7485
/// - Returns: A `NIO.EventLoopFuture` that resolves if the password as accepted, otherwise it fails.
75-
public func authorize(with password: String) -> EventLoopFuture<Void> {
76-
return self.send(.auth(with: password))
86+
public func authorize(
87+
with password: String,
88+
eventLoop: EventLoop? = nil,
89+
logger: Logger? = nil
90+
) -> EventLoopFuture<Void> {
91+
return self.send(.auth(with: password), eventLoop: eventLoop, logger: logger)
7792
}
7893

7994
/// Selects the Redis logical database having the given zero-based numeric index.
8095
///
8196
/// See ``RedisCommand/select(database:)``
8297
/// - Note: New connections always use the database `0`.
83-
/// - Parameter index: The 0-based index of the database that the connection sending this command will execute later commands against.
98+
/// - Parameters:
99+
/// - index: The 0-based index of the database that the connection sending this command will execute later commands against.
100+
/// - eventLoop: An optional event loop to hop to for any further chaining on the returned event loop future.
101+
/// - logger: An optional logger instance to use for logs generated from this command.
84102
/// - Returns: A `NIO.EventLoopFuture` resolving once the operation has succeeded.
85-
public func select(database index: Int) -> EventLoopFuture<Void> {
86-
return self.send(.select(database: index))
103+
public func select(
104+
database index: Int,
105+
eventLoop: EventLoop? = nil,
106+
logger: Logger? = nil
107+
) -> EventLoopFuture<Void> {
108+
return self.send(.select(database: index), eventLoop: eventLoop, logger: logger)
87109
}
88110
}

Sources/RediStack/Commands/HashCommands.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import struct Logging.Logger
1516
import NIO
1617

1718
// MARK: Hashes
@@ -237,16 +238,19 @@ extension RedisClient {
237238
/// - position: The position to start the scan from.
238239
/// - match: A glob-style pattern to filter values to be selected from the result set.
239240
/// - count: The number of elements to advance by. Redis default is 10.
240-
/// - valueType: The type to cast all values to.
241+
/// - eventLoop: An optional event loop to hop to for any further chaining on the returned event loop future.
242+
/// - logger: An optional logger instance to use for logs generated from this command.
241243
/// - Returns: A `NIO.EventLoopFuture` that resolves a cursor position for additional scans,
242244
/// with a limited collection of fields and their associated values that were iterated over.
243245
public func scanHashFields(
244246
in key: RedisKey,
245247
startingFrom position: Int = 0,
246248
matching match: String? = nil,
247-
count: Int? = nil
249+
count: Int? = nil,
250+
eventLoop: EventLoop? = nil,
251+
logger: Logger? = nil
248252
) -> EventLoopFuture<(Int, [RedisHashFieldKey: RESPValue])> {
249-
return self.send(.hscan(key, startingFrom: position, matching: match, count: count))
253+
return self.send(.hscan(key, startingFrom: position, matching: match, count: count), eventLoop: eventLoop, logger: logger)
250254
}
251255
}
252256

Sources/RediStack/Commands/KeyCommands.swift

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import struct Logging.Logger
1516
import NIO
1617

1718
// MARK: Key
@@ -94,20 +95,34 @@ extension RedisClient {
9495
/// Deletes the given keys. Any key that does not exist is ignored.
9596
///
9697
/// See ``RedisCommand/.del(keys:)``
97-
/// - Parameter keys: The list of keys to delete from the database.
98+
/// - Parameters:
99+
/// - keys: The list of keys to delete from the database.
100+
/// - eventLoop: An optional event loop to hop to for any further chaining on the returned event loop future.
101+
/// - logger: An optional logger instance to use for logs generated from this command.
98102
/// - Returns: A `NIO.EventLoopFuture` that resolves the number of keys that were deleted from the database.
99-
public func delete(_ keys: RedisKey...) -> EventLoopFuture<Int> {
100-
return self.delete(keys)
103+
public func delete(
104+
_ keys: RedisKey...,
105+
eventLoop: EventLoop? = nil,
106+
logger: Logger? = nil
107+
) -> EventLoopFuture<Int> {
108+
return self.delete(keys, eventLoop: eventLoop, logger: logger)
101109
}
102110

103111
/// Deletes the given keys. Any key that does not exist is ignored.
104112
///
105113
/// See ``RedisCommand/del(keys:)``
106-
/// - Parameter keys: The list of keys to delete from the database.
114+
/// - Parameters:
115+
/// - keys: The list of keys to delete from the database.
116+
/// - eventLoop: An optional event loop to hop to for any further chaining on the returned event loop future.
117+
/// - logger: An optional logger instance to use for logs generated from this command.
107118
/// - Returns: A `NIO.EventLoopFuture` that resolves the number of keys that were deleted from the database.
108-
public func delete(_ keys: [RedisKey]) -> EventLoopFuture<Int> {
119+
public func delete(
120+
_ keys: [RedisKey],
121+
eventLoop: EventLoop? = nil,
122+
logger: Logger? = nil
123+
) -> EventLoopFuture<Int> {
109124
guard keys.count > 0 else { return self.eventLoop.makeSucceededFuture(0) }
110-
return self.send(.del(keys))
125+
return self.send(.del(keys), eventLoop: eventLoop, logger: logger)
111126
}
112127

113128
/// Sets a timeout on key. After the timeout has expired, the key will automatically be deleted.
@@ -116,18 +131,32 @@ extension RedisClient {
116131
/// - Parameters:
117132
/// - key: The key to set the expiration on.
118133
/// - timeout: The time from now the key will expire at.
134+
/// - eventLoop: An optional event loop to hop to for any further chaining on the returned event loop future.
135+
/// - logger: An optional logger instance to use for logs generated from this command.
119136
/// - Returns: A `NIO.EventLoopFuture` that resolves `true` if the expiration was set and `false` if it wasn't.
120-
public func expire(_ key: RedisKey, after timeout: TimeAmount) -> EventLoopFuture<Bool> {
121-
return self.send(.expire(key, after: timeout))
137+
public func expire(
138+
_ key: RedisKey,
139+
after timeout: TimeAmount,
140+
eventLoop: EventLoop? = nil,
141+
logger: Logger? = nil
142+
) -> EventLoopFuture<Bool> {
143+
return self.send(.expire(key, after: timeout), eventLoop: eventLoop, logger: logger)
122144
}
123145

124146
/// Searches the keys in the database that match the given pattern.
125147
///
126148
/// See ``RedisCommand/keys(matching:)``
127-
/// - Parameter pattern: The key pattern to search for matching keys that exist in Redis.
149+
/// - Parameters:
150+
/// - pattern: The key pattern to search for matching keys that exist in Redis.
151+
/// - eventLoop: An optional event loop to hop to for any further chaining on the returned event loop future.
152+
/// - logger: An optional logger instance to use for logs generated from this command.
128153
/// - Returns: A result set of ``RedisKey`` values that exist and match the provided pattern.
129-
public func listKeys(matching pattern: String) -> EventLoopFuture<[RedisKey]> {
130-
return self.send(.keys(matching: pattern))
154+
public func listKeys(
155+
matching pattern: String,
156+
eventLoop: EventLoop? = nil,
157+
logger: Logger? = nil
158+
) -> EventLoopFuture<[RedisKey]> {
159+
return self.send(.keys(matching: pattern), eventLoop: eventLoop, logger: logger)
131160
}
132161

133162
/// Incrementally iterates over all keys in the currently selected database.
@@ -137,12 +166,16 @@ extension RedisClient {
137166
/// - position: The cursor position to start from.
138167
/// - match: A glob-style pattern to filter values to be selected from the result set.
139168
/// - count: The number of elements to advance by. Redis default is 10.
169+
/// - eventLoop: An optional event loop to hop to for any further chaining on the returned event loop future.
170+
/// - logger: An optional logger instance to use for logs generated from this command.
140171
/// - Returns: A cursor position for additional invocations with a limited collection of keys found in the database.
141172
public func scanKeys(
142173
startingFrom position: Int = 0,
143174
matching match: String? = nil,
144-
count: Int? = nil
175+
count: Int? = nil,
176+
eventLoop: EventLoop? = nil,
177+
logger: Logger? = nil
145178
) -> EventLoopFuture<(Int, [RedisKey])> {
146-
return self.send(.scan(startingFrom: position, matching: match, count: count))
179+
return self.send(.scan(startingFrom: position, matching: match, count: count), eventLoop: eventLoop, logger: logger)
147180
}
148181
}

Sources/RediStack/Commands/PubSubCommands.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import struct Logging.Logger
1516
import NIO
1617

1718
// MARK: PubSub
@@ -86,13 +87,17 @@ extension RedisClient {
8687
/// - Parameters:
8788
/// - message: The "message" value to publish on the channel.
8889
/// - channel: The name of the channel to publish the message to.
90+
/// - eventLoop: An optional event loop to hop to for any further chaining on the returned event loop future.
91+
/// - logger: An optional logger instance to use for logs generated from this command.
8992
/// - Returns: The number of subscribed clients that received the message.
9093
@inlinable
9194
@discardableResult
9295
public func publish<Message: RESPValueConvertible>(
9396
_ message: Message,
94-
to channel: RedisChannelName
97+
to channel: RedisChannelName,
98+
eventLoop: EventLoop? = nil,
99+
logger: Logger? = nil
95100
) -> EventLoopFuture<Int> {
96-
return self.send(.publish(message, to: channel))
101+
return self.send(.publish(message, to: channel), eventLoop: eventLoop, logger: logger)
97102
}
98103
}

Sources/RediStack/Commands/ServerCommands.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import struct Logging.Logger
1516
import NIO
1617

1718
// MARK: Server
@@ -41,8 +42,15 @@ extension RedisClient {
4142
/// - Parameters:
4243
/// - first: The index of the first database.
4344
/// - second: The index of the second database.
45+
/// - eventLoop: An optional event loop to hop to for any further chaining on the returned event loop future.
46+
/// - logger: An optional logger instance to use for logs generated from this command.
4447
/// - Returns: A `NIO.EventLoopFuture` that resolves `true` if the command succeed or `false` if it didn't.
45-
public func swapDatabase(_ first: Int, with second: Int) -> EventLoopFuture<Bool> {
46-
return self.send(.swapdb(first, with: second))
48+
public func swapDatabase(
49+
_ first: Int,
50+
with second: Int,
51+
eventLoop: EventLoop? = nil,
52+
logger: Logger? = nil
53+
) -> EventLoopFuture<Bool> {
54+
return self.send(.swapdb(first, with: second), eventLoop: eventLoop, logger: logger)
4755
}
4856
}

Sources/RediStack/Commands/SetCommands.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import struct Logging.Logger
1516
import NIO
1617

1718
// MARK: Sets
@@ -254,13 +255,17 @@ extension RedisClient {
254255
/// - position: The position to start the scan from.
255256
/// - count: The number of elements to advance by. Redis default is 10.
256257
/// - match: A glob-style pattern to filter values to be selected from the result set.
258+
/// - eventLoop: An optional event loop to hop to for any further chaining on the returned event loop future.
259+
/// - logger: An optional logger instance to use for logs generated from this command.
257260
/// - Returns: A `NIO.EventLoopFuture` that resolves a cursor position for additional scans, with a limited collection of values that were iterated over.
258261
public func scanSetValues(
259262
in key: RedisKey,
260263
startingFrom position: Int = 0,
261264
matching match: String? = nil,
262-
count: Int? = nil
265+
count: Int? = nil,
266+
eventLoop: EventLoop? = nil,
267+
logger: Logger? = nil
263268
) -> EventLoopFuture<(Int, [RESPValue])> {
264-
return self.send(.sscan(key, startingFrom: position, matching: match, count: count))
269+
return self.send(.sscan(key, startingFrom: position, matching: match, count: count), eventLoop: eventLoop, logger: logger)
265270
}
266271
}

Sources/RediStack/Commands/SortedSetCommands.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import struct Logging.Logger
1516
import NIO
1617

1718
// MARK: Sorted Sets
@@ -1154,15 +1155,19 @@ extension RedisClient {
11541155
/// - position: The position to start the scan from.
11551156
/// - match: A glob-style pattern to filter values to be selected from the result set.
11561157
/// - count: The number of elements to advance by. Redis default is 10.
1158+
/// - eventLoop: An optional event loop to hop to for any further chaining on the returned event loop future.
1159+
/// - logger: An optional logger instance to use for logs generated from this command.
11571160
/// - Returns: A `NIO.EventLoopFuture` that resolves a cursor position for additional scans,
11581161
/// with a limited collection of elements with their scores found in the Sorted Set.
11591162
public func scanSortedSetValues(
11601163
in key: RedisKey,
11611164
startingFrom position: Int = 0,
11621165
matching match: String? = nil,
1163-
count: Int? = nil
1166+
count: Int? = nil,
1167+
eventLoop: EventLoop? = nil,
1168+
logger: Logger? = nil
11641169
) -> EventLoopFuture<(Int, [(RESPValue, Double)])> {
1165-
return self.send(.zscan(key, startingFrom: position, matching: match, count: count))
1170+
return self.send(.zscan(key, startingFrom: position, matching: match, count: count), eventLoop: eventLoop, logger: logger)
11661171
}
11671172
}
11681173

0 commit comments

Comments
 (0)