Skip to content

Commit 7f37aea

Browse files
committed
Update Redis interface
Add expire, increment, change to throwing instead of returning optionals
1 parent 5f86d67 commit 7f37aea

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

Sources/App/Core/Dependencies/CurrentReferenceCacheClient.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ extension CurrentReferenceCacheClient: DependencyKey {
3030
.init(
3131
set: { owner, repository, reference async in
3232
@Dependency(\.redis) var redis
33-
await redis.set(key: getKey(owner: owner, repository: repository),
34-
value: reference,
35-
expiresIn: timeToLive)
33+
try? await redis.set(key: getKey(owner: owner, repository: repository),
34+
value: reference,
35+
expiresIn: timeToLive)
3636
},
3737
get: { owner, repository in
3838
@Dependency(\.redis) var redis
39-
return await redis.get(key: getKey(owner: owner, repository: repository))
39+
return try? await redis.get(key: getKey(owner: owner, repository: repository))
4040
}
4141
)
4242
}

Sources/App/Core/Dependencies/RedisClient.swift

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,20 @@ import DependenciesMacros
2020

2121
@DependencyClient
2222
struct RedisClient {
23-
var set: @Sendable (_ key: String, _ value: String?, Duration?) async -> Void
24-
var get: @Sendable (_ key: String) async -> String?
25-
var increment: @Sendable (_ key: String, _ by: Int) async -> Int?
23+
var set: @Sendable (_ key: String, _ value: String?, Duration?) async throws -> Void
24+
var get: @Sendable (_ key: String) async throws -> String?
25+
var expire: @Sendable (_ key: String, _ after: Duration) async throws -> Bool
26+
var increment: @Sendable (_ key: String, _ by: Int) async throws -> Int
2627
}
2728

2829

2930
extension RedisClient {
30-
func set(key: String, value: String?, expiresIn: Duration? = nil) async {
31-
await set(key: key, value: value, expiresIn)
31+
func set(key: String, value: String?, expiresIn: Duration? = nil) async throws {
32+
try await set(key: key, value: value, expiresIn)
3233
}
3334

34-
func increment(key: String) async -> Int? {
35-
await increment(key: key, by: 1)
35+
func increment(key: String) async throws -> Int {
36+
try await increment(key: key, by: 1)
3637
}
3738
}
3839

@@ -41,10 +42,11 @@ extension RedisClient: DependencyKey {
4142
static var liveValue: RedisClient {
4243
.init(
4344
set: { key, value, expiresIn in
44-
await Redis.shared?.set(key: key, value: value, expiresIn: expiresIn)
45+
try await Redis.shared.set(key: key, value: value, expiresIn: expiresIn)
4546
},
46-
get: { key in await Redis.shared?.get(key: key) },
47-
increment: { key, value in try? await Redis.shared?.increment(key: key, by: value) }
47+
get: { key in try await Redis.shared.get(key: key) },
48+
expire: { key, ttl in try await Redis.shared.expire(key: key, after: ttl) },
49+
increment: { key, value in try await Redis.shared.increment(key: key, by: value) }
4850
)
4951
}
5052
}
@@ -66,22 +68,25 @@ extension DependencyValues {
6668
#if DEBUG
6769
extension RedisClient {
6870
static var disabled: Self {
69-
.init(set: { _, _, _ in }, get: { _ in nil }, increment: { _, _ in nil})
71+
.init(set: { _, _, _ in },
72+
get: { _ in nil },
73+
expire: { _, _ in true },
74+
increment: { _, value in value })
7075
}
7176
}
7277
#endif
7378

7479

7580
private actor Redis {
7681
var client: RediStack.RedisClient
77-
static private var task: Task<Redis?, Never>?
82+
static private var task: Task<Redis, Swift.Error>?
7883

79-
static var shared: Redis? {
80-
get async {
84+
static var shared: Redis {
85+
get async throws {
8186
if let task {
82-
return await task.value
87+
return try await task.value
8388
}
84-
let task = Task<Redis?, Never> {
89+
let task = Task<Redis, Swift.Error> {
8590
var attemptsLeft = maxConnectionAttempts
8691
while attemptsLeft > 0 {
8792
do {
@@ -92,13 +97,17 @@ private actor Redis {
9297
try? await Task.sleep(for: .milliseconds(500))
9398
}
9499
}
95-
return nil
100+
throw Error.unavailable
96101
}
97102
self.task = task
98-
return await task.value
103+
return try await task.value
99104
}
100105
}
101106

107+
enum Error: Swift.Error {
108+
case unavailable
109+
}
110+
102111
private init() async throws {
103112
let connection = RedisConnection.make(
104113
configuration: try .init(hostname: Redis.hostname),
@@ -130,6 +139,10 @@ private actor Redis {
130139
return try? await client.get(.init(key)).map(\.string).get()
131140
}
132141

142+
func expire(key: String, after ttl: Duration) async throws -> Bool {
143+
try await client.expire(.init(key), after: .init(ttl)).get()
144+
}
145+
133146
func increment(key: String) async throws -> Int {
134147
try await client.increment(.init(key)).get()
135148
}

0 commit comments

Comments
 (0)