summaryrefslogtreecommitdiff
path: root/Sora/Data/ImageCacheManager.swift
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-03-01 04:37:10 -0800
committerFuwn <[email protected]>2025-03-01 04:37:10 -0800
commitffa1c05f14400889905a450477a71443c5fe1741 (patch)
treea03b022615fb47cf1546153a0a91a58f74ad4da3 /Sora/Data/ImageCacheManager.swift
parentfeat: Development commit (diff)
downloadsora-testing-ffa1c05f14400889905a450477a71443c5fe1741.tar.xz
sora-testing-ffa1c05f14400889905a450477a71443c5fe1741.zip
feat: Development commit
Diffstat (limited to 'Sora/Data/ImageCacheManager.swift')
-rw-r--r--Sora/Data/ImageCacheManager.swift36
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)
+ }
+ }
+}