ImageCache.swift 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. //
  2. // ImageCache.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 15/4/6.
  6. //
  7. // Copyright (c) 2019 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 KFCrossPlatformImage: CacheCostCalculable {
  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(KFCrossPlatformImage)
  93. /// The image can be retrieved memory cache.
  94. case memory(KFCrossPlatformImage)
  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: KFCrossPlatformImage? {
  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.Backend` and a `DiskStorage.Backend`.
  116. /// `ImageCache` is a high level abstract for storing an image as well as its data to memory and disk, and
  117. /// retrieving them back.
  118. ///
  119. /// While a default image cache object will be used if you prefer the extension methods of Kingfisher, you can create
  120. /// your own cache object and configure its storages as your need. This class also provide an interface for you to set
  121. /// the memory and disk storage config.
  122. open class ImageCache {
  123. // MARK: Singleton
  124. /// The default `ImageCache` object. Kingfisher will use this cache for its related methods if there is no
  125. /// other cache specified. The `name` of this default cache is "default", and you should not use this name
  126. /// for any of your customize cache.
  127. public static let `default` = ImageCache(name: "default")
  128. // MARK: Public Properties
  129. /// The `MemoryStorage.Backend` object used in this cache. This storage holds loaded images in memory with a
  130. /// reasonable expire duration and a maximum memory usage. To modify the configuration of a storage, just set
  131. /// the storage `config` and its properties.
  132. public let memoryStorage: MemoryStorage.Backend<KFCrossPlatformImage>
  133. /// The `DiskStorage.Backend` object used in this cache. This storage stores loaded images in disk with a
  134. /// reasonable expire duration and a maximum disk usage. To modify the configuration of a storage, just set
  135. /// the storage `config` and its properties.
  136. public let diskStorage: DiskStorage.Backend<Data>
  137. private let ioQueue: DispatchQueue
  138. /// Closure that defines the disk cache path from a given path and cacheName.
  139. public typealias DiskCachePathClosure = (URL, String) -> URL
  140. // MARK: Initializers
  141. /// Creates an `ImageCache` from a customized `MemoryStorage` and `DiskStorage`.
  142. ///
  143. /// - Parameters:
  144. /// - memoryStorage: The `MemoryStorage.Backend` object to use in the image cache.
  145. /// - diskStorage: The `DiskStorage.Backend` object to use in the image cache.
  146. public init(
  147. memoryStorage: MemoryStorage.Backend<KFCrossPlatformImage>,
  148. diskStorage: DiskStorage.Backend<Data>)
  149. {
  150. self.memoryStorage = memoryStorage
  151. self.diskStorage = diskStorage
  152. let ioQueueName = "com.onevcat.Kingfisher.ImageCache.ioQueue.\(UUID().uuidString)"
  153. ioQueue = DispatchQueue(label: ioQueueName)
  154. let notifications: [(Notification.Name, Selector)]
  155. #if !os(macOS) && !os(watchOS)
  156. notifications = [
  157. (UIApplication.didReceiveMemoryWarningNotification, #selector(clearMemoryCache)),
  158. (UIApplication.willTerminateNotification, #selector(cleanExpiredDiskCache)),
  159. (UIApplication.didEnterBackgroundNotification, #selector(backgroundCleanExpiredDiskCache))
  160. ]
  161. #elseif os(macOS)
  162. notifications = [
  163. (NSApplication.willResignActiveNotification, #selector(cleanExpiredDiskCache)),
  164. ]
  165. #else
  166. notifications = []
  167. #endif
  168. notifications.forEach {
  169. NotificationCenter.default.addObserver(self, selector: $0.1, name: $0.0, object: nil)
  170. }
  171. }
  172. /// Creates an `ImageCache` with a given `name`. Both `MemoryStorage` and `DiskStorage` will be created
  173. /// with a default config based on the `name`.
  174. ///
  175. /// - Parameter name: The name of cache object. It is used to setup disk cache directories and IO queue.
  176. /// You should not use the same `name` for different caches, otherwise, the disk storage would
  177. /// be conflicting to each other. The `name` should not be an empty string.
  178. public convenience init(name: String) {
  179. self.init(noThrowName: name, cacheDirectoryURL: nil, diskCachePathClosure: nil)
  180. }
  181. /// Creates an `ImageCache` with a given `name`, cache directory `path`
  182. /// and a closure to modify the cache directory.
  183. ///
  184. /// - Parameters:
  185. /// - name: The name of cache object. It is used to setup disk cache directories and IO queue.
  186. /// You should not use the same `name` for different caches, otherwise, the disk storage would
  187. /// be conflicting to each other.
  188. /// - cacheDirectoryURL: Location of cache directory URL on disk. It will be internally pass to the
  189. /// initializer of `DiskStorage` as the disk cache directory. If `nil`, the cache
  190. /// directory under user domain mask will be used.
  191. /// - diskCachePathClosure: Closure that takes in an optional initial path string and generates
  192. /// the final disk cache path. You could use it to fully customize your cache path.
  193. /// - Throws: An error that happens during image cache creating, such as unable to create a directory at the given
  194. /// path.
  195. public convenience init(
  196. name: String,
  197. cacheDirectoryURL: URL?,
  198. diskCachePathClosure: DiskCachePathClosure? = nil
  199. ) throws
  200. {
  201. if name.isEmpty {
  202. fatalError("[Kingfisher] You should specify a name for the cache. A cache with empty name is not permitted.")
  203. }
  204. let memoryStorage = ImageCache.createMemoryStorage()
  205. let config = ImageCache.createConfig(
  206. name: name, cacheDirectoryURL: cacheDirectoryURL, diskCachePathClosure: diskCachePathClosure
  207. )
  208. let diskStorage = try DiskStorage.Backend<Data>(config: config)
  209. self.init(memoryStorage: memoryStorage, diskStorage: diskStorage)
  210. }
  211. convenience init(
  212. noThrowName name: String,
  213. cacheDirectoryURL: URL?,
  214. diskCachePathClosure: DiskCachePathClosure?
  215. )
  216. {
  217. if name.isEmpty {
  218. fatalError("[Kingfisher] You should specify a name for the cache. A cache with empty name is not permitted.")
  219. }
  220. let memoryStorage = ImageCache.createMemoryStorage()
  221. let config = ImageCache.createConfig(
  222. name: name, cacheDirectoryURL: cacheDirectoryURL, diskCachePathClosure: diskCachePathClosure
  223. )
  224. let diskStorage = DiskStorage.Backend<Data>(noThrowConfig: config, creatingDirectory: true)
  225. self.init(memoryStorage: memoryStorage, diskStorage: diskStorage)
  226. }
  227. private static func createMemoryStorage() -> MemoryStorage.Backend<KFCrossPlatformImage> {
  228. let totalMemory = ProcessInfo.processInfo.physicalMemory
  229. let costLimit = totalMemory / 4
  230. let memoryStorage = MemoryStorage.Backend<KFCrossPlatformImage>(config:
  231. .init(totalCostLimit: (costLimit > Int.max) ? Int.max : Int(costLimit)))
  232. return memoryStorage
  233. }
  234. private static func createConfig(
  235. name: String,
  236. cacheDirectoryURL: URL?,
  237. diskCachePathClosure: DiskCachePathClosure? = nil
  238. ) -> DiskStorage.Config
  239. {
  240. var diskConfig = DiskStorage.Config(
  241. name: name,
  242. sizeLimit: 0,
  243. directory: cacheDirectoryURL
  244. )
  245. if let closure = diskCachePathClosure {
  246. diskConfig.cachePathBlock = closure
  247. }
  248. return diskConfig
  249. }
  250. deinit {
  251. NotificationCenter.default.removeObserver(self)
  252. }
  253. // MARK: Storing Images
  254. open func store(_ image: KFCrossPlatformImage,
  255. original: Data? = nil,
  256. forKey key: String,
  257. options: KingfisherParsedOptionsInfo,
  258. toDisk: Bool = true,
  259. completionHandler: ((CacheStoreResult) -> Void)? = nil)
  260. {
  261. let identifier = options.processor.identifier
  262. let callbackQueue = options.callbackQueue
  263. let computedKey = key.computedKey(with: identifier)
  264. // Memory storage should not throw.
  265. memoryStorage.storeNoThrow(value: image, forKey: computedKey, expiration: options.memoryCacheExpiration)
  266. guard toDisk else {
  267. if let completionHandler = completionHandler {
  268. let result = CacheStoreResult(memoryCacheResult: .success(()), diskCacheResult: .success(()))
  269. callbackQueue.execute { completionHandler(result) }
  270. }
  271. return
  272. }
  273. ioQueue.async {
  274. let serializer = options.cacheSerializer
  275. if let data = serializer.data(with: image, original: original) {
  276. self.syncStoreToDisk(
  277. data,
  278. forKey: key,
  279. processorIdentifier: identifier,
  280. callbackQueue: callbackQueue,
  281. expiration: options.diskCacheExpiration,
  282. writeOptions: options.diskStoreWriteOptions,
  283. completionHandler: completionHandler)
  284. } else {
  285. guard let completionHandler = completionHandler else { return }
  286. let diskError = KingfisherError.cacheError(
  287. reason: .cannotSerializeImage(image: image, original: original, serializer: serializer))
  288. let result = CacheStoreResult(
  289. memoryCacheResult: .success(()),
  290. diskCacheResult: .failure(diskError))
  291. callbackQueue.execute { completionHandler(result) }
  292. }
  293. }
  294. }
  295. /// Stores an image to the cache.
  296. ///
  297. /// - Parameters:
  298. /// - image: The image to be stored.
  299. /// - original: The original data of the image. This value will be forwarded to the provided `serializer` for
  300. /// further use. By default, Kingfisher uses a `DefaultCacheSerializer` to serialize the image to
  301. /// data for caching in disk, it checks the image format based on `original` data to determine in
  302. /// which image format should be used. For other types of `serializer`, it depends on their
  303. /// implementation detail on how to use this original data.
  304. /// - key: The key used for caching the image.
  305. /// - identifier: The identifier of processor being used for caching. If you are using a processor for the
  306. /// image, pass the identifier of processor to this parameter.
  307. /// - serializer: The `CacheSerializer`
  308. /// - toDisk: Whether this image should be cached to disk or not. If `false`, the image is only cached in memory.
  309. /// Otherwise, it is cached in both memory storage and disk storage. Default is `true`.
  310. /// - callbackQueue: The callback queue on which `completionHandler` is invoked. Default is `.untouch`. For case
  311. /// that `toDisk` is `false`, a `.untouch` queue means `callbackQueue` will be invoked from the
  312. /// caller queue of this method. If `toDisk` is `true`, the `completionHandler` will be called
  313. /// from an internal file IO queue. To change this behavior, specify another `CallbackQueue`
  314. /// value.
  315. /// - completionHandler: A closure which is invoked when the cache operation finishes.
  316. open func store(_ image: KFCrossPlatformImage,
  317. original: Data? = nil,
  318. forKey key: String,
  319. processorIdentifier identifier: String = "",
  320. cacheSerializer serializer: CacheSerializer = DefaultCacheSerializer.default,
  321. toDisk: Bool = true,
  322. callbackQueue: CallbackQueue = .untouch,
  323. completionHandler: ((CacheStoreResult) -> Void)? = nil)
  324. {
  325. struct TempProcessor: ImageProcessor {
  326. let identifier: String
  327. func process(item: ImageProcessItem, options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage? {
  328. return nil
  329. }
  330. }
  331. let options = KingfisherParsedOptionsInfo([
  332. .processor(TempProcessor(identifier: identifier)),
  333. .cacheSerializer(serializer),
  334. .callbackQueue(callbackQueue)
  335. ])
  336. store(image, original: original, forKey: key, options: options,
  337. toDisk: toDisk, completionHandler: completionHandler)
  338. }
  339. open func storeToDisk(
  340. _ data: Data,
  341. forKey key: String,
  342. processorIdentifier identifier: String = "",
  343. expiration: StorageExpiration? = nil,
  344. callbackQueue: CallbackQueue = .untouch,
  345. completionHandler: ((CacheStoreResult) -> Void)? = nil)
  346. {
  347. ioQueue.async {
  348. self.syncStoreToDisk(
  349. data,
  350. forKey: key,
  351. processorIdentifier: identifier,
  352. callbackQueue: callbackQueue,
  353. expiration: expiration,
  354. completionHandler: completionHandler)
  355. }
  356. }
  357. private func syncStoreToDisk(
  358. _ data: Data,
  359. forKey key: String,
  360. processorIdentifier identifier: String = "",
  361. callbackQueue: CallbackQueue = .untouch,
  362. expiration: StorageExpiration? = nil,
  363. writeOptions: Data.WritingOptions = [],
  364. completionHandler: ((CacheStoreResult) -> Void)? = nil)
  365. {
  366. let computedKey = key.computedKey(with: identifier)
  367. let result: CacheStoreResult
  368. do {
  369. try self.diskStorage.store(value: data, forKey: computedKey, expiration: expiration, writeOptions: writeOptions)
  370. result = CacheStoreResult(memoryCacheResult: .success(()), diskCacheResult: .success(()))
  371. } catch {
  372. let diskError: KingfisherError
  373. if let error = error as? KingfisherError {
  374. diskError = error
  375. } else {
  376. diskError = .cacheError(reason: .cannotConvertToData(object: data, error: error))
  377. }
  378. result = CacheStoreResult(
  379. memoryCacheResult: .success(()),
  380. diskCacheResult: .failure(diskError)
  381. )
  382. }
  383. if let completionHandler = completionHandler {
  384. callbackQueue.execute { completionHandler(result) }
  385. }
  386. }
  387. // MARK: Removing Images
  388. /// Removes the image for the given key from the cache.
  389. ///
  390. /// - Parameters:
  391. /// - key: The key used for caching the image.
  392. /// - identifier: The identifier of processor being used for caching. If you are using a processor for the
  393. /// image, pass the identifier of processor to this parameter.
  394. /// - fromMemory: Whether this image should be removed from memory storage or not.
  395. /// If `false`, the image won't be removed from the memory storage. Default is `true`.
  396. /// - fromDisk: Whether this image should be removed from disk storage or not.
  397. /// If `false`, the image won't be removed from the disk storage. Default is `true`.
  398. /// - callbackQueue: The callback queue on which `completionHandler` is invoked. Default is `.untouch`.
  399. /// - completionHandler: A closure which is invoked when the cache removing operation finishes.
  400. open func removeImage(forKey key: String,
  401. processorIdentifier identifier: String = "",
  402. fromMemory: Bool = true,
  403. fromDisk: Bool = true,
  404. callbackQueue: CallbackQueue = .untouch,
  405. completionHandler: (() -> Void)? = nil)
  406. {
  407. removeImage(
  408. forKey: key,
  409. processorIdentifier: identifier,
  410. fromMemory: fromMemory,
  411. fromDisk: fromDisk,
  412. callbackQueue: callbackQueue,
  413. completionHandler: { _ in completionHandler?() } // This is a version which ignores error.
  414. )
  415. }
  416. func removeImage(forKey key: String,
  417. processorIdentifier identifier: String = "",
  418. fromMemory: Bool = true,
  419. fromDisk: Bool = true,
  420. callbackQueue: CallbackQueue = .untouch,
  421. completionHandler: ((Error?) -> Void)? = nil)
  422. {
  423. let computedKey = key.computedKey(with: identifier)
  424. if fromMemory {
  425. memoryStorage.remove(forKey: computedKey)
  426. }
  427. func callHandler(_ error: Error?) {
  428. if let completionHandler = completionHandler {
  429. callbackQueue.execute { completionHandler(error) }
  430. }
  431. }
  432. if fromDisk {
  433. ioQueue.async{
  434. do {
  435. try self.diskStorage.remove(forKey: computedKey)
  436. callHandler(nil)
  437. } catch {
  438. callHandler(error)
  439. }
  440. }
  441. } else {
  442. callHandler(nil)
  443. }
  444. }
  445. // MARK: Getting Images
  446. /// Gets an image for a given key from the cache, either from memory storage or disk storage.
  447. ///
  448. /// - Parameters:
  449. /// - key: The key used for caching the image.
  450. /// - options: The `KingfisherParsedOptionsInfo` options setting used for retrieving the image.
  451. /// - callbackQueue: The callback queue on which `completionHandler` is invoked. Default is `.mainCurrentOrAsync`.
  452. /// - completionHandler: A closure which is invoked when the image getting operation finishes. If the
  453. /// image retrieving operation finishes without problem, an `ImageCacheResult` value
  454. /// will be sent to this closure as result. Otherwise, a `KingfisherError` result
  455. /// with detail failing reason will be sent.
  456. open func retrieveImage(
  457. forKey key: String,
  458. options: KingfisherParsedOptionsInfo,
  459. callbackQueue: CallbackQueue = .mainCurrentOrAsync,
  460. completionHandler: ((Result<ImageCacheResult, KingfisherError>) -> Void)?)
  461. {
  462. // No completion handler. No need to start working and early return.
  463. guard let completionHandler = completionHandler else { return }
  464. // Try to check the image from memory cache first.
  465. if let image = retrieveImageInMemoryCache(forKey: key, options: options) {
  466. callbackQueue.execute { completionHandler(.success(.memory(image))) }
  467. } else if options.fromMemoryCacheOrRefresh {
  468. callbackQueue.execute { completionHandler(.success(.none)) }
  469. } else {
  470. // Begin to disk search.
  471. self.retrieveImageInDiskCache(forKey: key, options: options, callbackQueue: callbackQueue) {
  472. result in
  473. switch result {
  474. case .success(let image):
  475. guard let image = image else {
  476. // No image found in disk storage.
  477. callbackQueue.execute { completionHandler(.success(.none)) }
  478. return
  479. }
  480. // Cache the disk image to memory.
  481. // We are passing `false` to `toDisk`, the memory cache does not change
  482. // callback queue, we can call `completionHandler` without another dispatch.
  483. var cacheOptions = options
  484. cacheOptions.callbackQueue = .untouch
  485. self.store(
  486. image,
  487. forKey: key,
  488. options: cacheOptions,
  489. toDisk: false)
  490. {
  491. _ in
  492. callbackQueue.execute { completionHandler(.success(.disk(image))) }
  493. }
  494. case .failure(let error):
  495. callbackQueue.execute { completionHandler(.failure(error)) }
  496. }
  497. }
  498. }
  499. }
  500. /// Gets an image for a given key from the cache, either from memory storage or disk storage.
  501. ///
  502. /// - Parameters:
  503. /// - key: The key used for caching the image.
  504. /// - options: The `KingfisherOptionsInfo` options setting used for retrieving the image.
  505. /// - callbackQueue: The callback queue on which `completionHandler` is invoked. Default is `.mainCurrentOrAsync`.
  506. /// - completionHandler: A closure which is invoked when the image getting operation finishes. If the
  507. /// image retrieving operation finishes without problem, an `ImageCacheResult` value
  508. /// will be sent to this closure as result. Otherwise, a `KingfisherError` result
  509. /// with detail failing reason will be sent.
  510. ///
  511. /// Note: This method is marked as `open` for only compatible purpose. Do not overide this method. Instead, override
  512. /// the version receives `KingfisherParsedOptionsInfo` instead.
  513. open func retrieveImage(forKey key: String,
  514. options: KingfisherOptionsInfo? = nil,
  515. callbackQueue: CallbackQueue = .mainCurrentOrAsync,
  516. completionHandler: ((Result<ImageCacheResult, KingfisherError>) -> Void)?)
  517. {
  518. retrieveImage(
  519. forKey: key,
  520. options: KingfisherParsedOptionsInfo(options),
  521. callbackQueue: callbackQueue,
  522. completionHandler: completionHandler)
  523. }
  524. /// Gets an image for a given key from the memory storage.
  525. ///
  526. /// - Parameters:
  527. /// - key: The key used for caching the image.
  528. /// - options: The `KingfisherParsedOptionsInfo` options setting used for retrieving the image.
  529. /// - Returns: The image stored in memory cache, if exists and valid. Otherwise, if the image does not exist or
  530. /// has already expired, `nil` is returned.
  531. open func retrieveImageInMemoryCache(
  532. forKey key: String,
  533. options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage?
  534. {
  535. let computedKey = key.computedKey(with: options.processor.identifier)
  536. return memoryStorage.value(forKey: computedKey, extendingExpiration: options.memoryCacheAccessExtendingExpiration)
  537. }
  538. /// Gets an image for a given key from the memory storage.
  539. ///
  540. /// - Parameters:
  541. /// - key: The key used for caching the image.
  542. /// - options: The `KingfisherOptionsInfo` options setting used for retrieving the image.
  543. /// - Returns: The image stored in memory cache, if exists and valid. Otherwise, if the image does not exist or
  544. /// has already expired, `nil` is returned.
  545. ///
  546. /// Note: This method is marked as `open` for only compatible purpose. Do not overide this method. Instead, override
  547. /// the version receives `KingfisherParsedOptionsInfo` instead.
  548. open func retrieveImageInMemoryCache(
  549. forKey key: String,
  550. options: KingfisherOptionsInfo? = nil) -> KFCrossPlatformImage?
  551. {
  552. return retrieveImageInMemoryCache(forKey: key, options: KingfisherParsedOptionsInfo(options))
  553. }
  554. func retrieveImageInDiskCache(
  555. forKey key: String,
  556. options: KingfisherParsedOptionsInfo,
  557. callbackQueue: CallbackQueue = .untouch,
  558. completionHandler: @escaping (Result<KFCrossPlatformImage?, KingfisherError>) -> Void)
  559. {
  560. let computedKey = key.computedKey(with: options.processor.identifier)
  561. let loadingQueue: CallbackQueue = options.loadDiskFileSynchronously ? .untouch : .dispatch(ioQueue)
  562. loadingQueue.execute {
  563. do {
  564. var image: KFCrossPlatformImage? = nil
  565. if let data = try self.diskStorage.value(forKey: computedKey, extendingExpiration: options.diskCacheAccessExtendingExpiration) {
  566. image = options.cacheSerializer.image(with: data, options: options)
  567. }
  568. if options.backgroundDecode {
  569. image = image?.kf.decoded(scale: options.scaleFactor)
  570. }
  571. callbackQueue.execute { completionHandler(.success(image)) }
  572. } catch let error as KingfisherError {
  573. callbackQueue.execute { completionHandler(.failure(error)) }
  574. } catch {
  575. assertionFailure("The internal thrown error should be a `KingfisherError`.")
  576. }
  577. }
  578. }
  579. /// Gets an image for a given key from the disk storage.
  580. ///
  581. /// - Parameters:
  582. /// - key: The key used for caching the image.
  583. /// - options: The `KingfisherOptionsInfo` options setting used for retrieving the image.
  584. /// - callbackQueue: The callback queue on which `completionHandler` is invoked. Default is `.untouch`.
  585. /// - completionHandler: A closure which is invoked when the operation finishes.
  586. open func retrieveImageInDiskCache(
  587. forKey key: String,
  588. options: KingfisherOptionsInfo? = nil,
  589. callbackQueue: CallbackQueue = .untouch,
  590. completionHandler: @escaping (Result<KFCrossPlatformImage?, KingfisherError>) -> Void)
  591. {
  592. retrieveImageInDiskCache(
  593. forKey: key,
  594. options: KingfisherParsedOptionsInfo(options),
  595. callbackQueue: callbackQueue,
  596. completionHandler: completionHandler)
  597. }
  598. // MARK: Cleaning
  599. /// Clears the memory & disk storage of this cache. This is an async operation.
  600. ///
  601. /// - Parameter handler: A closure which is invoked when the cache clearing operation finishes.
  602. /// This `handler` will be called from the main queue.
  603. public func clearCache(completion handler: (() -> Void)? = nil) {
  604. clearMemoryCache()
  605. clearDiskCache(completion: handler)
  606. }
  607. /// Clears the memory storage of this cache.
  608. @objc public func clearMemoryCache() {
  609. memoryStorage.removeAll()
  610. }
  611. /// Clears the disk storage of this cache. This is an async operation.
  612. ///
  613. /// - Parameter handler: A closure which is invoked when the cache clearing operation finishes.
  614. /// This `handler` will be called from the main queue.
  615. open func clearDiskCache(completion handler: (() -> Void)? = nil) {
  616. ioQueue.async {
  617. do {
  618. try self.diskStorage.removeAll()
  619. } catch _ { }
  620. if let handler = handler {
  621. DispatchQueue.main.async { handler() }
  622. }
  623. }
  624. }
  625. /// Clears the expired images from memory & disk storage. This is an async operation.
  626. open func cleanExpiredCache(completion handler: (() -> Void)? = nil) {
  627. cleanExpiredMemoryCache()
  628. cleanExpiredDiskCache(completion: handler)
  629. }
  630. /// Clears the expired images from disk storage.
  631. open func cleanExpiredMemoryCache() {
  632. memoryStorage.removeExpired()
  633. }
  634. /// Clears the expired images from disk storage. This is an async operation.
  635. @objc func cleanExpiredDiskCache() {
  636. cleanExpiredDiskCache(completion: nil)
  637. }
  638. /// Clears the expired images from disk storage. This is an async operation.
  639. ///
  640. /// - Parameter handler: A closure which is invoked when the cache clearing operation finishes.
  641. /// This `handler` will be called from the main queue.
  642. open func cleanExpiredDiskCache(completion handler: (() -> Void)? = nil) {
  643. ioQueue.async {
  644. do {
  645. var removed: [URL] = []
  646. let removedExpired = try self.diskStorage.removeExpiredValues()
  647. removed.append(contentsOf: removedExpired)
  648. let removedSizeExceeded = try self.diskStorage.removeSizeExceededValues()
  649. removed.append(contentsOf: removedSizeExceeded)
  650. if !removed.isEmpty {
  651. DispatchQueue.main.async {
  652. let cleanedHashes = removed.map { $0.lastPathComponent }
  653. NotificationCenter.default.post(
  654. name: .KingfisherDidCleanDiskCache,
  655. object: self,
  656. userInfo: [KingfisherDiskCacheCleanedHashKey: cleanedHashes])
  657. }
  658. }
  659. if let handler = handler {
  660. DispatchQueue.main.async { handler() }
  661. }
  662. } catch {}
  663. }
  664. }
  665. #if !os(macOS) && !os(watchOS)
  666. /// Clears the expired images from disk storage when app is in background. This is an async operation.
  667. /// In most cases, you should not call this method explicitly.
  668. /// It will be called automatically when `UIApplicationDidEnterBackgroundNotification` received.
  669. @objc public func backgroundCleanExpiredDiskCache() {
  670. // if 'sharedApplication()' is unavailable, then return
  671. guard let sharedApplication = KingfisherWrapper<UIApplication>.shared else { return }
  672. func endBackgroundTask(_ task: inout UIBackgroundTaskIdentifier) {
  673. sharedApplication.endBackgroundTask(task)
  674. task = UIBackgroundTaskIdentifier.invalid
  675. }
  676. var backgroundTask: UIBackgroundTaskIdentifier!
  677. backgroundTask = sharedApplication.beginBackgroundTask {
  678. endBackgroundTask(&backgroundTask!)
  679. }
  680. cleanExpiredDiskCache {
  681. endBackgroundTask(&backgroundTask!)
  682. }
  683. }
  684. #endif
  685. // MARK: Image Cache State
  686. /// Returns the cache type for a given `key` and `identifier` combination.
  687. /// This method is used for checking whether an image is cached in current cache.
  688. /// It also provides information on which kind of cache can it be found in the return value.
  689. ///
  690. /// - Parameters:
  691. /// - key: The key used for caching the image.
  692. /// - identifier: Processor identifier which used for this image. Default is the `identifier` of
  693. /// `DefaultImageProcessor.default`.
  694. /// - Returns: A `CacheType` instance which indicates the cache status.
  695. /// `.none` means the image is not in cache or it is already expired.
  696. open func imageCachedType(
  697. forKey key: String,
  698. processorIdentifier identifier: String = DefaultImageProcessor.default.identifier) -> CacheType
  699. {
  700. let computedKey = key.computedKey(with: identifier)
  701. if memoryStorage.isCached(forKey: computedKey) { return .memory }
  702. if diskStorage.isCached(forKey: computedKey) { return .disk }
  703. return .none
  704. }
  705. /// Returns whether the file exists in cache for a given `key` and `identifier` combination.
  706. ///
  707. /// - Parameters:
  708. /// - key: The key used for caching the image.
  709. /// - identifier: Processor identifier which used for this image. Default is the `identifier` of
  710. /// `DefaultImageProcessor.default`.
  711. /// - Returns: A `Bool` which indicates whether a cache could match the given `key` and `identifier` combination.
  712. ///
  713. /// - Note:
  714. /// The return value does not contain information about from which kind of storage the cache matches.
  715. /// To get the information about cache type according `CacheType`,
  716. /// use `imageCachedType(forKey:processorIdentifier:)` instead.
  717. public func isCached(
  718. forKey key: String,
  719. processorIdentifier identifier: String = DefaultImageProcessor.default.identifier) -> Bool
  720. {
  721. return imageCachedType(forKey: key, processorIdentifier: identifier).cached
  722. }
  723. /// Gets the hash used as cache file name for the key.
  724. ///
  725. /// - Parameters:
  726. /// - key: The key used for caching the image.
  727. /// - identifier: Processor identifier which used for this image. Default is the `identifier` of
  728. /// `DefaultImageProcessor.default`.
  729. /// - Returns: The hash which is used as the cache file name.
  730. ///
  731. /// - Note:
  732. /// By default, for a given combination of `key` and `identifier`, `ImageCache` will use the value
  733. /// returned by this method as the cache file name. You can use this value to check and match cache file
  734. /// if you need.
  735. open func hash(
  736. forKey key: String,
  737. processorIdentifier identifier: String = DefaultImageProcessor.default.identifier) -> String
  738. {
  739. let computedKey = key.computedKey(with: identifier)
  740. return diskStorage.cacheFileName(forKey: computedKey)
  741. }
  742. /// Calculates the size taken by the disk storage.
  743. /// It is the total file size of all cached files in the `diskStorage` on disk in bytes.
  744. ///
  745. /// - Parameter handler: Called with the size calculating finishes. This closure is invoked from the main queue.
  746. open func calculateDiskStorageSize(completion handler: @escaping ((Result<UInt, KingfisherError>) -> Void)) {
  747. ioQueue.async {
  748. do {
  749. let size = try self.diskStorage.totalSize()
  750. DispatchQueue.main.async { handler(.success(size)) }
  751. } catch let error as KingfisherError {
  752. DispatchQueue.main.async { handler(.failure(error)) }
  753. } catch {
  754. assertionFailure("The internal thrown error should be a `KingfisherError`.")
  755. }
  756. }
  757. }
  758. /// Gets the cache path for the key.
  759. /// It is useful for projects with web view or anyone that needs access to the local file path.
  760. ///
  761. /// i.e. Replacing the `<img src='path_for_key'>` tag in your HTML.
  762. ///
  763. /// - Parameters:
  764. /// - key: The key used for caching the image.
  765. /// - identifier: Processor identifier which used for this image. Default is the `identifier` of
  766. /// `DefaultImageProcessor.default`.
  767. /// - Returns: The disk path of cached image under the given `key` and `identifier`.
  768. ///
  769. /// - Note:
  770. /// This method does not guarantee there is an image already cached in the returned path. It just gives your
  771. /// the path that the image should be, if it exists in disk storage.
  772. ///
  773. /// You could use `isCached(forKey:)` method to check whether the image is cached under that key in disk.
  774. open func cachePath(
  775. forKey key: String,
  776. processorIdentifier identifier: String = DefaultImageProcessor.default.identifier) -> String
  777. {
  778. let computedKey = key.computedKey(with: identifier)
  779. return diskStorage.cacheFileURL(forKey: computedKey).path
  780. }
  781. // MARK: - Concurrency
  782. open func store(
  783. _ image: KFCrossPlatformImage,
  784. original: Data? = nil,
  785. forKey key: String,
  786. options: KingfisherParsedOptionsInfo,
  787. toDisk: Bool = true
  788. ) async throws {
  789. try await withCheckedThrowingContinuation { continuation in
  790. store(image, original: original, forKey: key, options: options, toDisk: toDisk) {
  791. continuation.resume(with: $0.diskCacheResult)
  792. }
  793. }
  794. }
  795. open func store(
  796. _ image: KFCrossPlatformImage,
  797. original: Data? = nil,
  798. forKey key: String,
  799. processorIdentifier identifier: String = "",
  800. cacheSerializer serializer: CacheSerializer = DefaultCacheSerializer.default,
  801. toDisk: Bool = true
  802. ) async throws {
  803. try await withCheckedThrowingContinuation { continuation in
  804. store(
  805. image,
  806. original: original,
  807. forKey: key,
  808. processorIdentifier: identifier,
  809. cacheSerializer: serializer,
  810. toDisk: toDisk) {
  811. // Only `diskCacheResult` can fail
  812. continuation.resume(with: $0.diskCacheResult)
  813. }
  814. }
  815. }
  816. open func storeToDisk(
  817. _ data: Data,
  818. forKey key: String,
  819. processorIdentifier identifier: String = "",
  820. expiration: StorageExpiration? = nil
  821. ) async throws
  822. {
  823. try await withCheckedThrowingContinuation { continuation in
  824. storeToDisk(
  825. data,
  826. forKey: key,
  827. processorIdentifier: identifier,
  828. expiration: expiration) {
  829. // Only `diskCacheResult` can fail
  830. continuation.resume(with: $0.diskCacheResult)
  831. }
  832. }
  833. }
  834. /// Removes the image for the given key from the cache.
  835. ///
  836. /// - Parameters:
  837. /// - key: The key used for caching the image.
  838. /// - identifier: The identifier of processor being used for caching. If you are using a processor for the
  839. /// image, pass the identifier of processor to this parameter.
  840. /// - fromMemory: Whether this image should be removed from memory storage or not.
  841. /// If `false`, the image won't be removed from the memory storage. Default is `true`.
  842. /// - fromDisk: Whether this image should be removed from disk storage or not.
  843. /// If `false`, the image won't be removed from the disk storage. Default is `true`.
  844. open func removeImage(
  845. forKey key: String,
  846. processorIdentifier identifier: String = "",
  847. fromMemory: Bool = true,
  848. fromDisk: Bool = true
  849. ) async throws {
  850. return try await withCheckedThrowingContinuation { continuation in
  851. removeImage(
  852. forKey: key,
  853. processorIdentifier: identifier,
  854. fromMemory: fromMemory,
  855. fromDisk: fromDisk,
  856. completionHandler: { error in
  857. if let error {
  858. continuation.resume(throwing: error)
  859. } else {
  860. continuation.resume()
  861. }
  862. }
  863. )
  864. }
  865. }
  866. /// Gets an image for a given key from the cache, either from memory storage or disk storage.
  867. ///
  868. /// - Parameters:
  869. /// - key: The key used for caching the image.
  870. /// - options: The `KingfisherParsedOptionsInfo` options setting used for retrieving the image.
  871. ///
  872. /// - Returns:
  873. /// If the image retrieving operation finishes without problem, an `ImageCacheResult` value.
  874. ///
  875. /// - Throws: An error of type `KingfisherError`, if any error happens inside Kingfisher framework.
  876. open func retrieveImage(
  877. forKey key: String,
  878. options: KingfisherParsedOptionsInfo
  879. ) async throws -> ImageCacheResult {
  880. try await withCheckedThrowingContinuation {
  881. retrieveImage(forKey: key, options: options, completionHandler: $0.resume)
  882. }
  883. }
  884. /// Gets an image for a given key from the cache, either from memory storage or disk storage.
  885. ///
  886. /// - Parameters:
  887. /// - key: The key used for caching the image.
  888. /// - options: The `KingfisherOptionsInfo` options setting used for retrieving the image.
  889. ///
  890. /// - Returns:
  891. /// If the image retrieving operation finishes without problem, an `ImageCacheResult` value.
  892. ///
  893. /// - Throws: An error of type `KingfisherError`, if any error happens inside Kingfisher framework.
  894. ///
  895. /// - Note: This method is marked as `open` for only compatible purpose. Do not overide this method.
  896. /// Instead, override the version receives `KingfisherParsedOptionsInfo` instead.
  897. open func retrieveImage(
  898. forKey key: String,
  899. options: KingfisherOptionsInfo? = nil
  900. ) async throws -> ImageCacheResult {
  901. try await withCheckedThrowingContinuation {
  902. retrieveImage(forKey: key, options: options, completionHandler: $0.resume)
  903. }
  904. }
  905. /// Gets an image for a given key from the disk storage.
  906. ///
  907. /// - Parameters:
  908. /// - key: The key used for caching the image.
  909. /// - options: The `KingfisherOptionsInfo` options setting used for retrieving the image.
  910. ///
  911. /// - Throws: An error of type `KingfisherError`, if any error happens inside Kingfisher framework.
  912. open func retrieveImageInDiskCache(
  913. forKey key: String,
  914. options: KingfisherOptionsInfo? = nil
  915. ) async throws -> KFCrossPlatformImage? {
  916. try await withCheckedThrowingContinuation {
  917. retrieveImageInDiskCache(forKey: key, options: options, completionHandler: $0.resume)
  918. }
  919. }
  920. /// Clears the memory & disk storage of this cache. This is an async operation.
  921. open func clearCache() async {
  922. await withCheckedContinuation {
  923. clearCache(completion: $0.resume)
  924. }
  925. }
  926. /// Clears the disk storage of this cache. This is an async operation.
  927. open func clearDiskCache() async {
  928. await withCheckedContinuation {
  929. clearDiskCache(completion: $0.resume)
  930. }
  931. }
  932. /// Clears the expired images from memory & disk storage. This is an async operation.
  933. open func cleanExpiredCache() async {
  934. await withCheckedContinuation {
  935. cleanExpiredCache(completion: $0.resume)
  936. }
  937. }
  938. /// Clears the expired images from disk storage. This is an async operation.
  939. open func cleanExpiredDiskCache() async {
  940. await withCheckedContinuation {
  941. cleanExpiredDiskCache(completion: $0.resume)
  942. }
  943. }
  944. /// Calculates and returns the size taken by the disk storage.
  945. /// It is the total file size of all cached files in the `diskStorage` on disk in bytes.
  946. open var diskStorageSize: UInt {
  947. get async throws {
  948. try await withCheckedThrowingContinuation {
  949. calculateDiskStorageSize(completion: $0.resume)
  950. }
  951. }
  952. }
  953. }
  954. // Concurrency
  955. #if !os(macOS) && !os(watchOS)
  956. // MARK: - For App Extensions
  957. extension UIApplication: KingfisherCompatible { }
  958. extension KingfisherWrapper where Base: UIApplication {
  959. public static var shared: UIApplication? {
  960. let selector = NSSelectorFromString("sharedApplication")
  961. guard Base.responds(to: selector) else { return nil }
  962. return Base.perform(selector).takeUnretainedValue() as? UIApplication
  963. }
  964. }
  965. #endif
  966. extension String {
  967. func computedKey(with identifier: String) -> String {
  968. if identifier.isEmpty {
  969. return self
  970. } else {
  971. return appending("@\(identifier)")
  972. }
  973. }
  974. }