ImageCache.swift 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  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. func endBackgroundTaskIfNeeded() {
  804. Task { @MainActor in
  805. guard let bgTask = await taskState.takeValidValueAndInvalidate() else { return }
  806. #if compiler(>=6)
  807. sharedApplication.endBackgroundTask(bgTask)
  808. #else
  809. await sharedApplication.endBackgroundTask(bgTask)
  810. #endif
  811. }
  812. }
  813. let createdTask = sharedApplication.beginBackgroundTask(
  814. withName: "Kingfisher:backgroundCleanExpiredDiskCache",
  815. expirationHandler: endBackgroundTaskIfNeeded
  816. )
  817. Task { await taskState.setValue(createdTask) }
  818. cleanExpiredDiskCache {
  819. endBackgroundTaskIfNeeded()
  820. }
  821. }
  822. #endif
  823. // MARK: Image Cache State
  824. /// Returns the cache type for a given `key` and `identifier` combination.
  825. ///
  826. /// This method is used to check whether an image is cached in the current cache. It also provides information on
  827. /// which kind of cache the image can be found in the return value.
  828. ///
  829. /// - Parameters:
  830. /// - key: The key used for caching the image.
  831. /// - identifier: The processor identifier used for this image. The default value is the
  832. /// ``DefaultImageProcessor/identifier`` of the ``DefaultImageProcessor/default`` image processor.
  833. /// - forcedExtension: The expected extension of the file. If `nil`, the file extension will be determined by the
  834. /// disk storage configuration instead.
  835. ///
  836. /// - Returns: A ``CacheType`` instance that indicates the cache status. ``CacheType/none`` indicates that the
  837. /// image is not in the cache or that it has already expired.
  838. open func imageCachedType(
  839. forKey key: String,
  840. processorIdentifier identifier: String = DefaultImageProcessor.default.identifier,
  841. forcedExtension: String? = nil
  842. ) -> CacheType
  843. {
  844. let computedKey = key.computedKey(with: identifier)
  845. if memoryStorage.isCached(forKey: computedKey) { return .memory }
  846. if diskStorage.isCached(forKey: computedKey, forcedExtension: forcedExtension) { return .disk }
  847. return .none
  848. }
  849. /// Returns whether the file exists in the cache for a given `key` and `identifier` combination.
  850. ///
  851. /// - Parameters:
  852. /// - key: The key used for caching the image.
  853. /// - identifier: The processor identifier used for this image. The default value is the
  854. /// ``DefaultImageProcessor/identifier`` of the ``DefaultImageProcessor/default`` image processor.
  855. /// - forcedExtension: The expected extension of the file. If `nil`, the file extension will be determined by the
  856. /// disk storage configuration instead.
  857. ///
  858. /// - Returns: A `Bool` value indicating whether a cache matches the given `key` and `identifier` combination.
  859. ///
  860. /// > The return value does not contain information about the kind of storage the cache matches from.
  861. /// > To obtain information about the cache type according to ``CacheType``, use
  862. /// ``ImageCache/imageCachedType(forKey:processorIdentifier:forcedExtension:)`` instead.
  863. public func isCached(
  864. forKey key: String,
  865. processorIdentifier identifier: String = DefaultImageProcessor.default.identifier,
  866. forcedExtension: String? = nil
  867. ) -> Bool
  868. {
  869. return imageCachedType(forKey: key, processorIdentifier: identifier, forcedExtension: forcedExtension).cached
  870. }
  871. /// Retrieves the hash used as the cache file name for the key.
  872. ///
  873. /// - Parameters:
  874. /// - key: The key used for caching the image.
  875. /// - identifier: The processor identifier used for this image. The default value is the
  876. /// ``DefaultImageProcessor/identifier`` of the ``DefaultImageProcessor/default`` image processor.
  877. /// - forcedExtension: The expected extension of the file. If `nil`, the file extension will be determined by the
  878. /// disk storage configuration instead.
  879. ///
  880. /// - Returns: The hash used as the cache file name.
  881. ///
  882. /// > By default, for a given combination of `key` and `identifier`, the ``ImageCache`` instance uses the value
  883. /// returned by this method as the cache file name. You can use this value to check and match the cache file if
  884. /// needed.
  885. open func hash(
  886. forKey key: String,
  887. processorIdentifier identifier: String = DefaultImageProcessor.default.identifier,
  888. forcedExtension: String? = nil
  889. ) -> String
  890. {
  891. let computedKey = key.computedKey(with: identifier)
  892. return diskStorage.cacheFileName(forKey: computedKey, forcedExtension: forcedExtension)
  893. }
  894. /// Calculates the size taken by the disk storage.
  895. ///
  896. /// It represents the total file size of all cached files in the ``ImageCache/diskStorage`` on disk in bytes.
  897. ///
  898. /// - Parameter handler: Called when the size calculation is complete. This closure is invoked from the main queue.
  899. open func calculateDiskStorageSize(
  900. completion handler: @escaping (@Sendable (Result<UInt, KingfisherError>) -> Void)
  901. ) {
  902. ioQueue.async {
  903. do {
  904. let size = try self.diskStorage.totalSize()
  905. DispatchQueue.main.async { handler(.success(size)) }
  906. } catch let error as KingfisherError {
  907. DispatchQueue.main.async { handler(.failure(error)) }
  908. } catch {
  909. assertionFailure("The internal thrown error should be a `KingfisherError`.")
  910. }
  911. }
  912. }
  913. /// Retrieves the cache path for the key.
  914. ///
  915. /// It is useful for projects with a web view or for anyone who needs access to the local file path.
  916. /// For instance, replacing the `<img src='path_for_key'>` tag in your HTML.
  917. ///
  918. /// - Parameters:
  919. /// - key: The key used for caching the image.
  920. /// - identifier: The processor identifier used for this image. The default value is the
  921. /// ``DefaultImageProcessor/identifier`` of the ``DefaultImageProcessor/default`` image processor.
  922. /// - forcedExtension: The expected extension of the file. If `nil`, the file extension will be determined by the
  923. /// disk storage configuration instead.
  924. ///
  925. /// - Returns: The disk path of the cached image under the given `key` and `identifier`.
  926. ///
  927. /// > This method does not guarantee that there is an image already cached in the returned path. It simply provides
  928. /// > the path where the image should be if it exists in the disk storage.
  929. /// >
  930. /// > You could use the ``ImageCache/isCached(forKey:processorIdentifier:forcedExtension:)`` method to check whether the image is
  931. /// cached under that key on disk if necessary.
  932. open func cachePath(
  933. forKey key: String,
  934. processorIdentifier identifier: String = DefaultImageProcessor.default.identifier,
  935. forcedExtension: String? = nil
  936. ) -> String
  937. {
  938. let computedKey = key.computedKey(with: identifier)
  939. return diskStorage.cacheFileURL(forKey: computedKey, forcedExtension: forcedExtension).path
  940. }
  941. /// Returns the file URL if a disk cache file is existing for the target key, identifier and forcedExtension
  942. /// combination. Otherwise, if the requested cache value is not on the disk as a file, `nil`.
  943. ///
  944. /// - Parameters:
  945. /// - key: The key used for caching the item.
  946. /// - identifier: The processor identifier used for this image. It involves into calculating the final cache key.
  947. /// - forcedExtension: The expected extension of the file.
  948. /// - Returns: The file URL if a disk cache file is existing for the combination. Otherwise, `nil`.
  949. open func cacheFileURLIfOnDisk(
  950. forKey key: String,
  951. processorIdentifier identifier: String = DefaultImageProcessor.default.identifier,
  952. forcedExtension: String? = nil
  953. ) -> URL?
  954. {
  955. let computedKey = key.computedKey(with: identifier)
  956. return diskStorage.isCached(
  957. forKey: computedKey,
  958. forcedExtension: forcedExtension
  959. ) ? diskStorage.cacheFileURL(forKey: computedKey, forcedExtension: forcedExtension) : nil
  960. }
  961. // MARK: - Concurrency
  962. /// Stores an image to the cache.
  963. ///
  964. /// - Parameters:
  965. /// - image: The image that to be stored.
  966. /// - original: The original data of the image. This value will be forwarded to the provided `serializer` for
  967. /// further use. By default, Kingfisher uses a ``DefaultCacheSerializer`` to serialize the image to data for
  968. /// caching in disk. It checks the image format based on the `original` data to determine the appropriate image
  969. /// format to use. For other types of `serializer`, it depends on their implementation details on how to use this
  970. /// original data.
  971. /// - key: The key used for caching the image.
  972. /// - options: The options which contains configurations for caching the image.
  973. /// - toDisk: Whether this image should be cached to disk or not. If `false`, the image is only cached in memory.
  974. /// Otherwise, it is cached in both memory storage and disk storage. The default is `true`.
  975. open func store(
  976. _ image: KFCrossPlatformImage,
  977. original: Data? = nil,
  978. forKey key: String,
  979. options: KingfisherParsedOptionsInfo,
  980. toDisk: Bool = true
  981. ) async throws {
  982. try await withCheckedThrowingContinuation { continuation in
  983. store(image, original: original, forKey: key, options: options, toDisk: toDisk) {
  984. continuation.resume(with: $0.diskCacheResult)
  985. }
  986. }
  987. }
  988. /// Stores an image in the cache.
  989. ///
  990. /// - Parameters:
  991. /// - image: The image to be stored.
  992. /// - original: The original data of the image. This value will be forwarded to the provided `serializer` for
  993. /// further use. By default, Kingfisher uses a ``DefaultCacheSerializer`` to serialize the image to data for
  994. /// caching in disk. It checks the image format based on the `original` data to determine the appropriate image
  995. /// format to use. For other types of `serializer`, it depends on their implementation details on how to use this
  996. /// original data.
  997. /// - key: The key used for caching the image.
  998. /// - identifier: The identifier of the processor being used for caching. If you are using a processor for the
  999. /// image, pass the identifier of the processor to this parameter.
  1000. /// - forcedExtension: The file extension, if exists.
  1001. /// - serializer: The ``CacheSerializer`` used to convert the `image` and `original` to the data that will be
  1002. /// stored to disk. By default, the ``DefaultCacheSerializer/default`` will be used.
  1003. /// - toDisk: Whether this image should be cached to disk or not. If `false`, the image is only cached in memory.
  1004. /// Otherwise, it is cached in both memory storage and disk storage. The default is `true`.
  1005. open func store(
  1006. _ image: KFCrossPlatformImage,
  1007. original: Data? = nil,
  1008. forKey key: String,
  1009. processorIdentifier identifier: String = "",
  1010. forcedExtension: String? = nil,
  1011. cacheSerializer serializer: any CacheSerializer = DefaultCacheSerializer.default,
  1012. toDisk: Bool = true
  1013. ) async throws {
  1014. try await withCheckedThrowingContinuation { continuation in
  1015. store(
  1016. image,
  1017. original: original,
  1018. forKey: key,
  1019. processorIdentifier: identifier,
  1020. forcedExtension: forcedExtension,
  1021. cacheSerializer: serializer,
  1022. toDisk: toDisk) {
  1023. // Only `diskCacheResult` can fail
  1024. continuation.resume(with: $0.diskCacheResult)
  1025. }
  1026. }
  1027. }
  1028. open func storeToDisk(
  1029. _ data: Data,
  1030. forKey key: String,
  1031. processorIdentifier identifier: String = "",
  1032. forcedExtension: String? = nil,
  1033. expiration: StorageExpiration? = nil
  1034. ) async throws
  1035. {
  1036. try await withCheckedThrowingContinuation { continuation in
  1037. storeToDisk(
  1038. data,
  1039. forKey: key,
  1040. processorIdentifier: identifier,
  1041. forcedExtension: forcedExtension,
  1042. expiration: expiration) {
  1043. // Only `diskCacheResult` can fail
  1044. continuation.resume(with: $0.diskCacheResult)
  1045. }
  1046. }
  1047. }
  1048. /// Removes the image for the given key from the cache.
  1049. ///
  1050. /// - Parameters:
  1051. /// - key: The key used for caching the image.
  1052. /// - identifier: The identifier of the processor being used for caching. If you are using a processor for the
  1053. /// image, pass the identifier of the processor to this parameter.
  1054. /// - forcedExtension: The file extension, if exists.
  1055. /// - fromMemory: Whether this image should be removed from memory storage or not. If `false`, the image won't be
  1056. /// removed from the memory storage. The default is `true`.
  1057. /// - fromDisk: Whether this image should be removed from the disk storage or not. If `false`, the image won't be
  1058. /// removed from the disk storage. The default is `true`.
  1059. open func removeImage(
  1060. forKey key: String,
  1061. processorIdentifier identifier: String = "",
  1062. forcedExtension: String? = nil,
  1063. fromMemory: Bool = true,
  1064. fromDisk: Bool = true
  1065. ) async throws {
  1066. return try await withCheckedThrowingContinuation { continuation in
  1067. removeImage(
  1068. forKey: key,
  1069. processorIdentifier: identifier,
  1070. forcedExtension: forcedExtension,
  1071. fromMemory: fromMemory,
  1072. fromDisk: fromDisk,
  1073. completionHandler: { error in
  1074. if let error {
  1075. continuation.resume(throwing: error)
  1076. } else {
  1077. continuation.resume()
  1078. }
  1079. }
  1080. )
  1081. }
  1082. }
  1083. /// Retrieves an image for a given key from the cache, either from memory storage or disk storage.
  1084. ///
  1085. /// - Parameters:
  1086. /// - key: The key used for caching the image.
  1087. /// - options: The ``KingfisherParsedOptionsInfo`` options setting used for retrieving the image.
  1088. /// - Returns:
  1089. /// If the image retrieving operation finishes without problem, an ``ImageCacheResult`` value.
  1090. ///
  1091. /// - Throws: An error of type ``KingfisherError``, if any error happens inside Kingfisher framework.
  1092. open func retrieveImage(
  1093. forKey key: String,
  1094. options: KingfisherParsedOptionsInfo
  1095. ) async throws -> ImageCacheResult {
  1096. try await withCheckedThrowingContinuation { continuation in
  1097. retrieveImage(forKey: key, options: options) { continuation.resume(with: $0) }
  1098. }
  1099. }
  1100. /// Retrieves an image for a given key from the cache, either from memory storage or disk storage.
  1101. ///
  1102. /// - Parameters:
  1103. /// - key: The key used for caching the image.
  1104. /// - options: The ``KingfisherOptionsInfo`` options setting used for retrieving the image.
  1105. ///
  1106. /// - Returns: If the image retrieving operation finishes without problem, an ``ImageCacheResult`` value.
  1107. ///
  1108. /// - Throws: An error of type ``KingfisherError``, if any error happens inside Kingfisher framework.
  1109. ///
  1110. /// > This method is marked as `open` for compatibility purposes only. Do not override this method. Instead,
  1111. /// override the version ``ImageCache/retrieveImage(forKey:options:callbackQueue:completionHandler:)-1jjo3`` that
  1112. /// accepts a ``KingfisherParsedOptionsInfo`` value.
  1113. open func retrieveImage(
  1114. forKey key: String,
  1115. options: KingfisherOptionsInfo? = nil
  1116. ) async throws -> ImageCacheResult {
  1117. try await withCheckedThrowingContinuation { continuation in
  1118. retrieveImage(forKey: key, options: options) { continuation.resume(with: $0) }
  1119. }
  1120. }
  1121. /// Retrieves an image associated with a given key from the disk storage.
  1122. ///
  1123. /// - Parameters:
  1124. /// - key: The key used for caching the image.
  1125. /// - options: The ``KingfisherOptionsInfo`` options setting used to fetch the image.
  1126. ///
  1127. /// - Returns: The image stored in the disk cache if it exists and is valid. If the image does not exist or has
  1128. /// already expired, `nil` is returned.
  1129. ///
  1130. /// - Returns: If the image retrieving operation finishes without problem, an ``ImageCacheResult`` value.
  1131. ///
  1132. /// - Throws: An error of type ``KingfisherError``, if any error happens inside Kingfisher framework.
  1133. /// ``KingfisherParsedOptionsInfo`` value.
  1134. open func retrieveImageInDiskCache(
  1135. forKey key: String,
  1136. options: KingfisherOptionsInfo? = nil
  1137. ) async throws -> KFCrossPlatformImage? {
  1138. try await withCheckedThrowingContinuation { continuation in
  1139. retrieveImageInDiskCache(forKey: key, options: options) {
  1140. continuation.resume(with: $0)
  1141. }
  1142. }
  1143. }
  1144. /// Clears the memory and disk storage of this cache.
  1145. ///
  1146. /// This is an asynchronous operation. When the cache clearing operation finishes, the whole method returns.
  1147. open func clearCache() async {
  1148. await withCheckedContinuation { continuation in
  1149. clearCache { continuation.resume() }
  1150. }
  1151. }
  1152. /// Clears the disk storage of this cache.
  1153. ///
  1154. /// This is an asynchronous operation. When the cache clearing operation finishes, the whole method returns.
  1155. open func clearDiskCache() async {
  1156. await withCheckedContinuation { continuation in
  1157. clearDiskCache { continuation.resume() }
  1158. }
  1159. }
  1160. /// Clears the expired images from the memory and disk storage.
  1161. ///
  1162. /// This is an asynchronous operation. When the cache clearing operation finishes, the whole method returns.
  1163. open func cleanExpiredCache() async {
  1164. await withCheckedContinuation { continuation in
  1165. cleanExpiredCache { continuation.resume() }
  1166. }
  1167. }
  1168. /// Clears the expired images from disk storage.
  1169. ///
  1170. /// This is an asynchronous operation. When the cache clearing operation finishes, the whole method returns.
  1171. open func cleanExpiredDiskCache() async {
  1172. await withCheckedContinuation { continuation in
  1173. cleanExpiredDiskCache { continuation.resume() }
  1174. }
  1175. }
  1176. /// Calculates the size taken by the disk storage.
  1177. ///
  1178. /// It represents the total file size of all cached files in the ``ImageCache/diskStorage`` on disk in bytes.
  1179. open var diskStorageSize: UInt {
  1180. get async throws {
  1181. try await withCheckedThrowingContinuation { continuation in
  1182. calculateDiskStorageSize { continuation.resume(with: $0) }
  1183. }
  1184. }
  1185. }
  1186. }
  1187. // Concurrency
  1188. #if !os(macOS) && !os(watchOS)
  1189. // MARK: - For App Extensions
  1190. extension UIApplication: KingfisherCompatible { }
  1191. extension KingfisherWrapper where Base: UIApplication {
  1192. public static var shared: UIApplication? {
  1193. let selector = NSSelectorFromString("sharedApplication")
  1194. guard Base.responds(to: selector) else { return nil }
  1195. guard let unmanaged = Base.perform(selector) else { return nil }
  1196. return unmanaged.takeUnretainedValue() as? UIApplication
  1197. }
  1198. }
  1199. #endif
  1200. extension String {
  1201. func computedKey(with identifier: String) -> String {
  1202. if identifier.isEmpty {
  1203. return self
  1204. } else {
  1205. return appending("@\(identifier)")
  1206. }
  1207. }
  1208. }