Skip to content

Commit 5f86d67

Browse files
committed
Add Redis.increment
1 parent 757e43a commit 5f86d67

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

Sources/App/Core/Dependencies/RedisClient.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@ import DependenciesMacros
2222
struct RedisClient {
2323
var set: @Sendable (_ key: String, _ value: String?, Duration?) async -> Void
2424
var get: @Sendable (_ key: String) async -> String?
25+
var increment: @Sendable (_ key: String, _ by: Int) async -> Int?
2526
}
2627

2728

2829
extension RedisClient {
2930
func set(key: String, value: String?, expiresIn: Duration? = nil) async {
3031
await set(key: key, value: value, expiresIn)
3132
}
33+
34+
func increment(key: String) async -> Int? {
35+
await increment(key: key, by: 1)
36+
}
3237
}
3338

3439

@@ -38,7 +43,8 @@ extension RedisClient: DependencyKey {
3843
set: { key, value, expiresIn in
3944
await Redis.shared?.set(key: key, value: value, expiresIn: expiresIn)
4045
},
41-
get: { key in await Redis.shared?.get(key: key) }
46+
get: { key in await Redis.shared?.get(key: key) },
47+
increment: { key, value in try? await Redis.shared?.increment(key: key, by: value) }
4248
)
4349
}
4450
}
@@ -60,7 +66,7 @@ extension DependencyValues {
6066
#if DEBUG
6167
extension RedisClient {
6268
static var disabled: Self {
63-
.init(set: { _, _, _ in }, get: { _ in nil })
69+
.init(set: { _, _, _ in }, get: { _ in nil }, increment: { _, _ in nil})
6470
}
6571
}
6672
#endif
@@ -105,7 +111,7 @@ private actor Redis {
105111
static let hostname = "redis"
106112
static let maxConnectionAttempts = 3
107113

108-
func set(key: String, value: String?, expiresIn: Duration?) async -> Void {
114+
func set(key: String, value: String?, expiresIn: Duration?) async {
109115
if let value {
110116
let buffer = ByteBuffer(string: value)
111117
let value = RESPValue.bulkString(buffer)
@@ -123,5 +129,13 @@ private actor Redis {
123129
func get(key: String) async -> String? {
124130
return try? await client.get(.init(key)).map(\.string).get()
125131
}
132+
133+
func increment(key: String) async throws -> Int {
134+
try await client.increment(.init(key)).get()
135+
}
136+
137+
func increment(key: String, by value: Int) async throws -> Int {
138+
try await client.increment(.init(key), by: value).get()
139+
}
126140
}
127141

0 commit comments

Comments
 (0)