Skip to content

Commit 1aeeb15

Browse files
committed
Move Redis to own file
1 parent 78b9503 commit 1aeeb15

File tree

2 files changed

+75
-62
lines changed

2 files changed

+75
-62
lines changed

Sources/App/Core/Dependencies/CurrentReferenceCacheClient.swift

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
import Dependencies
1616
import DependenciesMacros
17-
import NIOCore
18-
@preconcurrency import RediStack
1917

2018

2119
@DependencyClient
@@ -52,66 +50,6 @@ extension DependencyValues {
5250
}
5351

5452

55-
extension CurrentReferenceCacheClient {
56-
actor Redis {
57-
var client: RedisClient
58-
static private var task: Task<Redis?, Never>?
59-
60-
static var shared: Redis? {
61-
get async {
62-
if let task {
63-
return await task.value
64-
}
65-
let task = Task<Redis?, Never> {
66-
var attemptsLeft = maxConnectionAttempts
67-
while attemptsLeft > 0 {
68-
do {
69-
return try await Redis()
70-
} catch {
71-
attemptsLeft -= 1
72-
Current.logger().warning("Redis connection failed, \(attemptsLeft) attempts left. Error: \(error)")
73-
try? await Task.sleep(for: .milliseconds(500))
74-
}
75-
}
76-
return nil
77-
}
78-
self.task = task
79-
return await task.value
80-
}
81-
}
82-
83-
private init() async throws {
84-
let connection = RedisConnection.make(
85-
configuration: try .init(hostname: Redis.hostname),
86-
boundEventLoop: NIOSingletons.posixEventLoopGroup.any()
87-
)
88-
self.client = try await connection.get()
89-
}
90-
91-
static let expirationInSeconds = 5*60
92-
static let hostname = "redis"
93-
static let maxConnectionAttempts = 3
94-
95-
func set(owner: String, repository: String, reference: String?) async -> Void {
96-
let key = "\(owner)/\(repository)".lowercased()
97-
if let reference {
98-
let buffer = ByteBuffer(string: reference)
99-
try? await client.setex(.init(key),
100-
to: RESPValue.bulkString(buffer),
101-
expirationInSeconds: Self.expirationInSeconds).get()
102-
} else {
103-
_ = try? await client.delete([.init(key)]).get()
104-
}
105-
}
106-
107-
func get(owner: String, repository: String) async -> String? {
108-
let key = "\(owner)/\(repository)".lowercased()
109-
return try? await client.get(.init(key)).map(\.string).get()
110-
}
111-
}
112-
}
113-
114-
11553
#if DEBUG
11654
extension CurrentReferenceCacheClient {
11755
static var disabled: Self {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright Dave Verwer, Sven A. Schmidt, and other contributors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import NIOCore
16+
@preconcurrency import RediStack
17+
18+
19+
actor Redis {
20+
var client: RedisClient
21+
static private var task: Task<Redis?, Never>?
22+
23+
static var shared: Redis? {
24+
get async {
25+
if let task {
26+
return await task.value
27+
}
28+
let task = Task<Redis?, Never> {
29+
var attemptsLeft = maxConnectionAttempts
30+
while attemptsLeft > 0 {
31+
do {
32+
return try await Redis()
33+
} catch {
34+
attemptsLeft -= 1
35+
Current.logger().warning("Redis connection failed, \(attemptsLeft) attempts left. Error: \(error)")
36+
try? await Task.sleep(for: .milliseconds(500))
37+
}
38+
}
39+
return nil
40+
}
41+
self.task = task
42+
return await task.value
43+
}
44+
}
45+
46+
private init() async throws {
47+
let connection = RedisConnection.make(
48+
configuration: try .init(hostname: Redis.hostname),
49+
boundEventLoop: NIOSingletons.posixEventLoopGroup.any()
50+
)
51+
self.client = try await connection.get()
52+
}
53+
54+
static let expirationInSeconds = 5*60
55+
static let hostname = "redis"
56+
static let maxConnectionAttempts = 3
57+
58+
func set(owner: String, repository: String, reference: String?) async -> Void {
59+
let key = "\(owner)/\(repository)".lowercased()
60+
if let reference {
61+
let buffer = ByteBuffer(string: reference)
62+
try? await client.setex(.init(key),
63+
to: RESPValue.bulkString(buffer),
64+
expirationInSeconds: Self.expirationInSeconds).get()
65+
} else {
66+
_ = try? await client.delete([.init(key)]).get()
67+
}
68+
}
69+
70+
func get(owner: String, repository: String) async -> String? {
71+
let key = "\(owner)/\(repository)".lowercased()
72+
return try? await client.get(.init(key)).map(\.string).get()
73+
}
74+
}
75+

0 commit comments

Comments
 (0)