import Combine import SwiftUI class ImageCacheManager { static let shared = ImageCacheManager() private let cache: URLCache private var cancellables: Set = [] 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) } } }