// // KMThumbnailCache.swift // PDF Reader Pro // // Created by lizhe on 2024/8/9. // class KMThumbnailCache: NSObject { static let shared = KMThumbnailCache() private let maxCacheSize = 50 private var thumbnails: [Int: NSImage] = [:] private var order: [Int] = [] // 记录插入顺序的数组 func addThumbnail(id: Int, image: NSImage) { if thumbnails.count >= maxCacheSize { if let oldestID = order.first { thumbnails.removeValue(forKey: oldestID) order.removeFirst() } } thumbnails[id] = image order.append(id) } func thumbnail(for id: Int) -> NSImage? { return thumbnails[id] } func removeThumbnail(for id: Int) { if let index = order.firstIndex(of: id) { thumbnails.removeValue(forKey: id) order.remove(at: index) } else { print("ID not found") } } func clearCache() { thumbnails.removeAll() order.removeAll() } }