1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
import CryptoKit
import Foundation
enum CachePolicy: Sendable, Equatable {
case networkOnly
case cacheOnly
case cacheFirstThenRefresh
case refreshIgnoringCache
}
enum CacheResourceType: String, Codable, Sendable {
case repositoryDetail
case repositoryList
case repositoryTree
case repositoryFile
case repositoryReadme
case ticketDetail
case ticketList
case buildDetail
case buildList
case buildLog
case userProfile
case status
case pasteList
case debug
}
struct CacheEntryMetadata: Codable, Sendable, Equatable {
let cacheKey: String
let resourceType: CacheResourceType
let fetchedAt: Date
let expiresAt: Date
var lastAccessedAt: Date
let payloadHash: String
let schemaVersion: Int
let payloadSize: Int
func isExpired(now: Date = Date()) -> Bool {
expiresAt <= now
}
}
struct APICacheEntry: Sendable {
var metadata: CacheEntryMetadata
let payload: Data
}
struct CachedValue<Value> {
let value: Value
let metadata: CacheEntryMetadata?
let source: CacheValueSource
var isFromCache: Bool { source == .cache }
var isStale: Bool { metadata?.isExpired() ?? false }
}
enum CacheValueSource: Sendable, Equatable {
case cache
case network
}
enum APICacheError: LocalizedError, Sendable {
case miss
case entryTooLarge(Int)
case cacheTooLarge
var errorDescription: String? {
switch self {
case .miss:
"No cached data is available."
case .entryTooLarge(let bytes):
"The response is too large to cache (\(bytes) bytes)."
case .cacheTooLarge:
"The cache size limit was exceeded."
}
}
}
struct APICacheConfiguration: Sendable {
var directory: URL
var maxCacheSizeBytes: Int
var maxEntrySizeBytes: Int
var memoryEntryLimit: Int
var schemaVersion: Int
static func accountScoped(accountID: String) -> APICacheConfiguration {
let base = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first
?? URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
return APICacheConfiguration(
directory: base
.appendingPathComponent("Hutch", isDirectory: true)
.appendingPathComponent("APICache", isDirectory: true)
.appendingPathComponent(accountID, isDirectory: true),
maxCacheSizeBytes: 50 * 1024 * 1024,
maxEntrySizeBytes: 2 * 1024 * 1024,
memoryEntryLimit: 64,
schemaVersion: 1
)
}
static func temporary(directory: URL) -> APICacheConfiguration {
APICacheConfiguration(
directory: directory,
maxCacheSizeBytes: 4 * 1024 * 1024,
maxEntrySizeBytes: 512 * 1024,
memoryEntryLimit: 16,
schemaVersion: 1
)
}
}
protocol APICache: Sendable {
func read(cacheKey: String) async throws -> APICacheEntry
func write(payload: Data, cacheKey: String, resourceType: CacheResourceType, ttl: TimeInterval) async throws -> CacheEntryMetadata
func remove(cacheKey: String) async
func removeByPrefix(_ prefix: String) async
func clearAll() async
func pruneExpired(now: Date) async
func pruneToSizeLimit() async
}
actor PersistentAPICache: APICache {
private struct StoredEntry: Codable, Sendable {
var metadata: CacheEntryMetadata
let payload: Data
}
private let configuration: APICacheConfiguration
private let fileManager: FileManager
private var memoryEntries: [String: APICacheEntry] = [:]
private var memoryOrder: [String] = []
private var knownMetadata: [String: CacheEntryMetadata] = [:]
private var writeCountSincePrune = 0
init(configuration: APICacheConfiguration, fileManager: FileManager = .default) {
self.configuration = configuration
self.fileManager = fileManager
}
func read(cacheKey: String) async throws -> APICacheEntry {
if var entry = memoryEntries[cacheKey] {
entry.metadata.lastAccessedAt = Date()
memoryEntries[cacheKey] = entry
markMemoryUse(cacheKey)
try? persist(entry)
return entry
}
let url = fileURL(for: cacheKey)
guard fileManager.fileExists(atPath: url.path) else {
throw APICacheError.miss
}
var stored = try decodeEntry(from: url)
guard stored.metadata.schemaVersion == configuration.schemaVersion else {
try? fileManager.removeItem(at: url)
throw APICacheError.miss
}
stored.metadata.lastAccessedAt = Date()
let entry = APICacheEntry(metadata: stored.metadata, payload: stored.payload)
knownMetadata[cacheKey] = stored.metadata
remember(entry)
try? persist(entry)
return entry
}
func write(
payload: Data,
cacheKey: String,
resourceType: CacheResourceType,
ttl: TimeInterval
) async throws -> CacheEntryMetadata {
guard payload.count <= configuration.maxEntrySizeBytes else {
throw APICacheError.entryTooLarge(payload.count)
}
try ensureDirectoryExists()
let now = Date()
let payloadHash = Self.payloadHash(payload)
if let existing = try? await read(cacheKey: cacheKey),
existing.metadata.payloadHash == payloadHash {
let metadata = CacheEntryMetadata(
cacheKey: cacheKey,
resourceType: resourceType,
fetchedAt: now,
expiresAt: now.addingTimeInterval(ttl),
lastAccessedAt: now,
payloadHash: payloadHash,
schemaVersion: configuration.schemaVersion,
payloadSize: payload.count
)
let entry = APICacheEntry(metadata: metadata, payload: payload)
remember(entry)
try persist(entry)
return metadata
}
let metadata = CacheEntryMetadata(
cacheKey: cacheKey,
resourceType: resourceType,
fetchedAt: now,
expiresAt: now.addingTimeInterval(ttl),
lastAccessedAt: now,
payloadHash: payloadHash,
schemaVersion: configuration.schemaVersion,
payloadSize: payload.count
)
let entry = APICacheEntry(metadata: metadata, payload: payload)
remember(entry)
try persist(entry)
writeCountSincePrune += 1
if writeCountSincePrune >= 12 {
writeCountSincePrune = 0
await pruneExpired(now: now)
await pruneToSizeLimit()
}
return metadata
}
func remove(cacheKey: String) async {
memoryEntries.removeValue(forKey: cacheKey)
memoryOrder.removeAll { $0 == cacheKey }
knownMetadata.removeValue(forKey: cacheKey)
try? fileManager.removeItem(at: fileURL(for: cacheKey))
}
func removeByPrefix(_ prefix: String) async {
await loadKnownMetadataIfNeeded()
for key in knownMetadata.keys where key.hasPrefix(prefix) {
await remove(cacheKey: key)
}
}
func clearAll() async {
memoryEntries.removeAll()
memoryOrder.removeAll()
knownMetadata.removeAll()
try? fileManager.removeItem(at: configuration.directory)
}
func pruneExpired(now: Date = Date()) async {
await loadKnownMetadataIfNeeded()
for metadata in knownMetadata.values where metadata.isExpired(now: now) {
await remove(cacheKey: metadata.cacheKey)
}
}
func pruneToSizeLimit() async {
await loadKnownMetadataIfNeeded()
var totalSize = knownMetadata.values.reduce(0) { $0 + $1.payloadSize }
guard totalSize > configuration.maxCacheSizeBytes else { return }
let victims = knownMetadata.values.sorted { $0.lastAccessedAt < $1.lastAccessedAt }
for metadata in victims {
await remove(cacheKey: metadata.cacheKey)
totalSize -= metadata.payloadSize
if totalSize <= configuration.maxCacheSizeBytes { break }
}
}
private func remember(_ entry: APICacheEntry) {
memoryEntries[entry.metadata.cacheKey] = entry
knownMetadata[entry.metadata.cacheKey] = entry.metadata
markMemoryUse(entry.metadata.cacheKey)
while memoryOrder.count > configuration.memoryEntryLimit, let evicted = memoryOrder.first {
memoryOrder.removeFirst()
memoryEntries.removeValue(forKey: evicted)
}
}
private func markMemoryUse(_ cacheKey: String) {
memoryOrder.removeAll { $0 == cacheKey }
memoryOrder.append(cacheKey)
}
private func persist(_ entry: APICacheEntry) throws {
try ensureDirectoryExists()
let stored = StoredEntry(metadata: entry.metadata, payload: entry.payload)
let data = try JSONEncoder.srhtCache.encode(stored)
try data.write(to: fileURL(for: entry.metadata.cacheKey), options: [.atomic])
}
private func decodeEntry(from url: URL) throws -> StoredEntry {
let data = try Data(contentsOf: url)
return try JSONDecoder.srhtCache.decode(StoredEntry.self, from: data)
}
private func loadKnownMetadataIfNeeded() async {
guard knownMetadata.isEmpty else { return }
guard let urls = try? fileManager.contentsOfDirectory(
at: configuration.directory,
includingPropertiesForKeys: nil
) else { return }
for url in urls where url.pathExtension == "json" {
guard let stored = try? decodeEntry(from: url) else { continue }
knownMetadata[stored.metadata.cacheKey] = stored.metadata
}
}
private func ensureDirectoryExists() throws {
if !fileManager.fileExists(atPath: configuration.directory.path) {
try fileManager.createDirectory(
at: configuration.directory,
withIntermediateDirectories: true
)
}
}
private func fileURL(for cacheKey: String) -> URL {
configuration.directory
.appendingPathComponent(Self.payloadHash(Data(cacheKey.utf8)))
.appendingPathExtension("json")
}
private static func payloadHash(_ data: Data) -> String {
SHA256.hash(data: data).map { String(format: "%02x", $0) }.joined()
}
}
extension JSONEncoder {
static var srhtCache: JSONEncoder {
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .iso8601
return encoder
}
}
extension JSONDecoder {
static var srhtCache: JSONDecoder {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
return decoder
}
}
|