import AppKit class ImageCache { static let shared = ImageCache() private let cache = NSCache() private init() {} func image(for url: URL) -> NSImage? { return cache.object(forKey: url.absoluteString as NSString) } func insertImage(_ image: NSImage, for url: URL) { cache.setObject(image, forKey: url.absoluteString as NSString) } func fetchImage(url: URL) async throws -> NSImage { if let cached = image(for: url) { return cached } let (data, _) = try await URLSession.shared.data(from: url) guard let image = NSImage(data: data) else { throw WallhavenError.decodingError(NSError(domain: "ImageCache", code: -1, userInfo: nil)) } insertImage(image, for: url) return image } }