ImageCache.swift 52 KB

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