diff options
| author | Fuwn <[email protected]> | 2025-03-01 20:44:37 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-03-01 20:44:37 -0800 |
| commit | 9d21c7dd2e8cf1adef72044ec69845e0d7027536 (patch) | |
| tree | e976b5b4e27cea6a2109f98102760cfe93090ce4 /Sora/Data | |
| parent | feat: Development commit (diff) | |
| download | sora-testing-9d21c7dd2e8cf1adef72044ec69845e0d7027536.tar.xz sora-testing-9d21c7dd2e8cf1adef72044ec69845e0d7027536.zip | |
feat: Development commit
Diffstat (limited to 'Sora/Data')
| -rw-r--r-- | Sora/Data/ImageCacheManager.swift | 32 |
1 files changed, 15 insertions, 17 deletions
diff --git a/Sora/Data/ImageCacheManager.swift b/Sora/Data/ImageCacheManager.swift index cc5cb7e..414ad4c 100644 --- a/Sora/Data/ImageCacheManager.swift +++ b/Sora/Data/ImageCacheManager.swift @@ -1,34 +1,32 @@ import Combine import SwiftUI -class ImageCacheManager { +final class ImageCacheManager { + // MARK: - Shared Instance static let shared = ImageCacheManager() - private let cache: URLCache - private var cancellables: Set<AnyCancellable> = [] - init() { - cache = URLCache( - memoryCapacity: 100 * 1_024 * 1_024, // 100 MB - diskCapacity: 500 * 1_024 * 1_024, // 500 MB - diskPath: "SoraImageCache" - ) + // 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 var cancellables = Set<AnyCancellable>() + // MARK: - Initialization + private init() { URLCache.shared = cache } + // MARK: - Public Methods func preloadImages(_ urls: [URL]) { urls.forEach { url in URLSession.shared.dataTaskPublisher(for: url) + .map { CachedURLResponse(response: $0.response, data: $0.data) } .sink( receiveCompletion: { _ in () }, - receiveValue: { data, response in - let cachedResponse = CachedURLResponse( - response: response, - data: data - ) - - self.cache.storeCachedResponse(cachedResponse, for: URLRequest(url: url)) - } + receiveValue: { [cache] in cache.storeCachedResponse($0, for: URLRequest(url: url)) } ) .store(in: &cancellables) } |