Skip to content

Commit 59df55d

Browse files
Merge pull request #3588 from SwiftPackageIndex/update-redis-interface
Update redis interface
2 parents 757e43a + 7f37aea commit 59df55d

File tree

2 files changed

+46
-19
lines changed

2 files changed

+46
-19
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: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +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?
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
2527
}
2628

2729

2830
extension RedisClient {
29-
func set(key: String, value: String?, expiresIn: Duration? = nil) async {
30-
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)
33+
}
34+
35+
func increment(key: String) async throws -> Int {
36+
try await increment(key: key, by: 1)
3137
}
3238
}
3339

@@ -36,9 +42,11 @@ extension RedisClient: DependencyKey {
3642
static var liveValue: RedisClient {
3743
.init(
3844
set: { key, value, expiresIn in
39-
await Redis.shared?.set(key: key, value: value, expiresIn: expiresIn)
45+
try await Redis.shared.set(key: key, value: value, expiresIn: expiresIn)
4046
},
41-
get: { key in await Redis.shared?.get(key: key) }
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) }
4250
)
4351
}
4452
}
@@ -60,22 +68,25 @@ extension DependencyValues {
6068
#if DEBUG
6169
extension RedisClient {
6270
static var disabled: Self {
63-
.init(set: { _, _, _ in }, get: { _ in nil })
71+
.init(set: { _, _, _ in },
72+
get: { _ in nil },
73+
expire: { _, _ in true },
74+
increment: { _, value in value })
6475
}
6576
}
6677
#endif
6778

6879

6980
private actor Redis {
7081
var client: RediStack.RedisClient
71-
static private var task: Task<Redis?, Never>?
82+
static private var task: Task<Redis, Swift.Error>?
7283

73-
static var shared: Redis? {
74-
get async {
84+
static var shared: Redis {
85+
get async throws {
7586
if let task {
76-
return await task.value
87+
return try await task.value
7788
}
78-
let task = Task<Redis?, Never> {
89+
let task = Task<Redis, Swift.Error> {
7990
var attemptsLeft = maxConnectionAttempts
8091
while attemptsLeft > 0 {
8192
do {
@@ -86,13 +97,17 @@ private actor Redis {
8697
try? await Task.sleep(for: .milliseconds(500))
8798
}
8899
}
89-
return nil
100+
throw Error.unavailable
90101
}
91102
self.task = task
92-
return await task.value
103+
return try await task.value
93104
}
94105
}
95106

107+
enum Error: Swift.Error {
108+
case unavailable
109+
}
110+
96111
private init() async throws {
97112
let connection = RedisConnection.make(
98113
configuration: try .init(hostname: Redis.hostname),
@@ -105,7 +120,7 @@ private actor Redis {
105120
static let hostname = "redis"
106121
static let maxConnectionAttempts = 3
107122

108-
func set(key: String, value: String?, expiresIn: Duration?) async -> Void {
123+
func set(key: String, value: String?, expiresIn: Duration?) async {
109124
if let value {
110125
let buffer = ByteBuffer(string: value)
111126
let value = RESPValue.bulkString(buffer)
@@ -123,5 +138,17 @@ private actor Redis {
123138
func get(key: String) async -> String? {
124139
return try? await client.get(.init(key)).map(\.string).get()
125140
}
141+
142+
func expire(key: String, after ttl: Duration) async throws -> Bool {
143+
try await client.expire(.init(key), after: .init(ttl)).get()
144+
}
145+
146+
func increment(key: String) async throws -> Int {
147+
try await client.increment(.init(key)).get()
148+
}
149+
150+
func increment(key: String, by value: Int) async throws -> Int {
151+
try await client.increment(.init(key), by: value).get()
152+
}
126153
}
127154

0 commit comments

Comments
 (0)