| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647 |
- //
- // ImageCache.swift
- // Kingfisher
- //
- // Created by Wei Wang on 15/4/6.
- //
- // Copyright (c) 2016 Wei Wang <onevcat@gmail.com>
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- #if os(OSX)
- import AppKit
- #else
- import UIKit
- #endif
- /**
- This notification will be sent when the disk cache got cleaned either there are cached files expired or the total size exceeding the max allowed size. The manually invoking of `clearDiskCache` method will not trigger this notification.
- The `object` of this notification is the `ImageCache` object which sends the notification.
- A list of removed hashes (files) could be retrieved by accessing the array under `KingfisherDiskCacheCleanedHashKey` key in `userInfo` of the notification object you received. By checking the array, you could know the hash codes of files are removed.
- The main purpose of this notification is supplying a chance to maintain some necessary information on the cached files. See [this wiki](https://github.com/onevcat/Kingfisher/wiki/How-to-implement-ETag-based-304-(Not-Modified)-handling-in-Kingfisher) for a use case on it.
- */
- public let KingfisherDidCleanDiskCacheNotification = "com.onevcat.Kingfisher.KingfisherDidCleanDiskCacheNotification"
- /**
- Key for array of cleaned hashes in `userInfo` of `KingfisherDidCleanDiskCacheNotification`.
- */
- public let KingfisherDiskCacheCleanedHashKey = "com.onevcat.Kingfisher.cleanedHash"
- private let defaultCacheName = "default"
- private let cacheReverseDNS = "com.onevcat.Kingfisher.ImageCache."
- private let ioQueueName = "com.onevcat.Kingfisher.ImageCache.ioQueue."
- private let processQueueName = "com.onevcat.Kingfisher.ImageCache.processQueue."
- private let defaultCacheInstance = ImageCache(name: defaultCacheName)
- private let defaultMaxCachePeriodInSecond: NSTimeInterval = 60 * 60 * 24 * 7 //Cache exists for 1 week
- /// It represents a task of retrieving image. You can call `cancel` on it to stop the process.
- public typealias RetrieveImageDiskTask = dispatch_block_t
- /**
- Cache type of a cached image.
- - None: The image is not cached yet when retrieving it.
- - Memory: The image is cached in memory.
- - Disk: The image is cached in disk.
- */
- public enum CacheType {
- case None, Memory, Disk
- }
- /// `ImageCache` represents both the memory and disk cache system of Kingfisher. While a default image cache object will be used if you prefer the extension methods of Kingfisher, you can create your own cache object and configure it as your need. You should use an `ImageCache` object to manipulate memory and disk cache for Kingfisher.
- public class ImageCache {
- //Memory
- private let memoryCache = NSCache()
-
- /// The largest cache cost of memory cache. The total cost is pixel count of all cached images in memory.
- public var maxMemoryCost: UInt = 0 {
- didSet {
- self.memoryCache.totalCostLimit = Int(maxMemoryCost)
- }
- }
-
- //Disk
- private let ioQueue: dispatch_queue_t
- private var fileManager: NSFileManager!
-
- ///The disk cache location.
- public let diskCachePath: String
-
- /// The longest time duration of the cache being stored in disk. Default is 1 week.
- public var maxCachePeriodInSecond = defaultMaxCachePeriodInSecond
-
- /// The largest disk size can be taken for the cache. It is the total allocated size of cached files in bytes. Default is 0, which means no limit.
- public var maxDiskCacheSize: UInt = 0
-
- private let processQueue: dispatch_queue_t
-
- /// The default cache.
- public class var defaultCache: ImageCache {
- return defaultCacheInstance
- }
-
- /**
- Init method. Passing a name for the cache. It represents a cache folder in the memory and disk.
-
- - parameter name: Name of the cache. It will be used as the memory cache name and the disk cache folder name appending to the cache path. This value should not be an empty string.
- - parameter path: Optional - Location of cache path on disk. If `nil` is passed (the default value),
- the cache folder in of your app will be used. If you want to cache some user generating images, you could pass the Documentation path here.
-
- - returns: The cache object.
- */
- public init(name: String, path: String? = nil) {
-
- if name.isEmpty {
- fatalError("[Kingfisher] You should specify a name for the cache. A cache with empty name is not permitted.")
- }
-
- let cacheName = cacheReverseDNS + name
- memoryCache.name = cacheName
-
- let dstPath = path ?? NSSearchPathForDirectoriesInDomains(.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true).first!
- diskCachePath = (dstPath as NSString).stringByAppendingPathComponent(cacheName)
-
- ioQueue = dispatch_queue_create(ioQueueName + name, DISPATCH_QUEUE_SERIAL)
- processQueue = dispatch_queue_create(processQueueName + name, DISPATCH_QUEUE_CONCURRENT)
-
- dispatch_sync(ioQueue, { () -> Void in
- self.fileManager = NSFileManager()
- })
-
- #if !os(OSX) && !os(watchOS)
- NSNotificationCenter.defaultCenter().addObserver(self, selector: "clearMemoryCache", name: UIApplicationDidReceiveMemoryWarningNotification, object: nil)
- NSNotificationCenter.defaultCenter().addObserver(self, selector: "cleanExpiredDiskCache", name: UIApplicationWillTerminateNotification, object: nil)
- NSNotificationCenter.defaultCenter().addObserver(self, selector: "backgroundCleanExpiredDiskCache", name: UIApplicationDidEnterBackgroundNotification, object: nil)
- #endif
- }
-
- deinit {
- NSNotificationCenter.defaultCenter().removeObserver(self)
- }
- }
- // MARK: - Store & Remove
- extension ImageCache {
- /**
- Store an image to cache. It will be saved to both memory and disk.
- It is an async operation, if you need to do something about the stored image, use `-storeImage:forKey:toDisk:completionHandler:`
- instead.
-
- - parameter image: The image will be stored.
- - parameter originalData: The original data of the image.
- Kingfisher will use it to check the format of the image and optimize cache size on disk.
- If `nil` is supplied, the image data will be saved as a normalized PNG file.
- It is strongly suggested to supply it whenever possible, to get a better performance and disk usage.
- - parameter key: Key for the image.
- */
- public func storeImage(image: Image, originalData: NSData? = nil, forKey key: String) {
- storeImage(image, originalData: originalData, forKey: key, toDisk: true, completionHandler: nil)
- }
-
- /**
- Store an image to cache. It is an async operation.
-
- - parameter image: The image will be stored.
- - parameter originalData: The original data of the image.
- Kingfisher will use it to check the format of the image and optimize cache size on disk.
- If `nil` is supplied, the image data will be saved as a normalized PNG file.
- It is strongly suggested to supply it whenever possible, to get a better performance and disk usage.
- - parameter key: Key for the image.
- - parameter toDisk: Whether this image should be cached to disk or not. If false, the image will be only cached in memory.
- - parameter completionHandler: Called when stroe operation completes.
- */
- public func storeImage(image: Image, originalData: NSData? = nil, forKey key: String, toDisk: Bool, completionHandler: (() -> ())?) {
- memoryCache.setObject(image, forKey: key, cost: image.kf_imageCost)
-
- func callHandlerInMainQueue() {
- if let handler = completionHandler {
- dispatch_async(dispatch_get_main_queue()) {
- handler()
- }
- }
- }
-
- if toDisk {
- dispatch_async(ioQueue, { () -> Void in
- let imageFormat: ImageFormat
- if let originalData = originalData {
- imageFormat = originalData.kf_imageFormat
- } else {
- imageFormat = .Unknown
- }
-
- let data: NSData?
- switch imageFormat {
- case .PNG: data = originalData ?? ImagePNGRepresentation(image)
- case .JPEG: data = originalData ?? ImageJPEGRepresentation(image, 1.0)
- case .GIF: data = originalData ?? ImageGIFRepresentation(image)
- case .Unknown: data = originalData ?? ImagePNGRepresentation(image.kf_normalizedImage())
- }
-
- if let data = data {
- if !self.fileManager.fileExistsAtPath(self.diskCachePath) {
- do {
- try self.fileManager.createDirectoryAtPath(self.diskCachePath, withIntermediateDirectories: true, attributes: nil)
- } catch _ {}
- }
-
- self.fileManager.createFileAtPath(self.cachePathForKey(key), contents: data, attributes: nil)
- }
- callHandlerInMainQueue()
- })
- } else {
- callHandlerInMainQueue()
- }
- }
-
- /**
- Remove the image for key for the cache. It will be opted out from both memory and disk.
- It is an async operation, if you need to do something about the stored image, use `-removeImageForKey:fromDisk:completionHandler:`
- instead.
-
- - parameter key: Key for the image.
- */
- public func removeImageForKey(key: String) {
- removeImageForKey(key, fromDisk: true, completionHandler: nil)
- }
-
- /**
- Remove the image for key for the cache. It is an async operation.
-
- - parameter key: Key for the image.
- - parameter fromDisk: Whether this image should be removed from disk or not. If false, the image will be only removed from memory.
- - parameter completionHandler: Called when removal operation completes.
- */
- public func removeImageForKey(key: String, fromDisk: Bool, completionHandler: (() -> ())?) {
- memoryCache.removeObjectForKey(key)
-
- func callHandlerInMainQueue() {
- if let handler = completionHandler {
- dispatch_async(dispatch_get_main_queue()) {
- handler()
- }
- }
- }
-
- if fromDisk {
- dispatch_async(ioQueue, { () -> Void in
- do {
- try self.fileManager.removeItemAtPath(self.cachePathForKey(key))
- } catch _ {}
- callHandlerInMainQueue()
- })
- } else {
- callHandlerInMainQueue()
- }
- }
-
- }
- // MARK: - Get data from cache
- extension ImageCache {
- /**
- Get an image for a key from memory or disk.
-
- - parameter key: Key for the image.
- - parameter options: Options of retrieving image.
- - parameter completionHandler: Called when getting operation completes with image result and cached type of this image. If there is no such key cached, the image will be `nil`.
-
- - returns: The retrieving task.
- */
- public func retrieveImageForKey(key: String, options: KingfisherOptionsInfo?, completionHandler: ((Image?, CacheType!) -> ())?) -> RetrieveImageDiskTask? {
- // No completion handler. Not start working and early return.
- guard let completionHandler = completionHandler else {
- return nil
- }
-
- var block: RetrieveImageDiskTask?
- let options = options ?? KingfisherEmptyOptionsInfo
-
- if let image = self.retrieveImageInMemoryCacheForKey(key) {
- dispatch_async_safely_to_queue(options.callbackDispatchQueue) { () -> Void in
- completionHandler(image, .Memory)
- }
- } else {
- var sSelf: ImageCache! = self
- block = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS) {
- // Begin to load image from disk
- if let image = sSelf.retrieveImageInDiskCacheForKey(key, scale: options.scaleFactor) {
- if options.backgroundDecode {
- dispatch_async(sSelf.processQueue, { () -> Void in
- let result = image.kf_decodedImage(scale: options.scaleFactor)
- sSelf.storeImage(result!, forKey: key, toDisk: false, completionHandler: nil)
- dispatch_async_safely_to_queue(options.callbackDispatchQueue, { () -> Void in
- completionHandler(result, .Memory)
- sSelf = nil
- })
- })
- } else {
- sSelf.storeImage(image, forKey: key, toDisk: false, completionHandler: nil)
- dispatch_async_safely_to_queue(options.callbackDispatchQueue, { () -> Void in
- completionHandler(image, .Disk)
- sSelf = nil
- })
- }
- } else {
- // No image found from either memory or disk
- dispatch_async_safely_to_queue(options.callbackDispatchQueue, { () -> Void in
- completionHandler(nil, nil)
- sSelf = nil
- })
- }
- }
-
- dispatch_async(sSelf.ioQueue, block!)
- }
-
- return block
- }
-
- /**
- Get an image for a key from memory.
-
- - parameter key: Key for the image.
-
- - returns: The image object if it is cached, or `nil` if there is no such key in the cache.
- */
- public func retrieveImageInMemoryCacheForKey(key: String) -> Image? {
- return memoryCache.objectForKey(key) as? Image
- }
-
- /**
- Get an image for a key from disk.
-
- - parameter key: Key for the image.
- - param scale: The scale factor to assume when interpreting the image data.
- - returns: The image object if it is cached, or `nil` if there is no such key in the cache.
- */
- public func retrieveImageInDiskCacheForKey(key: String, scale: CGFloat = 1.0) -> Image? {
- return diskImageForKey(key, scale: scale)
- }
- }
- // MARK: - Clear & Clean
- extension ImageCache {
- /**
- Clear memory cache.
- */
- @objc public func clearMemoryCache() {
- memoryCache.removeAllObjects()
- }
- /**
- Clear disk cache. This is could be an async or sync operation.
- Specify the way you want it by passing the `sync` parameter.
- */
- public func clearDiskCache() {
- clearDiskCacheWithCompletionHandler(nil)
- }
-
- /**
- Clear disk cache. This is an async operation.
-
- - parameter completionHander: Called after the operation completes.
- */
- public func clearDiskCacheWithCompletionHandler(completionHander: (()->())?) {
- dispatch_async(ioQueue, { () -> Void in
- do {
- try self.fileManager.removeItemAtPath(self.diskCachePath)
- try self.fileManager.createDirectoryAtPath(self.diskCachePath, withIntermediateDirectories: true, attributes: nil)
- } catch _ {
- }
-
- if let completionHander = completionHander {
- dispatch_async(dispatch_get_main_queue(), { () -> Void in
- completionHander()
- })
- }
- })
- }
-
- /**
- Clean expired disk cache. This is an async operation.
- */
- @objc public func cleanExpiredDiskCache() {
- cleanExpiredDiskCacheWithCompletionHander(nil)
- }
-
- /**
- Clean expired disk cache. This is an async operation.
-
- - parameter completionHandler: Called after the operation completes.
- */
- public func cleanExpiredDiskCacheWithCompletionHander(completionHandler: (()->())?) {
-
- // Do things in cocurrent io queue
- dispatch_async(ioQueue, { () -> Void in
-
- var (URLsToDelete, diskCacheSize, cachedFiles) = self.travelCachedFiles(onlyForCacheSize: false)
-
- for fileURL in URLsToDelete {
- do {
- try self.fileManager.removeItemAtURL(fileURL)
- } catch _ {
- }
- }
-
- if self.maxDiskCacheSize > 0 && diskCacheSize > self.maxDiskCacheSize {
- let targetSize = self.maxDiskCacheSize / 2
-
- // Sort files by last modify date. We want to clean from the oldest files.
- let sortedFiles = cachedFiles.keysSortedByValue {
- resourceValue1, resourceValue2 -> Bool in
-
- if let date1 = resourceValue1[NSURLContentModificationDateKey] as? NSDate,
- date2 = resourceValue2[NSURLContentModificationDateKey] as? NSDate {
- return date1.compare(date2) == .OrderedAscending
- }
- // Not valid date information. This should not happen. Just in case.
- return true
- }
-
- for fileURL in sortedFiles {
-
- do {
- try self.fileManager.removeItemAtURL(fileURL)
- } catch {
-
- }
-
- URLsToDelete.append(fileURL)
-
- if let fileSize = cachedFiles[fileURL]?[NSURLTotalFileAllocatedSizeKey] as? NSNumber {
- diskCacheSize -= fileSize.unsignedLongValue
- }
-
- if diskCacheSize < targetSize {
- break
- }
- }
- }
-
- dispatch_async(dispatch_get_main_queue(), { () -> Void in
-
- if URLsToDelete.count != 0 {
- let cleanedHashes = URLsToDelete.map({ (url) -> String in
- return url.lastPathComponent!
- })
-
- NSNotificationCenter.defaultCenter().postNotificationName(KingfisherDidCleanDiskCacheNotification, object: self, userInfo: [KingfisherDiskCacheCleanedHashKey: cleanedHashes])
- }
-
- completionHandler?()
- })
- })
- }
-
- private func travelCachedFiles(onlyForCacheSize onlyForCacheSize: Bool) -> (URLsToDelete: [NSURL], diskCacheSize: UInt, cachedFiles: [NSURL: [NSObject: AnyObject]]) {
-
- let diskCacheURL = NSURL(fileURLWithPath: diskCachePath)
- let resourceKeys = [NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey]
- let expiredDate = NSDate(timeIntervalSinceNow: -self.maxCachePeriodInSecond)
-
- var cachedFiles = [NSURL: [NSObject: AnyObject]]()
- var URLsToDelete = [NSURL]()
- var diskCacheSize: UInt = 0
-
- if let fileEnumerator = self.fileManager.enumeratorAtURL(diskCacheURL, includingPropertiesForKeys: resourceKeys, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles, errorHandler: nil),
- urls = fileEnumerator.allObjects as? [NSURL] {
- for fileURL in urls {
-
- do {
- let resourceValues = try fileURL.resourceValuesForKeys(resourceKeys)
- // If it is a Directory. Continue to next file URL.
- if let isDirectory = resourceValues[NSURLIsDirectoryKey] as? NSNumber {
- if isDirectory.boolValue {
- continue
- }
- }
-
- if !onlyForCacheSize {
- // If this file is expired, add it to URLsToDelete
- if let modificationDate = resourceValues[NSURLContentModificationDateKey] as? NSDate {
- if modificationDate.laterDate(expiredDate) == expiredDate {
- URLsToDelete.append(fileURL)
- continue
- }
- }
- }
-
- if let fileSize = resourceValues[NSURLTotalFileAllocatedSizeKey] as? NSNumber {
- diskCacheSize += fileSize.unsignedLongValue
- if !onlyForCacheSize {
- cachedFiles[fileURL] = resourceValues
- }
- }
- } catch _ {
- }
- }
- }
-
- return (URLsToDelete, diskCacheSize, cachedFiles)
- }
-
- #if !os(OSX) && !os(watchOS)
- /**
- Clean expired disk cache when app in background. This is an async operation.
- In most cases, you should not call this method explicitly.
- It will be called automatically when `UIApplicationDidEnterBackgroundNotification` received.
- */
- @objc public func backgroundCleanExpiredDiskCache() {
-
- func endBackgroundTask(inout task: UIBackgroundTaskIdentifier) {
- UIApplication.sharedApplication().endBackgroundTask(task)
- task = UIBackgroundTaskInvalid
- }
-
- var backgroundTask: UIBackgroundTaskIdentifier!
-
- backgroundTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler { () -> Void in
- endBackgroundTask(&backgroundTask!)
- }
-
- cleanExpiredDiskCacheWithCompletionHander { () -> () in
- endBackgroundTask(&backgroundTask!)
- }
- }
- #endif
- }
- // MARK: - Check cache status
- extension ImageCache {
-
- /**
- * Cache result for checking whether an image is cached for a key.
- */
- public struct CacheCheckResult {
- public let cached: Bool
- public let cacheType: CacheType?
- }
-
- /**
- Check whether an image is cached for a key.
-
- - parameter key: Key for the image.
-
- - returns: The check result.
- */
- public func isImageCachedForKey(key: String) -> CacheCheckResult {
-
- if memoryCache.objectForKey(key) != nil {
- return CacheCheckResult(cached: true, cacheType: .Memory)
- }
-
- let filePath = cachePathForKey(key)
-
- var diskCached = false
- dispatch_sync(ioQueue) { () -> Void in
- diskCached = self.fileManager.fileExistsAtPath(filePath)
- }
- if diskCached {
- return CacheCheckResult(cached: true, cacheType: .Disk)
- }
-
- return CacheCheckResult(cached: false, cacheType: nil)
- }
-
- /**
- Get the hash for the key. This could be used for matching files.
-
- - parameter key: The key which is used for caching.
-
- - returns: Corresponding hash.
- */
- public func hashForKey(key: String) -> String {
- return cacheFileNameForKey(key)
- }
-
- /**
- Calculate the disk size taken by cache.
- It is the total allocated size of the cached files in bytes.
-
- - parameter completionHandler: Called with the calculated size when finishes.
- */
- public func calculateDiskCacheSizeWithCompletionHandler(completionHandler: ((size: UInt) -> ())) {
- dispatch_async(ioQueue, { () -> Void in
- let (_, diskCacheSize, _) = self.travelCachedFiles(onlyForCacheSize: true)
- dispatch_async(dispatch_get_main_queue(), { () -> Void in
- completionHandler(size: diskCacheSize)
- })
- })
- }
-
- /**
- Get the cache path for the key.
- It is useful for projects with UIWebView or anyone that needs access to the local file path.
-
- i.e. `<img src='path_for_key'>`
-
- - Note: This method does not guarantee there is an image already cached in the path.
- You could use `isImageCachedForKey` method to check whether the image is cached under that key.
- */
- public func cachePathForKey(key: String) -> String {
- let fileName = cacheFileNameForKey(key)
- return (diskCachePath as NSString).stringByAppendingPathComponent(fileName)
- }
- }
- // MARK: - Internal Helper
- extension ImageCache {
-
- func diskImageForKey(key: String, scale: CGFloat) -> Image? {
- if let data = diskImageDataForKey(key) {
- return Image.kf_imageWithData(data, scale: scale)
- } else {
- return nil
- }
- }
-
- func diskImageDataForKey(key: String) -> NSData? {
- let filePath = cachePathForKey(key)
- return NSData(contentsOfFile: filePath)
- }
-
- func cacheFileNameForKey(key: String) -> String {
- return key.kf_MD5
- }
- }
- extension Image {
- var kf_imageCost: Int {
- return kf_images == nil ?
- Int(size.height * size.width * kf_scale * kf_scale) :
- Int(size.height * size.width * kf_scale * kf_scale) * kf_images!.count
- }
- }
- extension Dictionary {
- func keysSortedByValue(isOrderedBefore: (Value, Value) -> Bool) -> [Key] {
- return Array(self).sort{ isOrderedBefore($0.1, $1.1) }.map{ $0.0 }
- }
- }
|