ImageCache.swift 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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. /// - forcedExtension: The expected extension of the file. If `nil`, the file extension will be determined by the
  352. /// disk storage configuration instead.
  353. /// - serializer: The ``CacheSerializer`` used to convert the `image` and `original` to the data that will be
  354. /// stored to disk. By default, the ``DefaultCacheSerializer/default`` will be used.
  355. /// - toDisk: Whether this image should be cached to disk or not. If `false`, the image is only cached in memory.
  356. /// Otherwise, it is cached in both memory storage and disk storage. The default is `true`.
  357. /// - callbackQueue: The callback queue on which the `completionHandler` is invoked. The default is
  358. /// ``CallbackQueue/untouch``. Under this default ``CallbackQueue/untouch`` queue, if `toDisk` is `false`, it
  359. /// means the `completionHandler` will be invoked from the caller queue of this method; if `toDisk` is `true`,
  360. /// the `completionHandler` will be called from an internal file IO queue. To change this behavior, specify
  361. /// another ``CallbackQueue`` value.
  362. /// - completionHandler: A closure that is invoked when the cache operation finishes.
  363. open func store(
  364. _ image: KFCrossPlatformImage,
  365. original: Data? = nil,
  366. forKey key: String,
  367. processorIdentifier identifier: String = "",
  368. forcedExtension: String? = nil,
  369. cacheSerializer serializer: any CacheSerializer = DefaultCacheSerializer.default,
  370. toDisk: Bool = true,
  371. callbackQueue: CallbackQueue = .untouch,
  372. completionHandler: (@Sendable (CacheStoreResult) -> Void)? = nil
  373. )
  374. {
  375. struct TempProcessor: ImageProcessor {
  376. let identifier: String
  377. func process(item: ImageProcessItem, options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage? {
  378. return nil
  379. }
  380. }
  381. let options = KingfisherParsedOptionsInfo([
  382. .processor(TempProcessor(identifier: identifier)),
  383. .cacheSerializer(serializer),
  384. .callbackQueue(callbackQueue),
  385. .forcedCacheFileExtension(forcedExtension)
  386. ])
  387. store(
  388. image,
  389. original: original,
  390. forKey: key,
  391. options: options,
  392. toDisk: toDisk,
  393. completionHandler: completionHandler
  394. )
  395. }
  396. /// Store some data to the disk.
  397. ///
  398. /// - Parameters:
  399. /// - data: The data to be stored.
  400. /// - key: The key used for caching the data.
  401. /// - identifier: The identifier of the processor being used for caching. If you are using a processor for the
  402. /// image, pass the identifier of the processor to this parameter.
  403. /// - forcedExtension: The expected extension of the file. If `nil`, the file extension will be determined by the
  404. /// disk storage configuration instead.
  405. /// - expiration: The expiration policy used by this storage action.
  406. /// - callbackQueue: The callback queue on which the `completionHandler` is invoked. The default is
  407. /// ``CallbackQueue/untouch``. Under this default ``CallbackQueue/untouch`` queue, if `toDisk` is `false`, it
  408. /// means the `completionHandler` will be invoked from the caller queue of this method; if `toDisk` is `true`,
  409. /// the `completionHandler` will be called from an internal file IO queue. To change this behavior, specify
  410. /// another ``CallbackQueue`` value.
  411. /// - completionHandler: A closure that is invoked when the cache operation finishes.
  412. open func storeToDisk(
  413. _ data: Data,
  414. forKey key: String,
  415. processorIdentifier identifier: String = "",
  416. forcedExtension: String? = nil,
  417. expiration: StorageExpiration? = nil,
  418. callbackQueue: CallbackQueue = .untouch,
  419. completionHandler: (@Sendable (CacheStoreResult) -> Void)? = nil)
  420. {
  421. ioQueue.async {
  422. self.syncStoreToDisk(
  423. data,
  424. forKey: key,
  425. forcedExtension: forcedExtension,
  426. processorIdentifier: identifier,
  427. callbackQueue: callbackQueue,
  428. expiration: expiration,
  429. completionHandler: completionHandler
  430. )
  431. }
  432. }
  433. private func syncStoreToDisk(
  434. _ data: Data,
  435. forKey key: String,
  436. forcedExtension: String?,
  437. processorIdentifier identifier: String = "",
  438. callbackQueue: CallbackQueue = .untouch,
  439. expiration: StorageExpiration? = nil,
  440. writeOptions: Data.WritingOptions = [],
  441. completionHandler: (@Sendable (CacheStoreResult) -> Void)? = nil)
  442. {
  443. let computedKey = key.computedKey(with: identifier)
  444. let result: CacheStoreResult
  445. do {
  446. try self.diskStorage.store(
  447. value: data,
  448. forKey: computedKey,
  449. expiration: expiration,
  450. writeOptions: writeOptions,
  451. forcedExtension: forcedExtension
  452. )
  453. result = CacheStoreResult(memoryCacheResult: .success(()), diskCacheResult: .success(()))
  454. } catch {
  455. let diskError: KingfisherError
  456. if let error = error as? KingfisherError {
  457. diskError = error
  458. } else {
  459. diskError = .cacheError(reason: .cannotConvertToData(object: data, error: error))
  460. }
  461. result = CacheStoreResult(
  462. memoryCacheResult: .success(()),
  463. diskCacheResult: .failure(diskError)
  464. )
  465. }
  466. if let completionHandler = completionHandler {
  467. callbackQueue.execute { completionHandler(result) }
  468. }
  469. }
  470. // MARK: Removing Images
  471. /// Removes the image for the given key from the cache.
  472. ///
  473. /// - Parameters:
  474. /// - key: The key used for caching the image.
  475. /// - identifier: The identifier of the processor being used for caching. If you are using a processor for the
  476. /// image, pass the identifier of the processor to this parameter.
  477. /// - forcedExtension: The expected extension of the file. If `nil`, the file extension will be determined by the
  478. /// disk storage configuration instead.
  479. /// - fromMemory: Whether this image should be removed from memory storage or not. If `false`, the image won't be
  480. /// removed from the memory storage. The default is `true`.
  481. /// - fromDisk: Whether this image should be removed from the disk storage or not. If `false`, the image won't be
  482. /// removed from the disk storage. The default is `true`.
  483. /// - callbackQueue: The callback queue on which the `completionHandler` is invoked. The default is
  484. /// ``CallbackQueue/untouch``.
  485. /// - completionHandler: A closure that is invoked when the cache removal operation finishes.
  486. open func removeImage(
  487. forKey key: String,
  488. processorIdentifier identifier: String = "",
  489. forcedExtension: String? = nil,
  490. fromMemory: Bool = true,
  491. fromDisk: Bool = true,
  492. callbackQueue: CallbackQueue = .untouch,
  493. completionHandler: (@Sendable () -> Void)? = nil
  494. )
  495. {
  496. removeImage(
  497. forKey: key,
  498. processorIdentifier: identifier,
  499. forcedExtension: forcedExtension,
  500. fromMemory: fromMemory,
  501. fromDisk: fromDisk,
  502. callbackQueue: callbackQueue,
  503. completionHandler: { _ in completionHandler?() } // This is a version which ignores error.
  504. )
  505. }
  506. func removeImage(
  507. forKey key: String,
  508. processorIdentifier identifier: String = "",
  509. forcedExtension: String?,
  510. fromMemory: Bool = true,
  511. fromDisk: Bool = true,
  512. callbackQueue: CallbackQueue = .untouch,
  513. completionHandler: (@Sendable ((any Error)?) -> Void)? = nil)
  514. {
  515. let computedKey = key.computedKey(with: identifier)
  516. if fromMemory {
  517. memoryStorage.remove(forKey: computedKey)
  518. }
  519. @Sendable func callHandler(_ error: (any Error)?) {
  520. if let completionHandler = completionHandler {
  521. callbackQueue.execute { completionHandler(error) }
  522. }
  523. }
  524. if fromDisk {
  525. ioQueue.async{
  526. do {
  527. try self.diskStorage.remove(forKey: computedKey, forcedExtension: forcedExtension)
  528. callHandler(nil)
  529. } catch {
  530. callHandler(error)
  531. }
  532. }
  533. } else {
  534. callHandler(nil)
  535. }
  536. }
  537. // MARK: Getting Images
  538. /// Retrieves an image for a given key from the cache, either from memory storage or disk storage.
  539. ///
  540. /// - Parameters:
  541. /// - key: The key used for caching the image.
  542. /// - options: The ``KingfisherParsedOptionsInfo`` options setting used for retrieving the image.
  543. /// - callbackQueue: The callback queue on which the `completionHandler` is invoked.
  544. /// The default is ``CallbackQueue/mainCurrentOrAsync``.
  545. /// - completionHandler: A closure that is invoked when the image retrieval operation finishes. If the image
  546. /// retrieval operation finishes without any problems, an ``ImageCacheResult`` value will be sent to this closure
  547. /// as a result. Otherwise, a ``KingfisherError`` result with detailed failure reason will be sent.
  548. open func retrieveImage(
  549. forKey key: String,
  550. options: KingfisherParsedOptionsInfo,
  551. callbackQueue: CallbackQueue = .mainCurrentOrAsync,
  552. completionHandler: (@Sendable (Result<ImageCacheResult, KingfisherError>) -> Void)?)
  553. {
  554. // No completion handler. No need to start working and early return.
  555. guard let completionHandler = completionHandler else { return }
  556. // Try to check the image from memory cache first.
  557. if let image = retrieveImageInMemoryCache(forKey: key, options: options) {
  558. callbackQueue.execute { completionHandler(.success(.memory(image))) }
  559. } else if options.fromMemoryCacheOrRefresh {
  560. callbackQueue.execute { completionHandler(.success(.none)) }
  561. } else {
  562. // Begin to disk search.
  563. self.retrieveImageInDiskCache(forKey: key, options: options, callbackQueue: callbackQueue) {
  564. result in
  565. switch result {
  566. case .success(let image):
  567. guard let image = image else {
  568. // No image found in disk storage.
  569. callbackQueue.execute { completionHandler(.success(.none)) }
  570. return
  571. }
  572. // Cache the disk image to memory.
  573. // We are passing `false` to `toDisk`, the memory cache does not change
  574. // callback queue, we can call `completionHandler` without another dispatch.
  575. var cacheOptions = options
  576. cacheOptions.callbackQueue = .untouch
  577. self.store(
  578. image,
  579. forKey: key,
  580. options: cacheOptions,
  581. toDisk: false)
  582. {
  583. _ in
  584. callbackQueue.execute { completionHandler(.success(.disk(image))) }
  585. }
  586. case .failure(let error):
  587. callbackQueue.execute { completionHandler(.failure(error)) }
  588. }
  589. }
  590. }
  591. }
  592. /// Retrieves an image for a given key from the cache, either from memory storage or disk storage.
  593. ///
  594. ///
  595. /// - Parameters:
  596. /// - key: The key used for caching the image.
  597. /// - options: The ``KingfisherOptionsInfo`` options setting used for retrieving the image.
  598. /// - callbackQueue: The callback queue on which the `completionHandler` is invoked.
  599. /// The default is ``CallbackQueue/mainCurrentOrAsync``.
  600. /// - completionHandler: A closure that is invoked when the image retrieval operation finishes. If the image
  601. /// retrieval operation finishes without any problems, an ``ImageCacheResult`` value will be sent to this closure
  602. /// as a result. Otherwise, a ``KingfisherError`` result with detailed failure reason will be sent.
  603. ///
  604. /// > This method is marked as `open` for compatibility purposes only. Do not override this method. Instead,
  605. /// override the version ``ImageCache/retrieveImageInDiskCache(forKey:options:callbackQueue:completionHandler:)``
  606. /// accepts a ``KingfisherParsedOptionsInfo`` value.
  607. open func retrieveImage(
  608. forKey key: String,
  609. options: KingfisherOptionsInfo? = nil,
  610. callbackQueue: CallbackQueue = .mainCurrentOrAsync,
  611. completionHandler: (@Sendable (Result<ImageCacheResult, KingfisherError>) -> Void)?
  612. )
  613. {
  614. retrieveImage(
  615. forKey: key,
  616. options: KingfisherParsedOptionsInfo(options),
  617. callbackQueue: callbackQueue,
  618. completionHandler: completionHandler)
  619. }
  620. /// Retrieves an image associated with a given key from the memory storage.
  621. ///
  622. /// - Parameters:
  623. /// - key: The key used for caching the image.
  624. /// - options: The ``KingfisherParsedOptionsInfo`` options setting used to fetch the image.
  625. /// - Returns: The image stored in the memory cache if it exists and is valid. If the image does not exist or has
  626. /// already expired, `nil` is returned.
  627. open func retrieveImageInMemoryCache(
  628. forKey key: String,
  629. options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage?
  630. {
  631. let computedKey = key.computedKey(with: options.processor.identifier)
  632. return memoryStorage.value(
  633. forKey: computedKey,
  634. extendingExpiration: options.memoryCacheAccessExtendingExpiration
  635. )
  636. }
  637. /// Retrieves an image associated with a given key from the memory storage.
  638. ///
  639. /// - Parameters:
  640. /// - key: The key used for caching the image.
  641. /// - options: The ``KingfisherOptionsInfo`` options setting used to fetch the image.
  642. /// - Returns: The image stored in the memory cache if it exists and is valid. If the image does not exist or has
  643. /// already expired, `nil` is returned.
  644. ///
  645. /// > This method is marked as `open` for compatibility purposes only. Do not override this method. Instead,
  646. /// override the version ``ImageCache/retrieveImageInMemoryCache(forKey:options:)-2xj0`` that accepts a
  647. /// ``KingfisherParsedOptionsInfo`` value.
  648. open func retrieveImageInMemoryCache(
  649. forKey key: String,
  650. options: KingfisherOptionsInfo? = nil) -> KFCrossPlatformImage?
  651. {
  652. return retrieveImageInMemoryCache(forKey: key, options: KingfisherParsedOptionsInfo(options))
  653. }
  654. func retrieveImageInDiskCache(
  655. forKey key: String,
  656. options: KingfisherParsedOptionsInfo,
  657. callbackQueue: CallbackQueue = .untouch,
  658. completionHandler: @escaping @Sendable (Result<KFCrossPlatformImage?, KingfisherError>) -> Void)
  659. {
  660. let computedKey = key.computedKey(with: options.processor.identifier)
  661. let loadingQueue: CallbackQueue = options.loadDiskFileSynchronously ? .untouch : .dispatch(ioQueue)
  662. loadingQueue.execute {
  663. do {
  664. var image: KFCrossPlatformImage? = nil
  665. if let data = try self.diskStorage.value(
  666. forKey: computedKey,
  667. forcedExtension: options.forcedExtension,
  668. extendingExpiration: options.diskCacheAccessExtendingExpiration
  669. ) {
  670. image = options.cacheSerializer.image(with: data, options: options)
  671. }
  672. if options.backgroundDecode {
  673. image = image?.kf.decoded(scale: options.scaleFactor)
  674. }
  675. callbackQueue.execute { [image] in completionHandler(.success(image)) }
  676. } catch let error as KingfisherError {
  677. callbackQueue.execute { completionHandler(.failure(error)) }
  678. } catch {
  679. assertionFailure("The internal thrown error should be a `KingfisherError`.")
  680. }
  681. }
  682. }
  683. /// Retrieves an image associated with a given key from the disk storage.
  684. ///
  685. /// - Parameters:
  686. /// - key: The key used for caching the image.
  687. /// - options: The ``KingfisherOptionsInfo`` options setting used to fetch the image.
  688. /// - callbackQueue: The callback queue on which the `completionHandler` is invoked.
  689. /// The default is ``CallbackQueue/untouch``.
  690. /// - completionHandler: A closure that is invoked when the operation is finished.
  691. open func retrieveImageInDiskCache(
  692. forKey key: String,
  693. options: KingfisherOptionsInfo? = nil,
  694. callbackQueue: CallbackQueue = .untouch,
  695. completionHandler: @escaping @Sendable (Result<KFCrossPlatformImage?, KingfisherError>) -> Void)
  696. {
  697. retrieveImageInDiskCache(
  698. forKey: key,
  699. options: KingfisherParsedOptionsInfo(options),
  700. callbackQueue: callbackQueue,
  701. completionHandler: completionHandler)
  702. }
  703. // MARK: Cleaning
  704. /// Clears the memory and disk storage of this cache.
  705. ///
  706. /// This is an asynchronous operation. When the cache clearing operation finishes, the `handler` will be invoked.
  707. ///
  708. /// - Parameter handler: A closure that is invoked when the cache clearing operation finishes.
  709. /// This `handler` will be called from the main queue.
  710. public func clearCache(completion handler: (@Sendable () -> Void)? = nil) {
  711. clearMemoryCache()
  712. clearDiskCache(completion: handler)
  713. }
  714. /// Clears the memory storage of this cache.
  715. @objc public func clearMemoryCache() {
  716. memoryStorage.removeAll()
  717. }
  718. /// Clears the disk storage of this cache.
  719. ///
  720. /// This is an asynchronous operation. When the cache clearing operation finishes, the `handler` will be invoked.
  721. ///
  722. /// - Parameter handler: A closure that is invoked when the cache clearing operation finishes.
  723. /// This `handler` will be called from the main queue.
  724. open func clearDiskCache(completion handler: (@Sendable () -> Void)? = nil) {
  725. ioQueue.async {
  726. do {
  727. try self.diskStorage.removeAll()
  728. } catch _ { }
  729. if let handler = handler {
  730. DispatchQueue.main.async { handler() }
  731. }
  732. }
  733. }
  734. /// Clears the expired images from the memory and disk storage.
  735. ///
  736. /// This is an asynchronous operation. When the cache clearing operation finishes, the `handler` will be invoked.
  737. open func cleanExpiredCache(completion handler: (@Sendable () -> Void)? = nil) {
  738. cleanExpiredMemoryCache()
  739. cleanExpiredDiskCache(completion: handler)
  740. }
  741. /// Clears the expired images from the memory storage.
  742. open func cleanExpiredMemoryCache() {
  743. memoryStorage.removeExpired()
  744. }
  745. /// Clears the expired images from disk storage.
  746. ///
  747. /// This is an async operation.
  748. @objc func cleanExpiredDiskCache() {
  749. cleanExpiredDiskCache(completion: nil)
  750. }
  751. /// Clears the expired images from disk storage.
  752. ///
  753. /// This is an asynchronous operation. When the cache clearing operation finishes, the `handler` will be invoked.
  754. ///
  755. /// - Parameter handler: A closure which is invoked when the cache clearing operation finishes.
  756. /// This `handler` will be called from the main queue.
  757. open func cleanExpiredDiskCache(completion handler: (@Sendable () -> Void)? = nil) {
  758. ioQueue.async {
  759. do {
  760. var removed: [URL] = []
  761. let removedExpired = try self.diskStorage.removeExpiredValues()
  762. removed.append(contentsOf: removedExpired)
  763. let removedSizeExceeded = try self.diskStorage.removeSizeExceededValues()
  764. removed.append(contentsOf: removedSizeExceeded)
  765. if !removed.isEmpty {
  766. DispatchQueue.main.async { [removed] in
  767. let cleanedHashes = removed.map { $0.lastPathComponent }
  768. NotificationCenter.default.post(
  769. name: .KingfisherDidCleanDiskCache,
  770. object: self,
  771. userInfo: [KingfisherDiskCacheCleanedHashKey: cleanedHashes])
  772. }
  773. }
  774. if let handler = handler {
  775. DispatchQueue.main.async { handler() }
  776. }
  777. } catch {}
  778. }
  779. }
  780. #if !os(macOS) && !os(watchOS)
  781. /// Clears the expired images from disk storage when the app is in the background.
  782. ///
  783. /// This is an asynchronous operation. When the cache clearing operation finishes, the `handler` will be invoked.
  784. ///
  785. /// In most cases, you should not call this method explicitly. It will be called automatically when a
  786. /// `UIApplicationDidEnterBackgroundNotification` is received.
  787. @MainActor
  788. @objc public func backgroundCleanExpiredDiskCache() {
  789. // if 'sharedApplication()' is unavailable, then return
  790. guard let sharedApplication = KingfisherWrapper<UIApplication>.shared else { return }
  791. actor BackgroundTaskState {
  792. private var value: UIBackgroundTaskIdentifier? = nil
  793. func setValue(_ newValue: UIBackgroundTaskIdentifier) {
  794. value = newValue
  795. }
  796. func takeValidValueAndInvalidate() -> UIBackgroundTaskIdentifier? {
  797. guard let task = value, task != .invalid else { return nil }
  798. value = .invalid
  799. return task
  800. }
  801. }
  802. let taskState = BackgroundTaskState()
  803. let endBackgroundTaskIfNeeded: @Sendable () -> Void = {
  804. Task { @MainActor in
  805. guard let bgTask = await taskState.takeValidValueAndInvalidate() else { return }
  806. guard let sharedApplication = KingfisherWrapper<UIApplication>.shared else { return }
  807. #if compiler(>=6)
  808. sharedApplication.endBackgroundTask(bgTask)
  809. #else
  810. await sharedApplication.endBackgroundTask(bgTask)
  811. #endif
  812. }
  813. }
  814. let createdTask = sharedApplication.beginBackgroundTask(
  815. withName: "Kingfisher:backgroundCleanExpiredDiskCache",
  816. expirationHandler: endBackgroundTaskIfNeeded
  817. )
  818. Task { await taskState.setValue(createdTask) }
  819. cleanExpiredDiskCache {
  820. Task { @MainActor in
  821. endBackgroundTaskIfNeeded()
  822. }
  823. }
  824. }
  825. #endif
  826. // MARK: Image Cache State
  827. /// Returns the cache type for a given `key` and `identifier` combination.
  828. ///
  829. /// This method is used to check whether an image is cached in the current cache. It also provides information on
  830. /// which kind of cache the image can be found in the return value.
  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. /// - forcedExtension: The expected extension of the file. If `nil`, the file extension will be determined by the
  837. /// disk storage configuration instead.
  838. ///
  839. /// - Returns: A ``CacheType`` instance that indicates the cache status. ``CacheType/none`` indicates that the
  840. /// image is not in the cache or that it has already expired.
  841. open func imageCachedType(
  842. forKey key: String,
  843. processorIdentifier identifier: String = DefaultImageProcessor.default.identifier,
  844. forcedExtension: String? = nil
  845. ) -> CacheType
  846. {
  847. let computedKey = key.computedKey(with: identifier)
  848. if memoryStorage.isCached(forKey: computedKey) { return .memory }
  849. if diskStorage.isCached(forKey: computedKey, forcedExtension: forcedExtension) { return .disk }
  850. return .none
  851. }
  852. /// Returns whether the file exists in the cache for a given `key` and `identifier` combination.
  853. ///
  854. /// - Parameters:
  855. /// - key: The key used for caching the image.
  856. /// - identifier: The processor identifier used for this image. The default value is the
  857. /// ``DefaultImageProcessor/identifier`` of the ``DefaultImageProcessor/default`` image processor.
  858. /// - forcedExtension: The expected extension of the file. If `nil`, the file extension will be determined by the
  859. /// disk storage configuration instead.
  860. ///
  861. /// - Returns: A `Bool` value indicating whether a cache matches the given `key` and `identifier` combination.
  862. ///
  863. /// > The return value does not contain information about the kind of storage the cache matches from.
  864. /// > To obtain information about the cache type according to ``CacheType``, use
  865. /// ``ImageCache/imageCachedType(forKey:processorIdentifier:forcedExtension:)`` instead.
  866. public func isCached(
  867. forKey key: String,
  868. processorIdentifier identifier: String = DefaultImageProcessor.default.identifier,
  869. forcedExtension: String? = nil
  870. ) -> Bool
  871. {
  872. return imageCachedType(forKey: key, processorIdentifier: identifier, forcedExtension: forcedExtension).cached
  873. }
  874. /// Retrieves the hash used as the cache file name for the key.
  875. ///
  876. /// - Parameters:
  877. /// - key: The key used for caching the image.
  878. /// - identifier: The processor identifier used for this image. The default value is the
  879. /// ``DefaultImageProcessor/identifier`` of the ``DefaultImageProcessor/default`` image processor.
  880. /// - forcedExtension: The expected extension of the file. If `nil`, the file extension will be determined by the
  881. /// disk storage configuration instead.
  882. ///
  883. /// - Returns: The hash used as the cache file name.
  884. ///
  885. /// > By default, for a given combination of `key` and `identifier`, the ``ImageCache`` instance uses the value
  886. /// returned by this method as the cache file name. You can use this value to check and match the cache file if
  887. /// needed.
  888. open func hash(
  889. forKey key: String,
  890. processorIdentifier identifier: String = DefaultImageProcessor.default.identifier,
  891. forcedExtension: String? = nil
  892. ) -> String
  893. {
  894. let computedKey = key.computedKey(with: identifier)
  895. return diskStorage.cacheFileName(forKey: computedKey, forcedExtension: forcedExtension)
  896. }
  897. /// Calculates the size taken by the disk storage.
  898. ///
  899. /// It represents the total file size of all cached files in the ``ImageCache/diskStorage`` on disk in bytes.
  900. ///
  901. /// - Parameter handler: Called when the size calculation is complete. This closure is invoked from the main queue.
  902. open func calculateDiskStorageSize(
  903. completion handler: @escaping (@Sendable (Result<UInt, KingfisherError>) -> Void)
  904. ) {
  905. ioQueue.async {
  906. do {
  907. let size = try self.diskStorage.totalSize()
  908. DispatchQueue.main.async { handler(.success(size)) }
  909. } catch let error as KingfisherError {
  910. DispatchQueue.main.async { handler(.failure(error)) }
  911. } catch {
  912. assertionFailure("The internal thrown error should be a `KingfisherError`.")
  913. }
  914. }
  915. }
  916. /// Retrieves the cache path for the key.
  917. ///
  918. /// It is useful for projects with a web view or for anyone who needs access to the local file path.
  919. /// For instance, replacing the `<img src='path_for_key'>` tag in your HTML.
  920. ///
  921. /// - Parameters:
  922. /// - key: The key used for caching the image.
  923. /// - identifier: The processor identifier used for this image. The default value is the
  924. /// ``DefaultImageProcessor/identifier`` of the ``DefaultImageProcessor/default`` image processor.
  925. /// - forcedExtension: The expected extension of the file. If `nil`, the file extension will be determined by the
  926. /// disk storage configuration instead.
  927. ///
  928. /// - Returns: The disk path of the cached image under the given `key` and `identifier`.
  929. ///
  930. /// > This method does not guarantee that there is an image already cached in the returned path. It simply provides
  931. /// > the path where the image should be if it exists in the disk storage.
  932. /// >
  933. /// > You could use the ``ImageCache/isCached(forKey:processorIdentifier:forcedExtension:)`` method to check whether the image is
  934. /// cached under that key on disk if necessary.
  935. open func cachePath(
  936. forKey key: String,
  937. processorIdentifier identifier: String = DefaultImageProcessor.default.identifier,
  938. forcedExtension: String? = nil
  939. ) -> String
  940. {
  941. let computedKey = key.computedKey(with: identifier)
  942. return diskStorage.cacheFileURL(forKey: computedKey, forcedExtension: forcedExtension).path
  943. }
  944. /// Returns the file URL if a disk cache file is existing for the target key, identifier and forcedExtension
  945. /// combination. Otherwise, if the requested cache value is not on the disk as a file, `nil`.
  946. ///
  947. /// - Parameters:
  948. /// - key: The key used for caching the item.
  949. /// - identifier: The processor identifier used for this image. It involves into calculating the final cache key.
  950. /// - forcedExtension: The expected extension of the file.
  951. /// - Returns: The file URL if a disk cache file is existing for the combination. Otherwise, `nil`.
  952. open func cacheFileURLIfOnDisk(
  953. forKey key: String,
  954. processorIdentifier identifier: String = DefaultImageProcessor.default.identifier,
  955. forcedExtension: String? = nil
  956. ) -> URL?
  957. {
  958. let computedKey = key.computedKey(with: identifier)
  959. return diskStorage.isCached(
  960. forKey: computedKey,
  961. forcedExtension: forcedExtension
  962. ) ? diskStorage.cacheFileURL(forKey: computedKey, forcedExtension: forcedExtension) : nil
  963. }
  964. // MARK: - Concurrency
  965. /// Stores an image to the cache.
  966. ///
  967. /// - Parameters:
  968. /// - image: The image that to be stored.
  969. /// - original: The original data of the image. This value will be forwarded to the provided `serializer` for
  970. /// further use. By default, Kingfisher uses a ``DefaultCacheSerializer`` to serialize the image to data for
  971. /// caching in disk. It checks the image format based on the `original` data to determine the appropriate image
  972. /// format to use. For other types of `serializer`, it depends on their implementation details on how to use this
  973. /// original data.
  974. /// - key: The key used for caching the image.
  975. /// - options: The options which contains configurations for caching the image.
  976. /// - toDisk: Whether this image should be cached to disk or not. If `false`, the image is only cached in memory.
  977. /// Otherwise, it is cached in both memory storage and disk storage. The default is `true`.
  978. open func store(
  979. _ image: KFCrossPlatformImage,
  980. original: Data? = nil,
  981. forKey key: String,
  982. options: KingfisherParsedOptionsInfo,
  983. toDisk: Bool = true
  984. ) async throws {
  985. try await withCheckedThrowingContinuation { continuation in
  986. store(image, original: original, forKey: key, options: options, toDisk: toDisk) {
  987. continuation.resume(with: $0.diskCacheResult)
  988. }
  989. }
  990. }
  991. /// Stores an image in the cache.
  992. ///
  993. /// - Parameters:
  994. /// - image: The image to be stored.
  995. /// - original: The original data of the image. This value will be forwarded to the provided `serializer` for
  996. /// further use. By default, Kingfisher uses a ``DefaultCacheSerializer`` to serialize the image to data for
  997. /// caching in disk. It checks the image format based on the `original` data to determine the appropriate image
  998. /// format to use. For other types of `serializer`, it depends on their implementation details on how to use this
  999. /// original data.
  1000. /// - key: The key used for caching the image.
  1001. /// - identifier: The identifier of the processor being used for caching. If you are using a processor for the
  1002. /// image, pass the identifier of the processor to this parameter.
  1003. /// - forcedExtension: The file extension, if exists.
  1004. /// - serializer: The ``CacheSerializer`` used to convert the `image` and `original` to the data that will be
  1005. /// stored to disk. By default, the ``DefaultCacheSerializer/default`` will be used.
  1006. /// - toDisk: Whether this image should be cached to disk or not. If `false`, the image is only cached in memory.
  1007. /// Otherwise, it is cached in both memory storage and disk storage. The default is `true`.
  1008. open func store(
  1009. _ image: KFCrossPlatformImage,
  1010. original: Data? = nil,
  1011. forKey key: String,
  1012. processorIdentifier identifier: String = "",
  1013. forcedExtension: String? = nil,
  1014. cacheSerializer serializer: any CacheSerializer = DefaultCacheSerializer.default,
  1015. toDisk: Bool = true
  1016. ) async throws {
  1017. try await withCheckedThrowingContinuation { continuation in
  1018. store(
  1019. image,
  1020. original: original,
  1021. forKey: key,
  1022. processorIdentifier: identifier,
  1023. forcedExtension: forcedExtension,
  1024. cacheSerializer: serializer,
  1025. toDisk: toDisk) {
  1026. // Only `diskCacheResult` can fail
  1027. continuation.resume(with: $0.diskCacheResult)
  1028. }
  1029. }
  1030. }
  1031. open func storeToDisk(
  1032. _ data: Data,
  1033. forKey key: String,
  1034. processorIdentifier identifier: String = "",
  1035. forcedExtension: String? = nil,
  1036. expiration: StorageExpiration? = nil
  1037. ) async throws
  1038. {
  1039. try await withCheckedThrowingContinuation { continuation in
  1040. storeToDisk(
  1041. data,
  1042. forKey: key,
  1043. processorIdentifier: identifier,
  1044. forcedExtension: forcedExtension,
  1045. expiration: expiration) {
  1046. // Only `diskCacheResult` can fail
  1047. continuation.resume(with: $0.diskCacheResult)
  1048. }
  1049. }
  1050. }
  1051. /// Removes the image for the given key from the cache.
  1052. ///
  1053. /// - Parameters:
  1054. /// - key: The key used for caching the image.
  1055. /// - identifier: The identifier of the processor being used for caching. If you are using a processor for the
  1056. /// image, pass the identifier of the processor to this parameter.
  1057. /// - forcedExtension: The file extension, if exists.
  1058. /// - fromMemory: Whether this image should be removed from memory storage or not. If `false`, the image won't be
  1059. /// removed from the memory storage. The default is `true`.
  1060. /// - fromDisk: Whether this image should be removed from the disk storage or not. If `false`, the image won't be
  1061. /// removed from the disk storage. The default is `true`.
  1062. open func removeImage(
  1063. forKey key: String,
  1064. processorIdentifier identifier: String = "",
  1065. forcedExtension: String? = nil,
  1066. fromMemory: Bool = true,
  1067. fromDisk: Bool = true
  1068. ) async throws {
  1069. return try await withCheckedThrowingContinuation { continuation in
  1070. removeImage(
  1071. forKey: key,
  1072. processorIdentifier: identifier,
  1073. forcedExtension: forcedExtension,
  1074. fromMemory: fromMemory,
  1075. fromDisk: fromDisk,
  1076. completionHandler: { error in
  1077. if let error {
  1078. continuation.resume(throwing: error)
  1079. } else {
  1080. continuation.resume()
  1081. }
  1082. }
  1083. )
  1084. }
  1085. }
  1086. /// Retrieves an image for a given key from the cache, either from memory storage or disk storage.
  1087. ///
  1088. /// - Parameters:
  1089. /// - key: The key used for caching the image.
  1090. /// - options: The ``KingfisherParsedOptionsInfo`` options setting used for retrieving the image.
  1091. /// - Returns:
  1092. /// If the image retrieving operation finishes without problem, an ``ImageCacheResult`` value.
  1093. ///
  1094. /// - Throws: An error of type ``KingfisherError``, if any error happens inside Kingfisher framework.
  1095. open func retrieveImage(
  1096. forKey key: String,
  1097. options: KingfisherParsedOptionsInfo
  1098. ) async throws -> ImageCacheResult {
  1099. try await withCheckedThrowingContinuation { continuation in
  1100. retrieveImage(forKey: key, options: options) { continuation.resume(with: $0) }
  1101. }
  1102. }
  1103. /// Retrieves an image for a given key from the cache, either from memory storage or disk storage.
  1104. ///
  1105. /// - Parameters:
  1106. /// - key: The key used for caching the image.
  1107. /// - options: The ``KingfisherOptionsInfo`` options setting used for retrieving the image.
  1108. ///
  1109. /// - Returns: If the image retrieving operation finishes without problem, an ``ImageCacheResult`` value.
  1110. ///
  1111. /// - Throws: An error of type ``KingfisherError``, if any error happens inside Kingfisher framework.
  1112. ///
  1113. /// > This method is marked as `open` for compatibility purposes only. Do not override this method. Instead,
  1114. /// override the version ``ImageCache/retrieveImage(forKey:options:callbackQueue:completionHandler:)-1jjo3`` that
  1115. /// accepts a ``KingfisherParsedOptionsInfo`` value.
  1116. open func retrieveImage(
  1117. forKey key: String,
  1118. options: KingfisherOptionsInfo? = nil
  1119. ) async throws -> ImageCacheResult {
  1120. try await withCheckedThrowingContinuation { continuation in
  1121. retrieveImage(forKey: key, options: options) { continuation.resume(with: $0) }
  1122. }
  1123. }
  1124. /// Retrieves an image associated with a given key from the disk storage.
  1125. ///
  1126. /// - Parameters:
  1127. /// - key: The key used for caching the image.
  1128. /// - options: The ``KingfisherOptionsInfo`` options setting used to fetch the image.
  1129. ///
  1130. /// - Returns: The image stored in the disk cache if it exists and is valid. If the image does not exist or has
  1131. /// already expired, `nil` is returned.
  1132. ///
  1133. /// - Returns: If the image retrieving operation finishes without problem, an ``ImageCacheResult`` value.
  1134. ///
  1135. /// - Throws: An error of type ``KingfisherError``, if any error happens inside Kingfisher framework.
  1136. /// ``KingfisherParsedOptionsInfo`` value.
  1137. open func retrieveImageInDiskCache(
  1138. forKey key: String,
  1139. options: KingfisherOptionsInfo? = nil
  1140. ) async throws -> KFCrossPlatformImage? {
  1141. try await withCheckedThrowingContinuation { continuation in
  1142. retrieveImageInDiskCache(forKey: key, options: options) {
  1143. continuation.resume(with: $0)
  1144. }
  1145. }
  1146. }
  1147. /// Clears the memory and disk storage of this cache.
  1148. ///
  1149. /// This is an asynchronous operation. When the cache clearing operation finishes, the whole method returns.
  1150. open func clearCache() async {
  1151. await withCheckedContinuation { continuation in
  1152. clearCache { continuation.resume() }
  1153. }
  1154. }
  1155. /// Clears the disk storage of this cache.
  1156. ///
  1157. /// This is an asynchronous operation. When the cache clearing operation finishes, the whole method returns.
  1158. open func clearDiskCache() async {
  1159. await withCheckedContinuation { continuation in
  1160. clearDiskCache { continuation.resume() }
  1161. }
  1162. }
  1163. /// Clears the expired images from the memory and disk storage.
  1164. ///
  1165. /// This is an asynchronous operation. When the cache clearing operation finishes, the whole method returns.
  1166. open func cleanExpiredCache() async {
  1167. await withCheckedContinuation { continuation in
  1168. cleanExpiredCache { continuation.resume() }
  1169. }
  1170. }
  1171. /// Clears the expired images from disk storage.
  1172. ///
  1173. /// This is an asynchronous operation. When the cache clearing operation finishes, the whole method returns.
  1174. open func cleanExpiredDiskCache() async {
  1175. await withCheckedContinuation { continuation in
  1176. cleanExpiredDiskCache { continuation.resume() }
  1177. }
  1178. }
  1179. /// Calculates the size taken by the disk storage.
  1180. ///
  1181. /// It represents the total file size of all cached files in the ``ImageCache/diskStorage`` on disk in bytes.
  1182. open var diskStorageSize: UInt {
  1183. get async throws {
  1184. try await withCheckedThrowingContinuation { continuation in
  1185. calculateDiskStorageSize { continuation.resume(with: $0) }
  1186. }
  1187. }
  1188. }
  1189. }
  1190. // Concurrency
  1191. #if !os(macOS) && !os(watchOS)
  1192. // MARK: - For App Extensions
  1193. extension UIApplication: KingfisherCompatible { }
  1194. extension KingfisherWrapper where Base: UIApplication {
  1195. public static var shared: UIApplication? {
  1196. let selector = NSSelectorFromString("sharedApplication")
  1197. guard Base.responds(to: selector) else { return nil }
  1198. guard let unmanaged = Base.perform(selector) else { return nil }
  1199. return unmanaged.takeUnretainedValue() as? UIApplication
  1200. }
  1201. }
  1202. #endif
  1203. extension String {
  1204. func computedKey(with identifier: String) -> String {
  1205. if identifier.isEmpty {
  1206. return self
  1207. } else {
  1208. return appending("@\(identifier)")
  1209. }
  1210. }
  1211. }