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
|
@preconcurrency import SwiftUI
@MainActor
final class ImageCacheManager {
// MARK: - Singleton
static let shared = ImageCacheManager()
// MARK: - Private Properties
private let cache = URLCache(
memoryCapacity: 100 * 1_024 * 1_024, // 100 MB
diskCapacity: 500 * 1_024 * 1_024, // 500 MB
directory: FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first?
.appendingPathComponent("SoraImageCache", isDirectory: true)
)
private let downloadQueue = OperationQueue()
private var preloadingURLs = Set<URL>()
private var memoryWarningObserver: NSObjectProtocol?
// MARK: - Initialisation
private init() {
downloadQueue.maxConcurrentOperationCount = 3
downloadQueue.qualityOfService = .utility
#if os(iOS)
memoryWarningObserver = NotificationCenter.default.addObserver(
forName: UIApplication.didReceiveMemoryWarningNotification,
object: nil,
queue: .main
) { [weak self] _ in
Task { @MainActor in
self?.handleMemoryPressure()
}
}
#endif
}
// MARK: - Public Methods
func preloadImages(
_ urls: [URL],
domain: String,
sendUserAgent: Bool,
customUserAgent: String
) {
let newURLs = urls.filter { url in
let request = BooruRequestConfiguration.request(
url: url,
domain: domain,
sendUserAgent: sendUserAgent,
customUserAgent: customUserAgent,
accept: "image/*"
)
return !preloadingURLs.contains(url) && cache.cachedResponse(for: request) == nil
}
for url in newURLs {
preloadingURLs.insert(url)
downloadQueue.addOperation { [weak self] in
guard let self else { return }
let completionSignal = DispatchSemaphore(value: 0)
let request = BooruRequestConfiguration.request(
url: url,
domain: domain,
sendUserAgent: sendUserAgent,
customUserAgent: customUserAgent,
accept: "image/*"
)
URLSession.shared.dataTask(with: request) { data, response, _ in
Task { @MainActor [weak self] in
guard let self else {
completionSignal.signal()
return
}
defer {
preloadingURLs.remove(url)
completionSignal.signal()
}
guard let data, let response else { return }
storeCachedResponse(
data: data,
response: response,
for: request
)
}
}
.resume()
completionSignal.wait()
}
}
}
func clearCache() {
cache.removeAllCachedResponses()
preloadingURLs.removeAll()
}
func storeCachedResponse(data: Data, response: URLResponse, for request: URLRequest) {
cache.storeCachedResponse(
CachedURLResponse(response: response, data: data),
for: request
)
}
func getCachedResponse(for request: URLRequest) -> CachedURLResponse? {
cache.cachedResponse(for: request)
}
func loadImageData(
for url: URL,
domain: String,
sendUserAgent: Bool,
customUserAgent: String
) async -> Data? {
let request = BooruRequestConfiguration.request(
url: url,
domain: domain,
sendUserAgent: sendUserAgent,
customUserAgent: customUserAgent,
accept: "image/*"
)
if let cachedResponse = getCachedResponse(for: request) {
return cachedResponse.data
}
do {
let (data, response) = try await URLSession.shared.data(for: request)
storeCachedResponse(
data: data,
response: response,
for: request
)
return data
} catch {
return nil
}
}
private func handleMemoryPressure() {
cache.removeAllCachedResponses()
downloadQueue.cancelAllOperations()
preloadingURLs.removeAll()
}
deinit {
if let observer = memoryWarningObserver {
NotificationCenter.default.removeObserver(observer)
}
}
}
|