ImageCache.swift 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. //
  2. // ImageCache.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 15/4/6.
  6. //
  7. // Copyright (c) 2018 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. #if os(macOS)
  27. import AppKit
  28. #else
  29. import UIKit
  30. #endif
  31. extension Notification.Name {
  32. /// This notification will be sent when the disk cache got cleaned either there are cached files expired or the
  33. /// total size exceeding the max allowed size. The manually invoking of `clearDiskCache` method will not trigger
  34. /// this notification.
  35. ///
  36. /// The `object` of this notification is the `ImageCache` object which sends the notification.
  37. /// A list of removed hashes (files) could be retrieved by accessing the array under
  38. /// `KingfisherDiskCacheCleanedHashKey` key in `userInfo` of the notification object you received.
  39. /// By checking the array, you could know the hash codes of files are removed.
  40. public static let KingfisherDidCleanDiskCache =
  41. Notification.Name("com.onevcat.Kingfisher.KingfisherDidCleanDiskCache")
  42. }
  43. /// Key for array of cleaned hashes in `userInfo` of `KingfisherDidCleanDiskCacheNotification`.
  44. public let KingfisherDiskCacheCleanedHashKey = "com.onevcat.Kingfisher.cleanedHash"
  45. /// Cache type of a cached image.
  46. /// - none: The image is not cached yet when retrieving it.
  47. /// - memory: The image is cached in memory.
  48. /// - disk: The image is cached in disk.
  49. public enum CacheType {
  50. /// The image is not cached yet when retrieving it.
  51. case none
  52. /// The image is cached in memory.
  53. case memory
  54. /// The image is cached in disk.
  55. case disk
  56. /// Whether the cache type represents the image is already cached or not.
  57. public var cached: Bool {
  58. switch self {
  59. case .memory, .disk: return true
  60. case .none: return false
  61. }
  62. }
  63. }
  64. /// Represents the caching operation result.
  65. public struct CacheStoreResult {
  66. /// The cache result for memory cache. Caching an image to memory will never fail.
  67. public let memoryCacheResult: Result<(), Never>
  68. /// The cache result for disk cache. If an error happens during caching operation,
  69. /// you can get it from `.failure` case of this `diskCacheResult`.
  70. public let diskCacheResult: Result<(), KingfisherError>
  71. }
  72. extension Image: CacheCostCalculatable {
  73. /// Cost of an image
  74. public var cacheCost: Int { return kf.cost }
  75. }
  76. extension Data: DataTransformable {
  77. public func toData() throws -> Data {
  78. return self
  79. }
  80. public static func fromData(_ data: Data) throws -> Data {
  81. return data
  82. }
  83. public static let empty = Data()
  84. }
  85. /// Represents the getting image operation from the cache.
  86. ///
  87. /// - disk: The image can be retrieved from disk cache.
  88. /// - memory: The image can be retrieved memory cache.
  89. /// - none: The image does not exist in the cache.
  90. public enum ImageCacheResult {
  91. /// The image can be retrieved from disk cache.
  92. case disk(Image)
  93. /// The image can be retrieved memory cache.
  94. case memory(Image)
  95. /// The image does not exist in the cache.
  96. case none
  97. /// Extracts the image from cache result. It returns the associated `Image` value for
  98. /// `.disk` and `.memory` case. For `.none` case, `nil` is returned.
  99. public var image: Image? {
  100. switch self {
  101. case .disk(let image): return image
  102. case .memory(let image): return image
  103. case .none: return nil
  104. }
  105. }
  106. /// Returns the corresponding `CacheType` value based on the result type of `self`.
  107. public var cacheType: CacheType {
  108. switch self {
  109. case .disk: return .disk
  110. case .memory: return .memory
  111. case .none: return .none
  112. }
  113. }
  114. }
  115. /// Represents a hybrid caching system which is composed by a `MemoryStorage` and a `DiskStorage`. `ImageCache` is a
  116. /// high level abstract for storing an image as well as its data to disk memory and disk, and retrieving them back.
  117. /// While a default image cache object will be used if you prefer the extension methods of Kingfisher, you can create
  118. /// your own cache object and configure its storages as your need. This class also provide an interface for you to set
  119. /// the memory and disk storage config.
  120. open class ImageCache {
  121. /// The `MemoryStorage` object used in this cache. This storage holds loaded images in memory with a reasonable
  122. /// expire duration and a maximum memory usage. To modify the configuration of a storage, just set the storage
  123. /// `config` and its properties.
  124. public let memoryStorage: MemoryStorage<Image>
  125. /// The `DiskStorage` object used in this cache. This storage stores loaded images in disk with a reasonable
  126. /// expire duration and a maximum disk usage. To modify the configuration of a storage, just set the storage
  127. /// `config` and its properties.
  128. public let diskStorage: DiskStorage<Data>
  129. private let ioQueue: DispatchQueue
  130. /// The default `ImageCache` object. Kingfisher will use this cache for its related methods if there is no
  131. /// other cache specified.
  132. public static let `default` = ImageCache(name: "default")
  133. /// Closure that defines the disk cache path from a given path and cacheName.
  134. public typealias DiskCachePathClosure = (URL, String) -> URL
  135. /// Creates an `ImageCache` from a customized `MemoryStorage` and `DiskStorage`.
  136. ///
  137. /// - Parameters:
  138. /// - memoryStorage: The `MemoryStorage` object to use in the image cache.
  139. /// - diskStorage: The `DiskStorage` object to use in the image cache.
  140. /// - name: A name used as a part of the bound IO queue.
  141. public init(memoryStorage: MemoryStorage<Image>, diskStorage: DiskStorage<Data>, name: String = "") {
  142. self.memoryStorage = memoryStorage
  143. self.diskStorage = diskStorage
  144. var ioQueueName = "com.onevcat.Kingfisher.ImageCache.ioQueue"
  145. if !name.isEmpty {
  146. ioQueueName.append(".\(name)")
  147. }
  148. ioQueue = DispatchQueue(label: ioQueueName)
  149. #if !os(macOS) && !os(watchOS)
  150. let notifications: [(Notification.Name, Selector)] = [
  151. (UIApplication.didReceiveMemoryWarningNotification, #selector(clearMemoryCache)),
  152. (UIApplication.willTerminateNotification, #selector(cleanExpiredDiskCache)),
  153. (UIApplication.didEnterBackgroundNotification, #selector(backgroundCleanExpiredDiskCache))
  154. ]
  155. notifications.forEach {
  156. NotificationCenter.default.addObserver(self, selector: $0.1, name: $0.0, object: nil)
  157. }
  158. #endif
  159. }
  160. /// Creates an `ImageCache` with a given `name`. Both `MemoryStorage` and `DiskStorage` will be created
  161. /// with a default config based on the `name`.
  162. ///
  163. /// - Parameter name: The name of cache object. It is used to setup disk cache directories and IO queue.
  164. /// You should not use the same `name` for different caches, otherwise, the disk storage would
  165. /// be conflicting to each other. The `name` should not be an empty string.
  166. public convenience init(name: String) {
  167. try! self.init(name: name, path: nil, diskCachePathClosure: nil)
  168. }
  169. /// Creates an `ImageCache` with a given `name`, cache directory `path`
  170. /// and a closure to modify the cache directory.
  171. ///
  172. /// - Parameters:
  173. /// - name: The name of cache object. It is used to setup disk cache directories and IO queue.
  174. /// You should not use the same `name` for different caches, otherwise, the disk storage would
  175. /// be conflicting to each other.
  176. /// - path: Location of cache path on disk. It will be internally pass to the initializer of `DiskStorage` as the
  177. /// disk cache directory.
  178. /// - diskCachePathClosure: Closure that takes in an optional initial path string and generates
  179. /// the final disk cache path. You could use it to fully customize your cache path.
  180. /// - Throws: An error that happens during image cache creating, such as unable to create a directory at the given
  181. /// path.
  182. public convenience init(
  183. name: String,
  184. path: String?,
  185. diskCachePathClosure: DiskCachePathClosure? = nil) throws
  186. {
  187. if name.isEmpty {
  188. fatalError("[Kingfisher] You should specify a name for the cache. A cache with empty name is not permitted.")
  189. }
  190. let cacheName = "com.onevcat.Kingfisher.ImageCache.\(name)"
  191. let totalMemory = ProcessInfo.processInfo.physicalMemory
  192. let costLimit = totalMemory / 4
  193. let memoryStorage = MemoryStorage<Image>(config:
  194. .init(totalCostLimit: (costLimit > Int.max) ? Int.max : Int(costLimit)))
  195. var diskConfig = DiskStorage<Data>.Config(
  196. name: name,
  197. directory: path.flatMap { URL(string: $0) },
  198. sizeLimit: 0)
  199. if let closure = diskCachePathClosure {
  200. diskConfig.cachePathBlock = diskCachePathClosure
  201. defer { diskConfig.cachePathBlock = nil }
  202. }
  203. let diskStorage = try DiskStorage(config: diskConfig)
  204. self.init(memoryStorage: memoryStorage, diskStorage: diskStorage, name: name)
  205. }
  206. deinit {
  207. NotificationCenter.default.removeObserver(self)
  208. }
  209. // MARK: - Store & Remove
  210. /// Stores an image to the cache.
  211. ///
  212. /// - Parameters:
  213. /// - image: The image to be stored.
  214. /// - original: The original data of the image. This value will be forwarded to the provided `serializer` for
  215. /// further use. By default, Kingfisher uses a `DefaultCacheSerializer` to serialize the image to
  216. /// data for caching in disk, it checks the image format based on `original` data to determine in
  217. /// which image format should be used. For other types of `serializer`, it depends on thier
  218. /// implemetation detail on how to use this original data.
  219. /// - key: The key used for caching the image.
  220. /// - identifier: The identifier of processor being used for caching. If you are using a processor for the
  221. /// image, pass the identifier of processor to this parameter.
  222. /// - serializer: The `CacheSerializer`
  223. /// - toDisk: Whether this image should be cached to disk or not. If `false`, the image is only cached in memory.
  224. /// Otherwise, it is cached in both memory storage and disk storage. Default is `true`.
  225. /// - callbackQueue: The callback queue on which `completionHandler` is invoked. Default is `.untouch`. For case
  226. /// that `toDisk` is `false`, a `.untouch` queue means `callbackQueue` will be invoked from the
  227. /// caller queue of this method. If `toDisk` is `true`, the `completionHandler` will be called
  228. /// from an internal file IO queue. To change this behavior, specify another `CallbackQueue`
  229. /// value.
  230. /// - completionHandler: A closure which is invoked when the cache operation finishes.
  231. open func store(_ image: Image,
  232. original: Data? = nil,
  233. forKey key: String,
  234. processorIdentifier identifier: String = "",
  235. cacheSerializer serializer: CacheSerializer = DefaultCacheSerializer.default,
  236. toDisk: Bool = true,
  237. callbackQueue: CallbackQueue = .untouch,
  238. completionHandler: ((CacheStoreResult) -> Void)? = nil)
  239. {
  240. let computedKey = key.computedKey(with: identifier)
  241. // Memory storage should not throw.
  242. memoryStorage.storeNoThrow(value: image, forKey: computedKey)
  243. if toDisk {
  244. ioQueue.async {
  245. let result: CacheStoreResult
  246. if let data = serializer.data(with: image, original: original) {
  247. do {
  248. try self.diskStorage.store(value: data, forKey: computedKey)
  249. result = CacheStoreResult(memoryCacheResult: .success(()), diskCacheResult: .success(()))
  250. } catch {
  251. let diskError: KingfisherError
  252. if let error = error as? KingfisherError {
  253. diskError = error
  254. } else {
  255. diskError = .cacheError(reason: .cannotConvertToData(object: data, error: error))
  256. }
  257. result = CacheStoreResult(
  258. memoryCacheResult: .success(()),
  259. diskCacheResult: .failure(diskError)
  260. )
  261. }
  262. } else {
  263. let diskError = KingfisherError.cacheError(
  264. reason: .cannotSerializeImage(image: image, original: original, serializer: serializer))
  265. result = CacheStoreResult(memoryCacheResult: .success(()), diskCacheResult: .failure(diskError))
  266. }
  267. if let completionHandler = completionHandler {
  268. callbackQueue.execute { completionHandler(result) }
  269. }
  270. }
  271. } else {
  272. if let completionHandler = completionHandler {
  273. let result = CacheStoreResult(memoryCacheResult: .success(()), diskCacheResult: .success(()))
  274. callbackQueue.execute { completionHandler(result) }
  275. }
  276. }
  277. }
  278. /// Removes the image for the given key from the cache.
  279. ///
  280. /// - Parameters:
  281. /// - key: The key used for caching the image.
  282. /// - identifier: The identifier of processor being used for caching. If you are using a processor for the
  283. /// image, pass the identifier of processor to this parameter.
  284. /// - fromMemory: Whether this image should be removed from memory storage or not.
  285. /// If `false`, the image won't be removed from the memory storage. Default is `true`.
  286. /// - fromDisk: Whether this image should be removed from disk storage or not.
  287. /// If `false`, the image won't be removed from the disk storage. Default is `true`.
  288. /// - callbackQueue: The callback queue on which `completionHandler` is invoked. Default is `.untouch`.
  289. /// - completionHandler: A closure which is invoked when the cache removing operation finishes.
  290. open func removeImage(forKey key: String,
  291. processorIdentifier identifier: String = "",
  292. fromMemory: Bool = true,
  293. fromDisk: Bool = true,
  294. callbackQueue: CallbackQueue = .untouch,
  295. completionHandler: (() -> Void)? = nil)
  296. {
  297. let computedKey = key.computedKey(with: identifier)
  298. if fromMemory {
  299. try? memoryStorage.remove(forKey: computedKey)
  300. }
  301. if fromDisk {
  302. ioQueue.async{
  303. try? self.diskStorage.remove(forKey: computedKey)
  304. if let completionHandler = completionHandler {
  305. callbackQueue.execute { completionHandler() }
  306. }
  307. }
  308. } else {
  309. if let completionHandler = completionHandler {
  310. callbackQueue.execute { completionHandler() }
  311. }
  312. }
  313. }
  314. /// Gets an image for a given key from the cache, either from memory storage or disk storage.
  315. ///
  316. /// - Parameters:
  317. /// - key: The key used for caching the image.
  318. /// - options: The `KingfisherOptionsInfo` options setting used for retrieving the image.
  319. /// - callbackQueue: The callback queue on which `completionHandler` is invoked. Default is `.untouch`.
  320. /// - completionHandler: A closure which is invoked when the image getting operation finishes. If the
  321. /// image retrieving operation finishes without problem, an `ImageCacheResult` value
  322. /// will be sent to this closuer as result. Otherwise, a `KingfisherError` result
  323. /// with detail failing reason will be sent.
  324. open func retrieveImage(forKey key: String,
  325. options: KingfisherOptionsInfo? = nil,
  326. callbackQueue: CallbackQueue = .untouch,
  327. completionHandler: ((Result<ImageCacheResult, KingfisherError>) -> Void)?)
  328. {
  329. // No completion handler. No need to start working and early return.
  330. guard let completionHandler = completionHandler else { return }
  331. let options = options ?? .empty
  332. let imageModifier = options.imageModifier
  333. // Try to check the image from memory cache first.
  334. if let image = retrieveImageInMemoryCache(forKey: key, options: options) {
  335. let image = imageModifier.modify(image)
  336. callbackQueue.execute { completionHandler(.success(.memory(image))) }
  337. } else if options.fromMemoryCacheOrRefresh {
  338. callbackQueue.execute { completionHandler(.success(.none)) }
  339. } else {
  340. // Begin to disk search.
  341. ioQueue.async {
  342. self.retrieveImageInDiskCache(forKey: key, options: options, callbackQueue: callbackQueue) {
  343. result in
  344. // The callback queue is already correct in this closure.
  345. switch result {
  346. case .success(let image):
  347. guard let image = imageModifier.modify(image) else {
  348. // No image found in disk storage.
  349. completionHandler(.success(.none))
  350. return
  351. }
  352. // Cache the disk image to memory.
  353. // We are passing `false` to `toDisk`, the memory cache does not change
  354. // callback queue, we can call `completionHandler` without another dispatch.
  355. self.store(
  356. image,
  357. forKey: key,
  358. processorIdentifier: options.processor.identifier,
  359. cacheSerializer: options.cacheSerializer,
  360. toDisk: false)
  361. {
  362. _ in
  363. completionHandler(.success(.disk(image)))
  364. }
  365. case .failure(let error):
  366. completionHandler(.failure(error))
  367. }
  368. }
  369. }
  370. }
  371. }
  372. /// Gets an image for a given key from the memory storage.
  373. ///
  374. /// - Parameters:
  375. /// - key: The key used for caching the image.
  376. /// - options: The `KingfisherOptionsInfo` options setting used for retrieving the image.
  377. /// - Returns: The image stored in memory cache, if exists and valid. Otherwise, if the image does not exist or
  378. /// has already expired, `nil` is returned.
  379. open func retrieveImageInMemoryCache(
  380. forKey key: String,
  381. options: KingfisherOptionsInfo? = nil) -> Image?
  382. {
  383. let options = options ?? .empty
  384. let computedKey = key.computedKey(with: options.processor.identifier)
  385. do {
  386. return try memoryStorage.value(forKey: computedKey)
  387. } catch {
  388. return nil
  389. }
  390. }
  391. /// Gets an image for a given key from the disk storage.
  392. ///
  393. /// - Parameters:
  394. /// - key: The key used for caching the image.
  395. /// - options: The `KingfisherOptionsInfo` options setting used for retrieving the image.
  396. /// - callbackQueue: The callback queue on which `completionHandler` is invoked. Default is `.untouch`.
  397. /// - completionHandler: A closure which is invoked when the operation finishes.
  398. open func retrieveImageInDiskCache(
  399. forKey key: String,
  400. options: KingfisherOptionsInfo? = nil,
  401. callbackQueue: CallbackQueue = .untouch,
  402. completionHandler: @escaping (Result<Image?, KingfisherError>) -> Void)
  403. {
  404. let options = options ?? .empty
  405. let computedKey = key.computedKey(with: options.processor.identifier)
  406. ioQueue.async {
  407. do {
  408. var image: Image? = nil
  409. if let data = try self.diskStorage.value(forKey: computedKey) {
  410. image = options.cacheSerializer.image(with: data, options: options)
  411. }
  412. callbackQueue.execute { completionHandler(.success(image)) }
  413. } catch {
  414. if let error = error as? KingfisherError {
  415. callbackQueue.execute { completionHandler(.failure(error)) }
  416. } else {
  417. assertionFailure("The internal thrown error should be a `KingfisherError`.")
  418. }
  419. }
  420. }
  421. }
  422. // MARK: - Clear & Clean
  423. /// Clears the memory storage of this cache.
  424. @objc public func clearMemoryCache() {
  425. try? memoryStorage.removeAll()
  426. }
  427. /// Clears the disk storage of this cache. This is an async operation.
  428. ///
  429. /// - Parameter handler: A closure which is invoked when the cache clearing operation finishes.
  430. /// This `handler` will be called from the main queue.
  431. open func clearDiskCache(completion handler: (()->())? = nil) {
  432. ioQueue.async {
  433. do {
  434. try self.diskStorage.removeAll()
  435. } catch _ { }
  436. if let handler = handler {
  437. DispatchQueue.main.async { handler() }
  438. }
  439. }
  440. }
  441. /// Clears the expired images from disk storage. This is an async operation.
  442. @objc func cleanExpiredDiskCache() {
  443. cleanExpiredDiskCache(completion: nil)
  444. }
  445. /// Clears the expired images from disk storage. This is an async operation.
  446. ///
  447. /// - Parameter handler: A closure which is invoked when the cache clearing operation finishes.
  448. /// This `handler` will be called from the main queue.
  449. open func cleanExpiredDiskCache(completion handler: (() -> Void)? = nil) {
  450. ioQueue.async {
  451. do {
  452. var removed: [URL] = []
  453. let removedExpired = try self.diskStorage.removeExpiredValues()
  454. removed.append(contentsOf: removedExpired)
  455. let removedSizeExceeded = try self.diskStorage.removeSizeExceededValues()
  456. removed.append(contentsOf: removedSizeExceeded)
  457. if !removed.isEmpty {
  458. DispatchQueue.main.async {
  459. let cleanedHashes = removed.map { $0.lastPathComponent }
  460. NotificationCenter.default.post(
  461. name: .KingfisherDidCleanDiskCache,
  462. object: self,
  463. userInfo: [KingfisherDiskCacheCleanedHashKey: cleanedHashes])
  464. }
  465. }
  466. if let handler = handler {
  467. DispatchQueue.main.async { handler() }
  468. }
  469. } catch {}
  470. }
  471. }
  472. #if !os(macOS) && !os(watchOS)
  473. /// Clears the expired images from disk storage when app is in background. This is an async operation.
  474. /// In most cases, you should not call this method explicitly.
  475. /// It will be called automatically when `UIApplicationDidEnterBackgroundNotification` received.
  476. @objc public func backgroundCleanExpiredDiskCache() {
  477. // if 'sharedApplication()' is unavailable, then return
  478. guard let sharedApplication = KingfisherClass<UIApplication>.shared else { return }
  479. func endBackgroundTask(_ task: inout UIBackgroundTaskIdentifier) {
  480. sharedApplication.endBackgroundTask(task)
  481. task = UIBackgroundTaskIdentifier.invalid
  482. }
  483. var backgroundTask: UIBackgroundTaskIdentifier!
  484. backgroundTask = sharedApplication.beginBackgroundTask {
  485. endBackgroundTask(&backgroundTask!)
  486. }
  487. cleanExpiredDiskCache {
  488. endBackgroundTask(&backgroundTask!)
  489. }
  490. }
  491. #endif
  492. /// Returns the cache type for a given `key` and `identifier` combination.
  493. /// This method is used for checking whether an image is cached in current cache.
  494. /// It also provides information on which kind of cache can it be found in the return value.
  495. ///
  496. /// - Parameters:
  497. /// - key: The key used for caching the image.
  498. /// - identifier: Processor identifier which used for this image. Default is the `identifier` of
  499. /// `DefaultImageProcessor.default`.
  500. /// - Returns: A `CacheType` instance which indicates the cache status.
  501. /// `.none` means the image is not in cache or it is already expired.
  502. open func imageCachedType(
  503. forKey key: String,
  504. processorIdentifier identifier: String = DefaultImageProcessor.default.identifier) -> CacheType
  505. {
  506. let computedKey = key.computedKey(with: identifier)
  507. if memoryStorage.isCached(forKey: computedKey) { return .memory }
  508. if diskStorage.isCached(forKey: computedKey) { return .disk }
  509. return .none
  510. }
  511. /// Returns whether the file exists in cache for a given `key` and `identifier` combination.
  512. ///
  513. /// - Parameters:
  514. /// - key: The key used for caching the image.
  515. /// - identifier: Processor identifier which used for this image. Default is the `identifier` of
  516. /// `DefaultImageProcessor.default`.
  517. /// - Returns: A `Bool` which indicates whether a cache could match the given `key` and `identifier` combination.
  518. ///
  519. /// - Note:
  520. /// The return value does not contain information about from which kind of storage the cache matches.
  521. /// To get the information about cache type according `CacheType`,
  522. /// use `imageCachedType(forKey:processorIdentifier:)` instead.
  523. public func isCached(
  524. forKey key: String,
  525. processorIdentifier identifier: String = DefaultImageProcessor.default.identifier) -> Bool
  526. {
  527. return imageCachedType(forKey: key, processorIdentifier: identifier).cached
  528. }
  529. /// Gets the hash used as cache file name for the key.
  530. ///
  531. /// - Parameters:
  532. /// - key: The key used for caching the image.
  533. /// - identifier: Processor identifier which used for this image. Default is the `identifier` of
  534. /// `DefaultImageProcessor.default`.
  535. /// - Returns: The hash which is used as the cache file name.
  536. ///
  537. /// - Note:
  538. /// By default, for a given combination of `key` and `identifier`, `ImageCache` will use the value
  539. /// returned by this method as the cache file name. You can use this value to check and match cache file
  540. /// if you need.
  541. open func hash(
  542. forKey key: String,
  543. processorIdentifier identifier: String = DefaultImageProcessor.default.identifier) -> String
  544. {
  545. let computedKey = key.computedKey(with: identifier)
  546. return diskStorage.cacheFileName(forKey: computedKey)
  547. }
  548. /// Calculates the disk size taken by cache.
  549. /// It is the total allocated size of the cached files on disk in bytes.
  550. ///
  551. /// - Parameter handler: Called with the size calculating finishes. This closure is invoked from the main queue.
  552. open func calculateDiskCacheSize(completion handler: @escaping ((Result<UInt, KingfisherError>) -> Void)) {
  553. ioQueue.async {
  554. do {
  555. let size = try self.diskStorage.totalSize()
  556. DispatchQueue.main.async {
  557. handler(.success(size))
  558. }
  559. } catch {
  560. if let error = error as? KingfisherError {
  561. handler(.failure(error))
  562. } else {
  563. assertionFailure("The internal thrown error should be a `KingfisherError`.")
  564. }
  565. }
  566. }
  567. }
  568. /// Gets the cache path for the key.
  569. /// It is useful for projects with web view or anyone that needs access to the local file path.
  570. ///
  571. /// i.e. Replacing the `<img src='path_for_key'>` tag in your HTML.
  572. ///
  573. /// - Parameters:
  574. /// - key: The key used for caching the image.
  575. /// - identifier: Processor identifier which used for this image. Default is the `identifier` of
  576. /// `DefaultImageProcessor.default`.
  577. /// - Returns: The disk path of cached image under the given `key` and `identifier`.
  578. ///
  579. /// - Note:
  580. /// This method does not guarantee there is an image already cached in the returned path. It just gives your
  581. /// the path that the image should be, if it exists in disk storage.
  582. ///
  583. /// You could use `isImageCached(forKey:)` method to check whether the image is cached under that key in disk.
  584. open func cachePath(
  585. forKey key: String,
  586. processorIdentifier identifier: String = DefaultImageProcessor.default.identifier) -> String
  587. {
  588. let computedKey = key.computedKey(with: identifier)
  589. return diskStorage.cacheFileURL(forKey: computedKey).path
  590. }
  591. }
  592. extension Dictionary {
  593. func keysSortedByValue(_ isOrderedBefore: (Value, Value) -> Bool) -> [Key] {
  594. return Array(self).sorted{ isOrderedBefore($0.1, $1.1) }.map{ $0.0 }
  595. }
  596. }
  597. #if !os(macOS) && !os(watchOS)
  598. // MARK: - For App Extensions
  599. extension UIApplication: KingfisherClassCompatible { }
  600. extension KingfisherClass where Base: UIApplication {
  601. public static var shared: UIApplication? {
  602. let selector = NSSelectorFromString("sharedApplication")
  603. guard Base.responds(to: selector) else { return nil }
  604. return Base.perform(selector).takeUnretainedValue() as? UIApplication
  605. }
  606. }
  607. #endif
  608. extension String {
  609. func computedKey(with identifier: String) -> String {
  610. if identifier.isEmpty {
  611. return self
  612. } else {
  613. return appending("@\(identifier)")
  614. }
  615. }
  616. }