You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

843 lines
36 KiB

  1. //
  2. // ImageCache.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 15/4/6.
  6. //
  7. // Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. #if os(macOS)
  27. import AppKit
  28. #else
  29. import UIKit
  30. #endif
  31. extension Notification.Name {
  32. /// This notification will be sent when the disk cache got cleaned either there are cached files expired or the
  33. /// total size exceeding the max allowed size. The manually invoking of `clearDiskCache` method will not trigger
  34. /// this notification.
  35. ///
  36. /// The `object` of this notification is the `ImageCache` object which sends the notification.
  37. /// A list of removed hashes (files) could be retrieved by accessing the array under
  38. /// `KingfisherDiskCacheCleanedHashKey` key in `userInfo` of the notification object you received.
  39. /// By checking the array, you could know the hash codes of files are removed.
  40. public static let KingfisherDidCleanDiskCache =
  41. Notification.Name("com.onevcat.Kingfisher.KingfisherDidCleanDiskCache")
  42. }
  43. /// Key for array of cleaned hashes in `userInfo` of `KingfisherDidCleanDiskCacheNotification`.
  44. public let KingfisherDiskCacheCleanedHashKey = "com.onevcat.Kingfisher.cleanedHash"
  45. /// Cache type of a cached image.
  46. /// - none: The image is not cached yet when retrieving it.
  47. /// - memory: The image is cached in memory.
  48. /// - disk: The image is cached in disk.
  49. public enum CacheType {
  50. /// The image is not cached yet when retrieving it.
  51. case none
  52. /// The image is cached in memory.
  53. case memory
  54. /// The image is cached in disk.
  55. case disk
  56. /// Whether the cache type represents the image is already cached or not.
  57. public var cached: Bool {
  58. switch self {
  59. case .memory, .disk: return true
  60. case .none: return false
  61. }
  62. }
  63. }
  64. /// Represents the caching operation result.
  65. public struct CacheStoreResult {
  66. /// The cache result for memory cache. Caching an image to memory will never fail.
  67. public let memoryCacheResult: Result<(), Never>
  68. /// The cache result for disk cache. If an error happens during caching operation,
  69. /// you can get it from `.failure` case of this `diskCacheResult`.
  70. public let diskCacheResult: Result<(), KingfisherError>
  71. }
  72. extension Image: CacheCostCalculable {
  73. /// Cost of an image
  74. public var cacheCost: Int { return kf.cost }
  75. }
  76. extension Data: DataTransformable {
  77. public func toData() throws -> Data {
  78. return self
  79. }
  80. public static func fromData(_ data: Data) throws -> Data {
  81. return data
  82. }
  83. public static let empty = Data()
  84. }
  85. /// Represents the getting image operation from the cache.
  86. ///
  87. /// - disk: The image can be retrieved from disk cache.
  88. /// - memory: The image can be retrieved memory cache.
  89. /// - none: The image does not exist in the cache.
  90. public enum ImageCacheResult {
  91. /// The image can be retrieved from disk cache.
  92. case disk(Image)
  93. /// The image can be retrieved memory cache.
  94. case memory(Image)
  95. /// The image does not exist in the cache.
  96. case none
  97. /// Extracts the image from cache result. It returns the associated `Image` value for
  98. /// `.disk` and `.memory` case. For `.none` case, `nil` is returned.
  99. public var image: Image? {
  100. switch self {
  101. case .disk(let image): return image
  102. case .memory(let image): return image
  103. case .none: return nil
  104. }
  105. }
  106. /// Returns the corresponding `CacheType` value based on the result type of `self`.
  107. public var cacheType: CacheType {
  108. switch self {
  109. case .disk: return .disk
  110. case .memory: return .memory
  111. case .none: return .none
  112. }
  113. }
  114. }
  115. /// Represents a hybrid caching system which is composed by a `MemoryStorage.Backend` and a `DiskStorage.Backend`.
  116. /// `ImageCache` is a high level abstract for storing an image as well as its data to disk memory and disk, and
  117. /// retrieving them back.
  118. ///
  119. /// While a default image cache object will be used if you prefer the extension methods of Kingfisher, you can create
  120. /// your own cache object and configure its storages as your need. This class also provide an interface for you to set
  121. /// the memory and disk storage config.
  122. open class ImageCache {
  123. // MARK: Singleton
  124. /// The default `ImageCache` object. Kingfisher will use this cache for its related methods if there is no
  125. /// other cache specified. The `name` of this default cache is "default", and you should not use this name
  126. /// for any of your customize cache.
  127. public static let `default` = ImageCache(name: "default")
  128. // MARK: Public Properties
  129. /// The `MemoryStorage.Backend` object used in this cache. This storage holds loaded images in memory with a
  130. /// reasonable expire duration and a maximum memory usage. To modify the configuration of a storage, just set
  131. /// the storage `config` and its properties.
  132. public let memoryStorage: MemoryStorage.Backend<Image>
  133. /// The `DiskStorage.Backend` object used in this cache. This storage stores loaded images in disk with a
  134. /// reasonable expire duration and a maximum disk usage. To modify the configuration of a storage, just set
  135. /// the storage `config` and its properties.
  136. public let diskStorage: DiskStorage.Backend<Data>
  137. private let ioQueue: DispatchQueue
  138. /// Closure that defines the disk cache path from a given path and cacheName.
  139. public typealias DiskCachePathClosure = (URL, String) -> URL
  140. // MARK: Initializers
  141. /// Creates an `ImageCache` from a customized `MemoryStorage` and `DiskStorage`.
  142. ///
  143. /// - Parameters:
  144. /// - memoryStorage: The `MemoryStorage.Backend` object to use in the image cache.
  145. /// - diskStorage: The `DiskStorage.Backend` object to use in the image cache.
  146. public init(
  147. memoryStorage: MemoryStorage.Backend<Image>,
  148. diskStorage: DiskStorage.Backend<Data>)
  149. {
  150. self.memoryStorage = memoryStorage
  151. self.diskStorage = diskStorage
  152. let ioQueueName = "com.onevcat.Kingfisher.ImageCache.ioQueue.\(UUID().uuidString)"
  153. ioQueue = DispatchQueue(label: ioQueueName)
  154. let notifications: [(Notification.Name, Selector)]
  155. #if !os(macOS) && !os(watchOS)
  156. #if swift(>=4.2)
  157. notifications = [
  158. (UIApplication.didReceiveMemoryWarningNotification, #selector(clearMemoryCache)),
  159. (UIApplication.willTerminateNotification, #selector(cleanExpiredDiskCache)),
  160. (UIApplication.didEnterBackgroundNotification, #selector(backgroundCleanExpiredDiskCache))
  161. ]
  162. #else
  163. notifications = [
  164. (NSNotification.Name.UIApplicationDidReceiveMemoryWarning, #selector(clearMemoryCache)),
  165. (NSNotification.Name.UIApplicationWillTerminate, #selector(cleanExpiredDiskCache)),
  166. (NSNotification.Name.UIApplicationDidEnterBackground, #selector(backgroundCleanExpiredDiskCache))
  167. ]
  168. #endif
  169. #elseif os(macOS)
  170. notifications = [
  171. (NSApplication.willResignActiveNotification, #selector(cleanExpiredDiskCache)),
  172. ]
  173. #else
  174. notifications = []
  175. #endif
  176. notifications.forEach {
  177. NotificationCenter.default.addObserver(self, selector: $0.1, name: $0.0, object: nil)
  178. }
  179. }
  180. /// Creates an `ImageCache` with a given `name`. Both `MemoryStorage` and `DiskStorage` will be created
  181. /// with a default config based on the `name`.
  182. ///
  183. /// - Parameter name: The name of cache object. It is used to setup disk cache directories and IO queue.
  184. /// You should not use the same `name` for different caches, otherwise, the disk storage would
  185. /// be conflicting to each other. The `name` should not be an empty string.
  186. public convenience init(name: String) {
  187. try! self.init(name: name, cacheDirectoryURL: nil, diskCachePathClosure: nil)
  188. }
  189. /// Creates an `ImageCache` with a given `name`, cache directory `path`
  190. /// and a closure to modify the cache directory.
  191. ///
  192. /// - Parameters:
  193. /// - name: The name of cache object. It is used to setup disk cache directories and IO queue.
  194. /// You should not use the same `name` for different caches, otherwise, the disk storage would
  195. /// be conflicting to each other.
  196. /// - cacheDirectoryURL: Location of cache directory URL on disk. It will be internally pass to the
  197. /// initializer of `DiskStorage` as the disk cache directory. If `nil`, the cache
  198. /// directory under user domain mask will be used.
  199. /// - diskCachePathClosure: Closure that takes in an optional initial path string and generates
  200. /// the final disk cache path. You could use it to fully customize your cache path.
  201. /// - Throws: An error that happens during image cache creating, such as unable to create a directory at the given
  202. /// path.
  203. public convenience init(
  204. name: String,
  205. cacheDirectoryURL: URL?,
  206. diskCachePathClosure: DiskCachePathClosure? = nil) throws
  207. {
  208. if name.isEmpty {
  209. fatalError("[Kingfisher] You should specify a name for the cache. A cache with empty name is not permitted.")
  210. }
  211. let totalMemory = ProcessInfo.processInfo.physicalMemory
  212. let costLimit = totalMemory / 4
  213. let memoryStorage = MemoryStorage.Backend<Image>(config:
  214. .init(totalCostLimit: (costLimit > Int.max) ? Int.max : Int(costLimit)))
  215. var diskConfig = DiskStorage.Config(
  216. name: name,
  217. sizeLimit: 0,
  218. directory: cacheDirectoryURL
  219. )
  220. if let closure = diskCachePathClosure {
  221. diskConfig.cachePathBlock = closure
  222. }
  223. let diskStorage = try DiskStorage.Backend<Data>(config: diskConfig)
  224. diskConfig.cachePathBlock = nil
  225. self.init(memoryStorage: memoryStorage, diskStorage: diskStorage)
  226. }
  227. deinit {
  228. NotificationCenter.default.removeObserver(self)
  229. }
  230. // MARK: Storing Images
  231. open func store(_ image: Image,
  232. original: Data? = nil,
  233. forKey key: String,
  234. options: KingfisherParsedOptionsInfo,
  235. toDisk: Bool = true,
  236. completionHandler: ((CacheStoreResult) -> Void)? = nil)
  237. {
  238. let identifier = options.processor.identifier
  239. let callbackQueue = options.callbackQueue
  240. let computedKey = key.computedKey(with: identifier)
  241. // Memory storage should not throw.
  242. memoryStorage.storeNoThrow(value: image, forKey: computedKey, expiration: options.memoryCacheExpiration)
  243. guard toDisk else {
  244. if let completionHandler = completionHandler {
  245. let result = CacheStoreResult(memoryCacheResult: .success(()), diskCacheResult: .success(()))
  246. callbackQueue.execute { completionHandler(result) }
  247. }
  248. return
  249. }
  250. ioQueue.async {
  251. let serializer = options.cacheSerializer
  252. if let data = serializer.data(with: image, original: original) {
  253. self.syncStoreToDisk(
  254. data,
  255. forKey: key,
  256. processorIdentifier: identifier,
  257. callbackQueue: callbackQueue,
  258. expiration: options.diskCacheExpiration,
  259. completionHandler: completionHandler)
  260. } else {
  261. guard let completionHandler = completionHandler else { return }
  262. let diskError = KingfisherError.cacheError(
  263. reason: .cannotSerializeImage(image: image, original: original, serializer: serializer))
  264. let result = CacheStoreResult(
  265. memoryCacheResult: .success(()),
  266. diskCacheResult: .failure(diskError))
  267. callbackQueue.execute { completionHandler(result) }
  268. }
  269. }
  270. }
  271. /// Stores an image to the cache.
  272. ///
  273. /// - Parameters:
  274. /// - image: The image to be stored.
  275. /// - original: The original data of the image. This value will be forwarded to the provided `serializer` for
  276. /// further use. By default, Kingfisher uses a `DefaultCacheSerializer` to serialize the image to
  277. /// data for caching in disk, it checks the image format based on `original` data to determine in
  278. /// which image format should be used. For other types of `serializer`, it depends on their
  279. /// implementation detail on how to use this original data.
  280. /// - key: The key used for caching the image.
  281. /// - identifier: The identifier of processor being used for caching. If you are using a processor for the
  282. /// image, pass the identifier of processor to this parameter.
  283. /// - serializer: The `CacheSerializer`
  284. /// - toDisk: Whether this image should be cached to disk or not. If `false`, the image is only cached in memory.
  285. /// Otherwise, it is cached in both memory storage and disk storage. Default is `true`.
  286. /// - callbackQueue: The callback queue on which `completionHandler` is invoked. Default is `.untouch`. For case
  287. /// that `toDisk` is `false`, a `.untouch` queue means `callbackQueue` will be invoked from the
  288. /// caller queue of this method. If `toDisk` is `true`, the `completionHandler` will be called
  289. /// from an internal file IO queue. To change this behavior, specify another `CallbackQueue`
  290. /// value.
  291. /// - completionHandler: A closure which is invoked when the cache operation finishes.
  292. open func store(_ image: Image,
  293. original: Data? = nil,
  294. forKey key: String,
  295. processorIdentifier identifier: String = "",
  296. cacheSerializer serializer: CacheSerializer = DefaultCacheSerializer.default,
  297. toDisk: Bool = true,
  298. callbackQueue: CallbackQueue = .untouch,
  299. completionHandler: ((CacheStoreResult) -> Void)? = nil)
  300. {
  301. struct TempProcessor: ImageProcessor {
  302. let identifier: String
  303. func process(item: ImageProcessItem, options: KingfisherParsedOptionsInfo) -> Image? {
  304. return nil
  305. }
  306. }
  307. let options = KingfisherParsedOptionsInfo([
  308. .processor(TempProcessor(identifier: identifier)),
  309. .cacheSerializer(serializer),
  310. .callbackQueue(callbackQueue)
  311. ])
  312. store(image, original: original, forKey: key, options: options,
  313. toDisk: toDisk, completionHandler: completionHandler)
  314. }
  315. open func storeToDisk(
  316. _ data: Data,
  317. forKey key: String,
  318. processorIdentifier identifier: String = "",
  319. expiration: StorageExpiration? = nil,
  320. callbackQueue: CallbackQueue = .untouch,
  321. completionHandler: ((CacheStoreResult) -> Void)? = nil)
  322. {
  323. ioQueue.async {
  324. self.syncStoreToDisk(
  325. data,
  326. forKey: key,
  327. processorIdentifier: identifier,
  328. callbackQueue: callbackQueue,
  329. expiration: expiration,
  330. completionHandler: completionHandler)
  331. }
  332. }
  333. private func syncStoreToDisk(
  334. _ data: Data,
  335. forKey key: String,
  336. processorIdentifier identifier: String = "",
  337. callbackQueue: CallbackQueue = .untouch,
  338. expiration: StorageExpiration? = nil,
  339. completionHandler: ((CacheStoreResult) -> Void)? = nil)
  340. {
  341. let computedKey = key.computedKey(with: identifier)
  342. let result: CacheStoreResult
  343. do {
  344. try self.diskStorage.store(value: data, forKey: computedKey, expiration: expiration)
  345. result = CacheStoreResult(memoryCacheResult: .success(()), diskCacheResult: .success(()))
  346. } catch {
  347. let diskError: KingfisherError
  348. if let error = error as? KingfisherError {
  349. diskError = error
  350. } else {
  351. diskError = .cacheError(reason: .cannotConvertToData(object: data, error: error))
  352. }
  353. result = CacheStoreResult(
  354. memoryCacheResult: .success(()),
  355. diskCacheResult: .failure(diskError)
  356. )
  357. }
  358. if let completionHandler = completionHandler {
  359. callbackQueue.execute { completionHandler(result) }
  360. }
  361. }
  362. // MARK: Removing Images
  363. /// Removes the image for the given key from the cache.
  364. ///
  365. /// - Parameters:
  366. /// - key: The key used for caching the image.
  367. /// - identifier: The identifier of processor being used for caching. If you are using a processor for the
  368. /// image, pass the identifier of processor to this parameter.
  369. /// - fromMemory: Whether this image should be removed from memory storage or not.
  370. /// If `false`, the image won't be removed from the memory storage. Default is `true`.
  371. /// - fromDisk: Whether this image should be removed from disk storage or not.
  372. /// If `false`, the image won't be removed from the disk storage. Default is `true`.
  373. /// - callbackQueue: The callback queue on which `completionHandler` is invoked. Default is `.untouch`.
  374. /// - completionHandler: A closure which is invoked when the cache removing operation finishes.
  375. open func removeImage(forKey key: String,
  376. processorIdentifier identifier: String = "",
  377. fromMemory: Bool = true,
  378. fromDisk: Bool = true,
  379. callbackQueue: CallbackQueue = .untouch,
  380. completionHandler: (() -> Void)? = nil)
  381. {
  382. let computedKey = key.computedKey(with: identifier)
  383. if fromMemory {
  384. try? memoryStorage.remove(forKey: computedKey)
  385. }
  386. if fromDisk {
  387. ioQueue.async{
  388. try? self.diskStorage.remove(forKey: computedKey)
  389. if let completionHandler = completionHandler {
  390. callbackQueue.execute { completionHandler() }
  391. }
  392. }
  393. } else {
  394. if let completionHandler = completionHandler {
  395. callbackQueue.execute { completionHandler() }
  396. }
  397. }
  398. }
  399. func retrieveImage(forKey key: String,
  400. options: KingfisherParsedOptionsInfo,
  401. callbackQueue: CallbackQueue = .untouch,
  402. completionHandler: ((Result<ImageCacheResult, KingfisherError>) -> Void)?)
  403. {
  404. // No completion handler. No need to start working and early return.
  405. guard let completionHandler = completionHandler else { return }
  406. // Try to check the image from memory cache first.
  407. if let image = retrieveImageInMemoryCache(forKey: key, options: options) {
  408. let image = options.imageModifier?.modify(image) ?? image
  409. callbackQueue.execute { completionHandler(.success(.memory(image))) }
  410. } else if options.fromMemoryCacheOrRefresh {
  411. callbackQueue.execute { completionHandler(.success(.none)) }
  412. } else {
  413. // Begin to disk search.
  414. self.retrieveImageInDiskCache(forKey: key, options: options, callbackQueue: callbackQueue) {
  415. result in
  416. // The callback queue is already correct in this closure.
  417. switch result {
  418. case .success(let image):
  419. guard let image = image else {
  420. // No image found in disk storage.
  421. completionHandler(.success(.none))
  422. return
  423. }
  424. let finalImage = options.imageModifier?.modify(image) ?? image
  425. // Cache the disk image to memory.
  426. // We are passing `false` to `toDisk`, the memory cache does not change
  427. // callback queue, we can call `completionHandler` without another dispatch.
  428. var cacheOptions = options
  429. cacheOptions.callbackQueue = .untouch
  430. self.store(
  431. finalImage,
  432. forKey: key,
  433. options: cacheOptions,
  434. toDisk: false)
  435. {
  436. _ in
  437. completionHandler(.success(.disk(finalImage)))
  438. }
  439. case .failure(let error):
  440. completionHandler(.failure(error))
  441. }
  442. }
  443. }
  444. }
  445. // MARK: Getting Images
  446. /// Gets an image for a given key from the cache, either from memory storage or disk storage.
  447. ///
  448. /// - Parameters:
  449. /// - key: The key used for caching the image.
  450. /// - options: The `KingfisherOptionsInfo` options setting used for retrieving the image.
  451. /// - callbackQueue: The callback queue on which `completionHandler` is invoked. Default is `.untouch`.
  452. /// - completionHandler: A closure which is invoked when the image getting operation finishes. If the
  453. /// image retrieving operation finishes without problem, an `ImageCacheResult` value
  454. /// will be sent to this closure as result. Otherwise, a `KingfisherError` result
  455. /// with detail failing reason will be sent.
  456. open func retrieveImage(forKey key: String,
  457. options: KingfisherOptionsInfo? = nil,
  458. callbackQueue: CallbackQueue = .untouch,
  459. completionHandler: ((Result<ImageCacheResult, KingfisherError>) -> Void)?)
  460. {
  461. retrieveImage(
  462. forKey: key,
  463. options: KingfisherParsedOptionsInfo(options),
  464. callbackQueue: callbackQueue,
  465. completionHandler: completionHandler)
  466. }
  467. func retrieveImageInMemoryCache(
  468. forKey key: String,
  469. options: KingfisherParsedOptionsInfo) -> Image?
  470. {
  471. let computedKey = key.computedKey(with: options.processor.identifier)
  472. do {
  473. return try memoryStorage.value(forKey: computedKey)
  474. } catch {
  475. return nil
  476. }
  477. }
  478. /// Gets an image for a given key from the memory storage.
  479. ///
  480. /// - Parameters:
  481. /// - key: The key used for caching the image.
  482. /// - options: The `KingfisherOptionsInfo` options setting used for retrieving the image.
  483. /// - Returns: The image stored in memory cache, if exists and valid. Otherwise, if the image does not exist or
  484. /// has already expired, `nil` is returned.
  485. open func retrieveImageInMemoryCache(
  486. forKey key: String,
  487. options: KingfisherOptionsInfo? = nil) -> Image?
  488. {
  489. return retrieveImageInMemoryCache(forKey: key, options: KingfisherParsedOptionsInfo(options))
  490. }
  491. func retrieveImageInDiskCache(
  492. forKey key: String,
  493. options: KingfisherParsedOptionsInfo,
  494. callbackQueue: CallbackQueue = .untouch,
  495. completionHandler: @escaping (Result<Image?, KingfisherError>) -> Void)
  496. {
  497. let computedKey = key.computedKey(with: options.processor.identifier)
  498. let loadingQueue: CallbackQueue = options.loadDiskFileSynchronously ? .untouch : .dispatch(ioQueue)
  499. loadingQueue.execute {
  500. do {
  501. var image: Image? = nil
  502. if let data = try self.diskStorage.value(forKey: computedKey) {
  503. image = options.cacheSerializer.image(with: data, options: options)
  504. }
  505. callbackQueue.execute { completionHandler(.success(image)) }
  506. } catch {
  507. if let error = error as? KingfisherError {
  508. callbackQueue.execute { completionHandler(.failure(error)) }
  509. } else {
  510. assertionFailure("The internal thrown error should be a `KingfisherError`.")
  511. }
  512. }
  513. }
  514. }
  515. /// Gets an image for a given key from the disk storage.
  516. ///
  517. /// - Parameters:
  518. /// - key: The key used for caching the image.
  519. /// - options: The `KingfisherOptionsInfo` options setting used for retrieving the image.
  520. /// - callbackQueue: The callback queue on which `completionHandler` is invoked. Default is `.untouch`.
  521. /// - completionHandler: A closure which is invoked when the operation finishes.
  522. open func retrieveImageInDiskCache(
  523. forKey key: String,
  524. options: KingfisherOptionsInfo? = nil,
  525. callbackQueue: CallbackQueue = .untouch,
  526. completionHandler: @escaping (Result<Image?, KingfisherError>) -> Void)
  527. {
  528. retrieveImageInDiskCache(
  529. forKey: key,
  530. options: KingfisherParsedOptionsInfo(options),
  531. callbackQueue: callbackQueue,
  532. completionHandler: completionHandler)
  533. }
  534. // MARK: Cleaning
  535. /// Clears the memory storage of this cache.
  536. @objc public func clearMemoryCache() {
  537. try? memoryStorage.removeAll()
  538. }
  539. /// Clears the disk storage of this cache. This is an async operation.
  540. ///
  541. /// - Parameter handler: A closure which is invoked when the cache clearing operation finishes.
  542. /// This `handler` will be called from the main queue.
  543. open func clearDiskCache(completion handler: (()->())? = nil) {
  544. ioQueue.async {
  545. do {
  546. try self.diskStorage.removeAll()
  547. } catch _ { }
  548. if let handler = handler {
  549. DispatchQueue.main.async { handler() }
  550. }
  551. }
  552. }
  553. /// Clears the expired images from disk storage. This is an async operation.
  554. open func cleanExpiredMemoryCache() {
  555. memoryStorage.removeExpired()
  556. }
  557. /// Clears the expired images from disk storage. This is an async operation.
  558. @objc func cleanExpiredDiskCache() {
  559. cleanExpiredDiskCache(completion: nil)
  560. }
  561. /// Clears the expired images from disk storage. This is an async operation.
  562. ///
  563. /// - Parameter handler: A closure which is invoked when the cache clearing operation finishes.
  564. /// This `handler` will be called from the main queue.
  565. open func cleanExpiredDiskCache(completion handler: (() -> Void)? = nil) {
  566. ioQueue.async {
  567. do {
  568. var removed: [URL] = []
  569. let removedExpired = try self.diskStorage.removeExpiredValues()
  570. removed.append(contentsOf: removedExpired)
  571. let removedSizeExceeded = try self.diskStorage.removeSizeExceededValues()
  572. removed.append(contentsOf: removedSizeExceeded)
  573. if !removed.isEmpty {
  574. DispatchQueue.main.async {
  575. let cleanedHashes = removed.map { $0.lastPathComponent }
  576. NotificationCenter.default.post(
  577. name: .KingfisherDidCleanDiskCache,
  578. object: self,
  579. userInfo: [KingfisherDiskCacheCleanedHashKey: cleanedHashes])
  580. }
  581. }
  582. if let handler = handler {
  583. DispatchQueue.main.async { handler() }
  584. }
  585. } catch {}
  586. }
  587. }
  588. #if !os(macOS) && !os(watchOS)
  589. /// Clears the expired images from disk storage when app is in background. This is an async operation.
  590. /// In most cases, you should not call this method explicitly.
  591. /// It will be called automatically when `UIApplicationDidEnterBackgroundNotification` received.
  592. @objc public func backgroundCleanExpiredDiskCache() {
  593. // if 'sharedApplication()' is unavailable, then return
  594. guard let sharedApplication = KingfisherWrapper<UIApplication>.shared else { return }
  595. func endBackgroundTask(_ task: inout UIBackgroundTaskIdentifier) {
  596. sharedApplication.endBackgroundTask(task)
  597. #if swift(>=4.2)
  598. task = UIBackgroundTaskIdentifier.invalid
  599. #else
  600. task = UIBackgroundTaskInvalid
  601. #endif
  602. }
  603. var backgroundTask: UIBackgroundTaskIdentifier!
  604. backgroundTask = sharedApplication.beginBackgroundTask {
  605. endBackgroundTask(&backgroundTask!)
  606. }
  607. cleanExpiredDiskCache {
  608. endBackgroundTask(&backgroundTask!)
  609. }
  610. }
  611. #endif
  612. // MARK: Image Cache State
  613. /// Returns the cache type for a given `key` and `identifier` combination.
  614. /// This method is used for checking whether an image is cached in current cache.
  615. /// It also provides information on which kind of cache can it be found in the return value.
  616. ///
  617. /// - Parameters:
  618. /// - key: The key used for caching the image.
  619. /// - identifier: Processor identifier which used for this image. Default is the `identifier` of
  620. /// `DefaultImageProcessor.default`.
  621. /// - Returns: A `CacheType` instance which indicates the cache status.
  622. /// `.none` means the image is not in cache or it is already expired.
  623. open func imageCachedType(
  624. forKey key: String,
  625. processorIdentifier identifier: String = DefaultImageProcessor.default.identifier) -> CacheType
  626. {
  627. let computedKey = key.computedKey(with: identifier)
  628. if memoryStorage.isCached(forKey: computedKey) { return .memory }
  629. if diskStorage.isCached(forKey: computedKey) { return .disk }
  630. return .none
  631. }
  632. /// Returns whether the file exists in cache for a given `key` and `identifier` combination.
  633. ///
  634. /// - Parameters:
  635. /// - key: The key used for caching the image.
  636. /// - identifier: Processor identifier which used for this image. Default is the `identifier` of
  637. /// `DefaultImageProcessor.default`.
  638. /// - Returns: A `Bool` which indicates whether a cache could match the given `key` and `identifier` combination.
  639. ///
  640. /// - Note:
  641. /// The return value does not contain information about from which kind of storage the cache matches.
  642. /// To get the information about cache type according `CacheType`,
  643. /// use `imageCachedType(forKey:processorIdentifier:)` instead.
  644. public func isCached(
  645. forKey key: String,
  646. processorIdentifier identifier: String = DefaultImageProcessor.default.identifier) -> Bool
  647. {
  648. return imageCachedType(forKey: key, processorIdentifier: identifier).cached
  649. }
  650. /// Gets the hash used as cache file name for the key.
  651. ///
  652. /// - Parameters:
  653. /// - key: The key used for caching the image.
  654. /// - identifier: Processor identifier which used for this image. Default is the `identifier` of
  655. /// `DefaultImageProcessor.default`.
  656. /// - Returns: The hash which is used as the cache file name.
  657. ///
  658. /// - Note:
  659. /// By default, for a given combination of `key` and `identifier`, `ImageCache` will use the value
  660. /// returned by this method as the cache file name. You can use this value to check and match cache file
  661. /// if you need.
  662. open func hash(
  663. forKey key: String,
  664. processorIdentifier identifier: String = DefaultImageProcessor.default.identifier) -> String
  665. {
  666. let computedKey = key.computedKey(with: identifier)
  667. return diskStorage.cacheFileName(forKey: computedKey)
  668. }
  669. /// Calculates the size taken by the disk storage.
  670. /// It is the total file size of all cached files in the `diskStorage` on disk in bytes.
  671. ///
  672. /// - Parameter handler: Called with the size calculating finishes. This closure is invoked from the main queue.
  673. open func calculateDiskStorageSize(completion handler: @escaping ((Result<UInt, KingfisherError>) -> Void)) {
  674. ioQueue.async {
  675. do {
  676. let size = try self.diskStorage.totalSize()
  677. DispatchQueue.main.async { handler(.success(size)) }
  678. } catch {
  679. if let error = error as? KingfisherError {
  680. DispatchQueue.main.async { handler(.failure(error)) }
  681. } else {
  682. assertionFailure("The internal thrown error should be a `KingfisherError`.")
  683. }
  684. }
  685. }
  686. }
  687. /// Gets the cache path for the key.
  688. /// It is useful for projects with web view or anyone that needs access to the local file path.
  689. ///
  690. /// i.e. Replacing the `<img src='path_for_key'>` tag in your HTML.
  691. ///
  692. /// - Parameters:
  693. /// - key: The key used for caching the image.
  694. /// - identifier: Processor identifier which used for this image. Default is the `identifier` of
  695. /// `DefaultImageProcessor.default`.
  696. /// - Returns: The disk path of cached image under the given `key` and `identifier`.
  697. ///
  698. /// - Note:
  699. /// This method does not guarantee there is an image already cached in the returned path. It just gives your
  700. /// the path that the image should be, if it exists in disk storage.
  701. ///
  702. /// You could use `isCached(forKey:)` method to check whether the image is cached under that key in disk.
  703. open func cachePath(
  704. forKey key: String,
  705. processorIdentifier identifier: String = DefaultImageProcessor.default.identifier) -> String
  706. {
  707. let computedKey = key.computedKey(with: identifier)
  708. return diskStorage.cacheFileURL(forKey: computedKey).path
  709. }
  710. }
  711. extension Dictionary {
  712. func keysSortedByValue(_ isOrderedBefore: (Value, Value) -> Bool) -> [Key] {
  713. return Array(self).sorted{ isOrderedBefore($0.1, $1.1) }.map{ $0.0 }
  714. }
  715. }
  716. #if !os(macOS) && !os(watchOS)
  717. // MARK: - For App Extensions
  718. extension UIApplication: KingfisherCompatible { }
  719. extension KingfisherWrapper where Base: UIApplication {
  720. public static var shared: UIApplication? {
  721. let selector = NSSelectorFromString("sharedApplication")
  722. guard Base.responds(to: selector) else { return nil }
  723. return Base.perform(selector).takeUnretainedValue() as? UIApplication
  724. }
  725. }
  726. #endif
  727. extension String {
  728. func computedKey(with identifier: String) -> String {
  729. if identifier.isEmpty {
  730. return self
  731. } else {
  732. return appending("@\(identifier)")
  733. }
  734. }
  735. }
  736. extension ImageCache {
  737. /// Creates an `ImageCache` with a given `name`, cache directory `path`
  738. /// and a closure to modify the cache directory.
  739. ///
  740. /// - Parameters:
  741. /// - name: The name of cache object. It is used to setup disk cache directories and IO queue.
  742. /// You should not use the same `name` for different caches, otherwise, the disk storage would
  743. /// be conflicting to each other.
  744. /// - path: Location of cache URL on disk. It will be internally pass to the initializer of `DiskStorage` as the
  745. /// disk cache directory.
  746. /// - diskCachePathClosure: Closure that takes in an optional initial path string and generates
  747. /// the final disk cache path. You could use it to fully customize your cache path.
  748. /// - Throws: An error that happens during image cache creating, such as unable to create a directory at the given
  749. /// path.
  750. @available(*, deprecated, message: "Use `init(name:cacheDirectoryURL:diskCachePathClosure:)` instead",
  751. renamed: "init(name:cacheDirectoryURL:diskCachePathClosure:)")
  752. public convenience init(
  753. name: String,
  754. path: String?,
  755. diskCachePathClosure: DiskCachePathClosure? = nil) throws
  756. {
  757. let directoryURL = path.flatMap { URL(string: $0) }
  758. try self.init(name: name, cacheDirectoryURL: directoryURL, diskCachePathClosure: diskCachePathClosure)
  759. }
  760. }