| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742 |
- #if os(macOS)
- import AppKit
- #else
- import UIKit
- #endif
- extension Notification.Name {
-
- public static let KingfisherDidCleanDiskCache = Notification.Name.init("com.onevcat.Kingfisher.KingfisherDidCleanDiskCache")
- }
- public let KingfisherDiskCacheCleanedHashKey = "com.onevcat.Kingfisher.cleanedHash"
- public typealias RetrieveImageDiskTask = DispatchWorkItem
- public enum CacheType {
- case none, memory, disk
-
- public var cached: Bool {
- switch self {
- case .memory, .disk: return true
- case .none: return false
- }
- }
- }
- open class ImageCache {
-
- fileprivate let memoryCache = NSCache<NSString, AnyObject>()
-
-
-
-
-
- open var maxMemoryCost: UInt = 0 {
- didSet {
- self.memoryCache.totalCostLimit = Int(maxMemoryCost)
- }
- }
-
-
- fileprivate let ioQueue: DispatchQueue
- fileprivate var fileManager: FileManager!
-
-
- public let diskCachePath: String
-
-
- open var pathExtension: String?
-
-
-
-
- open var maxCachePeriodInSecond: TimeInterval = 60 * 60 * 24 * 7
-
-
-
-
- open var maxDiskCacheSize: UInt = 0
-
- fileprivate let processQueue: DispatchQueue
-
-
- public static let `default` = ImageCache(name: "default")
-
-
- public typealias DiskCachePathClosure = (String?, String) -> String
-
-
- public final class func defaultDiskCachePathClosure(path: String?, cacheName: String) -> String {
- let dstPath = path ?? NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first!
- return (dstPath as NSString).appendingPathComponent(cacheName)
- }
-
-
- public init(name: String,
- path: String? = nil,
- diskCachePathClosure: DiskCachePathClosure = ImageCache.defaultDiskCachePathClosure)
- {
-
- if name.isEmpty {
- fatalError("[Kingfisher] You should specify a name for the cache. A cache with empty name is not permitted.")
- }
-
- let cacheName = "com.onevcat.Kingfisher.ImageCache.\(name)"
- memoryCache.name = cacheName
-
- diskCachePath = diskCachePathClosure(path, cacheName)
-
- let ioQueueName = "com.onevcat.Kingfisher.ImageCache.ioQueue.\(name)"
- ioQueue = DispatchQueue(label: ioQueueName)
-
- let processQueueName = "com.onevcat.Kingfisher.ImageCache.processQueue.\(name)"
- processQueue = DispatchQueue(label: processQueueName, attributes: .concurrent)
-
- ioQueue.sync { fileManager = FileManager() }
-
- #if !os(macOS) && !os(watchOS)
-
- #if swift(>=4.2)
- let memoryNotification = UIApplication.didReceiveMemoryWarningNotification
- let terminateNotification = UIApplication.willTerminateNotification
- let enterbackgroundNotification = UIApplication.didEnterBackgroundNotification
- #else
- let memoryNotification = NSNotification.Name.UIApplicationDidReceiveMemoryWarning
- let terminateNotification = NSNotification.Name.UIApplicationWillTerminate
- let enterbackgroundNotification = NSNotification.Name.UIApplicationDidEnterBackground
- #endif
-
- NotificationCenter.default.addObserver(
- self, selector: #selector(clearMemoryCache), name: memoryNotification, object: nil)
- NotificationCenter.default.addObserver(
- self, selector: #selector(cleanExpiredDiskCache), name: terminateNotification, object: nil)
- NotificationCenter.default.addObserver(
- self, selector: #selector(backgroundCleanExpiredDiskCache), name: enterbackgroundNotification, object: nil)
- #endif
- }
-
- deinit {
- NotificationCenter.default.removeObserver(self)
- }
-
-
- open func store(_ image: Image,
- original: Data? = nil,
- forKey key: String,
- processorIdentifier identifier: String = "",
- cacheSerializer serializer: CacheSerializer = DefaultCacheSerializer.default,
- toDisk: Bool = true,
- completionHandler: (() -> Void)? = nil)
- {
-
- let computedKey = key.computedKey(with: identifier)
- memoryCache.setObject(image, forKey: computedKey as NSString, cost: image.kf.imageCost)
- func callHandlerInMainQueue() {
- if let handler = completionHandler {
- DispatchQueue.main.async {
- handler()
- }
- }
- }
-
- if toDisk {
- ioQueue.async {
-
- if let data = serializer.data(with: image, original: original) {
- if !self.fileManager.fileExists(atPath: self.diskCachePath) {
- do {
- try self.fileManager.createDirectory(atPath: self.diskCachePath, withIntermediateDirectories: true, attributes: nil)
- } catch _ {}
- }
-
- self.fileManager.createFile(atPath: self.cachePath(forComputedKey: computedKey), contents: data, attributes: nil)
- }
- callHandlerInMainQueue()
- }
- } else {
- callHandlerInMainQueue()
- }
- }
-
-
- open func removeImage(forKey key: String,
- processorIdentifier identifier: String = "",
- fromMemory: Bool = true,
- fromDisk: Bool = true,
- completionHandler: (() -> Void)? = nil)
- {
- let computedKey = key.computedKey(with: identifier)
- if fromMemory {
- memoryCache.removeObject(forKey: computedKey as NSString)
- }
-
- func callHandlerInMainQueue() {
- if let handler = completionHandler {
- DispatchQueue.main.async {
- handler()
- }
- }
- }
-
- if fromDisk {
- ioQueue.async{
- do {
- try self.fileManager.removeItem(atPath: self.cachePath(forComputedKey: computedKey))
- } catch _ {}
- callHandlerInMainQueue()
- }
- } else {
- callHandlerInMainQueue()
- }
- }
-
-
- @discardableResult
- open func retrieveImage(forKey key: String,
- options: KingfisherOptionsInfo?,
- completionHandler: ((Image?, CacheType) -> Void)?) -> RetrieveImageDiskTask?
- {
-
- guard let completionHandler = completionHandler else {
- return nil
- }
-
- var block: RetrieveImageDiskTask?
- let options = options ?? KingfisherEmptyOptionsInfo
- let imageModifier = options.imageModifier
- if let image = self.retrieveImageInMemoryCache(forKey: key, options: options) {
- options.callbackDispatchQueue.safeAsync {
- completionHandler(imageModifier.modify(image), .memory)
- }
- } else if options.fromMemoryCacheOrRefresh {
- options.callbackDispatchQueue.safeAsync {
- completionHandler(nil, .none)
- }
- } else {
- var sSelf: ImageCache! = self
- block = DispatchWorkItem(block: {
-
- if let image = sSelf.retrieveImageInDiskCache(forKey: key, options: options) {
- if options.backgroundDecode {
- sSelf.processQueue.async {
- let result = image.kf.decoded
-
- sSelf.store(result,
- forKey: key,
- processorIdentifier: options.processor.identifier,
- cacheSerializer: options.cacheSerializer,
- toDisk: false,
- completionHandler: nil)
- options.callbackDispatchQueue.safeAsync {
- completionHandler(imageModifier.modify(result), .disk)
- sSelf = nil
- }
- }
- } else {
- sSelf.store(image,
- forKey: key,
- processorIdentifier: options.processor.identifier,
- cacheSerializer: options.cacheSerializer,
- toDisk: false,
- completionHandler: nil
- )
- options.callbackDispatchQueue.safeAsync {
- completionHandler(imageModifier.modify(image), .disk)
- sSelf = nil
- }
- }
- } else {
-
- options.callbackDispatchQueue.safeAsync {
- completionHandler(nil, .none)
- sSelf = nil
- }
- }
- })
-
- sSelf.ioQueue.async(execute: block!)
- }
-
- return block
- }
-
-
- open func retrieveImageInMemoryCache(forKey key: String, options: KingfisherOptionsInfo? = nil) -> Image? {
-
- let options = options ?? KingfisherEmptyOptionsInfo
- let computedKey = key.computedKey(with: options.processor.identifier)
-
- return memoryCache.object(forKey: computedKey as NSString) as? Image
- }
-
-
- open func retrieveImageInDiskCache(forKey key: String, options: KingfisherOptionsInfo? = nil) -> Image? {
-
- let options = options ?? KingfisherEmptyOptionsInfo
- let computedKey = key.computedKey(with: options.processor.identifier)
-
- return diskImage(forComputedKey: computedKey, serializer: options.cacheSerializer, options: options)
- }
-
-
- @objc public func clearMemoryCache() {
- memoryCache.removeAllObjects()
- }
-
-
- open func clearDiskCache(completion handler: (()->())? = nil) {
- ioQueue.async {
- do {
- try self.fileManager.removeItem(atPath: self.diskCachePath)
- try self.fileManager.createDirectory(atPath: self.diskCachePath, withIntermediateDirectories: true, attributes: nil)
- } catch _ { }
-
- if let handler = handler {
- DispatchQueue.main.async {
- handler()
- }
- }
- }
- }
-
-
- @objc fileprivate func cleanExpiredDiskCache() {
- cleanExpiredDiskCache(completion: nil)
- }
-
-
- open func cleanExpiredDiskCache(completion handler: (()->())? = nil) {
-
-
- ioQueue.async {
-
- var (URLsToDelete, diskCacheSize, cachedFiles) = self.travelCachedFiles(onlyForCacheSize: false)
-
- for fileURL in URLsToDelete {
- do {
- try self.fileManager.removeItem(at: fileURL)
- } catch _ { }
- }
-
- if self.maxDiskCacheSize > 0 && diskCacheSize > self.maxDiskCacheSize {
- let targetSize = self.maxDiskCacheSize / 2
-
-
- let sortedFiles = cachedFiles.keysSortedByValue {
- resourceValue1, resourceValue2 -> Bool in
-
- if let date1 = resourceValue1.contentAccessDate,
- let date2 = resourceValue2.contentAccessDate
- {
- return date1.compare(date2) == .orderedAscending
- }
-
-
- return true
- }
-
- for fileURL in sortedFiles {
-
- do {
- try self.fileManager.removeItem(at: fileURL)
- } catch { }
-
- URLsToDelete.append(fileURL)
-
- if let fileSize = cachedFiles[fileURL]?.totalFileAllocatedSize {
- diskCacheSize -= UInt(fileSize)
- }
-
- if diskCacheSize < targetSize {
- break
- }
- }
- }
-
- DispatchQueue.main.async {
-
- if URLsToDelete.count != 0 {
- let cleanedHashes = URLsToDelete.map { $0.lastPathComponent }
- NotificationCenter.default.post(name: .KingfisherDidCleanDiskCache, object: self, userInfo: [KingfisherDiskCacheCleanedHashKey: cleanedHashes])
- }
-
- handler?()
- }
- }
- }
-
- fileprivate func travelCachedFiles(onlyForCacheSize: Bool) -> (urlsToDelete: [URL], diskCacheSize: UInt, cachedFiles: [URL: URLResourceValues]) {
-
- let diskCacheURL = URL(fileURLWithPath: diskCachePath)
- let resourceKeys: Set<URLResourceKey> = [.isDirectoryKey, .contentAccessDateKey, .totalFileAllocatedSizeKey]
- let expiredDate: Date? = (maxCachePeriodInSecond < 0) ? nil : Date(timeIntervalSinceNow: -maxCachePeriodInSecond)
-
- var cachedFiles = [URL: URLResourceValues]()
- var urlsToDelete = [URL]()
- var diskCacheSize: UInt = 0
- for fileUrl in (try? fileManager.contentsOfDirectory(at: diskCacheURL, includingPropertiesForKeys: Array(resourceKeys), options: .skipsHiddenFiles)) ?? [] {
- do {
- let resourceValues = try fileUrl.resourceValues(forKeys: resourceKeys)
-
- if resourceValues.isDirectory == true {
- continue
- }
-
- if !onlyForCacheSize,
- let expiredDate = expiredDate,
- let lastAccessData = resourceValues.contentAccessDate,
- (lastAccessData as NSDate).laterDate(expiredDate) == expiredDate
- {
- urlsToDelete.append(fileUrl)
- continue
- }
- if let fileSize = resourceValues.totalFileAllocatedSize {
- diskCacheSize += UInt(fileSize)
- if !onlyForCacheSize {
- cachedFiles[fileUrl] = resourceValues
- }
- }
- } catch _ { }
- }
- return (urlsToDelete, diskCacheSize, cachedFiles)
- }
- #if !os(macOS) && !os(watchOS)
-
- @objc public func backgroundCleanExpiredDiskCache() {
-
- guard let sharedApplication = Kingfisher<UIApplication>.shared else { return }
- func endBackgroundTask(_ task: inout UIBackgroundTaskIdentifier) {
- sharedApplication.endBackgroundTask(task)
- #if swift(>=4.2)
- task = UIBackgroundTaskIdentifier.invalid
- #else
- task = UIBackgroundTaskInvalid
- #endif
- }
-
- var backgroundTask: UIBackgroundTaskIdentifier!
- backgroundTask = sharedApplication.beginBackgroundTask {
- endBackgroundTask(&backgroundTask!)
- }
-
- cleanExpiredDiskCache {
- endBackgroundTask(&backgroundTask!)
- }
- }
- #endif
-
-
-
-
-
-
-
-
- open func imageCachedType(forKey key: String, processorIdentifier identifier: String = "") -> CacheType {
- let computedKey = key.computedKey(with: identifier)
-
- if memoryCache.object(forKey: computedKey as NSString) != nil {
- return .memory
- }
-
- let filePath = cachePath(forComputedKey: computedKey)
-
- var diskCached = false
- ioQueue.sync {
- diskCached = fileManager.fileExists(atPath: filePath)
- }
-
- if diskCached {
- return .disk
- }
-
- return .none
- }
-
-
- open func hash(forKey key: String, processorIdentifier identifier: String = "") -> String {
- let computedKey = key.computedKey(with: identifier)
- return cacheFileName(forComputedKey: computedKey)
- }
-
-
- open func calculateDiskCacheSize(completion handler: @escaping ((_ size: UInt) -> Void)) {
- ioQueue.async {
- let (_, diskCacheSize, _) = self.travelCachedFiles(onlyForCacheSize: true)
- DispatchQueue.main.async {
- handler(diskCacheSize)
- }
- }
- }
-
-
- open func cachePath(forKey key: String, processorIdentifier identifier: String = "") -> String {
- let computedKey = key.computedKey(with: identifier)
- return cachePath(forComputedKey: computedKey)
- }
- open func cachePath(forComputedKey key: String) -> String {
- let fileName = cacheFileName(forComputedKey: key)
- return (diskCachePath as NSString).appendingPathComponent(fileName)
- }
- }
- extension ImageCache {
-
- func diskImage(forComputedKey key: String, serializer: CacheSerializer, options: KingfisherOptionsInfo) -> Image? {
- if let data = diskImageData(forComputedKey: key) {
- return serializer.image(with: data, options: options)
- } else {
- return nil
- }
- }
-
- func diskImageData(forComputedKey key: String) -> Data? {
- let filePath = cachePath(forComputedKey: key)
- return (try? Data(contentsOf: URL(fileURLWithPath: filePath)))
- }
-
- func cacheFileName(forComputedKey key: String) -> String {
- if let ext = self.pathExtension {
- return (key.kf.md5 as NSString).appendingPathExtension(ext)!
- }
- return key.kf.md5
- }
- }
- extension ImageCache {
-
- @available(*, deprecated,
- message: "CacheCheckResult is deprecated. Use imageCachedType(forKey:processorIdentifier:) API instead.")
- public struct CacheCheckResult {
- public let cached: Bool
- public let cacheType: CacheType?
- }
-
-
- @available(*, deprecated,
- message: "Use imageCachedType(forKey:processorIdentifier:) instead. CacheCheckResult.none indicates not being cached.",
- renamed: "imageCachedType(forKey:processorIdentifier:)")
- open func isImageCached(forKey key: String, processorIdentifier identifier: String = "") -> CacheCheckResult {
- let result = imageCachedType(forKey: key, processorIdentifier: identifier)
- switch result {
- case .memory, .disk:
- return CacheCheckResult(cached: true, cacheType: result)
- case .none:
- return CacheCheckResult(cached: false, cacheType: nil)
- }
- }
- }
- extension Kingfisher where Base: Image {
- var imageCost: Int {
- return images == nil ?
- Int(size.height * size.width * scale * scale) :
- Int(size.height * size.width * scale * scale) * images!.count
- }
- }
- extension Dictionary {
- func keysSortedByValue(_ isOrderedBefore: (Value, Value) -> Bool) -> [Key] {
- return Array(self).sorted{ isOrderedBefore($0.1, $1.1) }.map{ $0.0 }
- }
- }
- #if !os(macOS) && !os(watchOS)
- extension UIApplication: KingfisherCompatible { }
- extension Kingfisher where Base: UIApplication {
- public static var shared: UIApplication? {
- let selector = NSSelectorFromString("sharedApplication")
- guard Base.responds(to: selector) else { return nil }
- return Base.perform(selector).takeUnretainedValue() as? UIApplication
- }
- }
- #endif
- extension String {
- func computedKey(with identifier: String) -> String {
- if identifier.isEmpty {
- return self
- } else {
- return appending("@\(identifier)")
- }
- }
- }
|