ImageCache.swift 55 KB

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