diff options
| author | Fuwn <[email protected]> | 2025-03-01 04:37:10 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-03-01 04:37:10 -0800 |
| commit | ffa1c05f14400889905a450477a71443c5fe1741 (patch) | |
| tree | a03b022615fb47cf1546153a0a91a58f74ad4da3 /Sora/Data/ImageCacheManager.swift | |
| parent | feat: Development commit (diff) | |
| download | sora-testing-ffa1c05f14400889905a450477a71443c5fe1741.tar.xz sora-testing-ffa1c05f14400889905a450477a71443c5fe1741.zip | |
feat: Development commit
Diffstat (limited to 'Sora/Data/ImageCacheManager.swift')
| -rw-r--r-- | Sora/Data/ImageCacheManager.swift | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/Sora/Data/ImageCacheManager.swift b/Sora/Data/ImageCacheManager.swift new file mode 100644 index 0000000..cc5cb7e --- /dev/null +++ b/Sora/Data/ImageCacheManager.swift @@ -0,0 +1,36 @@ +import Combine +import SwiftUI + +class ImageCacheManager { + 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" + ) + + URLCache.shared = cache + } + + func preloadImages(_ urls: [URL]) { + urls.forEach { url in + URLSession.shared.dataTaskPublisher(for: url) + .sink( + receiveCompletion: { _ in () }, + receiveValue: { data, response in + let cachedResponse = CachedURLResponse( + response: response, + data: data + ) + + self.cache.storeCachedResponse(cachedResponse, for: URLRequest(url: url)) + } + ) + .store(in: &cancellables) + } + } +} |