DiskStorage.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. //
  2. // DiskStorage.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 2018/10/15.
  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. import Foundation
  27. /// Represents a set of conception related to storage which stores a certain type of value in disk.
  28. /// This is a namespace for the disk storage types. A `Backend` with a certain `Config` will be used to describe the
  29. /// storage. See these composed types for more information.
  30. public enum DiskStorage {
  31. /// Represents a storage back-end for the `DiskStorage`. The value is serialized to data
  32. /// and stored as file in the file system under a specified location.
  33. ///
  34. /// You can config a `DiskStorage.Backend` in its initializer by passing a `DiskStorage.Config` value.
  35. /// or modifying the `config` property after it being created. `DiskStorage` will use file's attributes to keep
  36. /// track of a file for its expiration or size limitation.
  37. public class Backend<T: DataTransformable> {
  38. /// The config used for this disk storage.
  39. public var config: Config
  40. // The final storage URL on disk, with `name` and `cachePathBlock` considered.
  41. public let directoryURL: URL
  42. let metaChangingQueue: DispatchQueue
  43. var maybeCached : Set<String>?
  44. let maybeCachedCheckingQueue = DispatchQueue(label: "com.onevcat.Kingfisher.maybeCachedCheckingQueue")
  45. // `false` if the storage initialized with an error. This prevents unexpected forcibly crash when creating
  46. // storage in the default cache.
  47. private var storageReady: Bool = true
  48. /// Creates a disk storage with the given `DiskStorage.Config`.
  49. ///
  50. /// - Parameter config: The config used for this disk storage.
  51. /// - Throws: An error if the folder for storage cannot be got or created.
  52. public convenience init(config: Config) throws {
  53. self.init(noThrowConfig: config, creatingDirectory: false)
  54. try prepareDirectory()
  55. }
  56. // If `creatingDirectory` is `false`, the directory preparation will be skipped.
  57. // We need to call `prepareDirectory` manually after this returns.
  58. init(noThrowConfig config: Config, creatingDirectory: Bool) {
  59. var config = config
  60. let creation = Creation(config)
  61. self.directoryURL = creation.directoryURL
  62. // Break any possible retain cycle set by outside.
  63. config.cachePathBlock = nil
  64. self.config = config
  65. metaChangingQueue = DispatchQueue(label: creation.cacheName)
  66. setupCacheChecking()
  67. if creatingDirectory {
  68. try? prepareDirectory()
  69. }
  70. }
  71. private func setupCacheChecking() {
  72. maybeCachedCheckingQueue.async {
  73. do {
  74. self.maybeCached = Set()
  75. try self.config.fileManager.contentsOfDirectory(atPath: self.directoryURL.path).forEach { fileName in
  76. self.maybeCached?.insert(fileName)
  77. }
  78. } catch {
  79. // Just disable the functionality if we fail to initialize it properly. This will just revert to
  80. // the behavior which is to check file existence on disk directly.
  81. self.maybeCached = nil
  82. }
  83. }
  84. }
  85. // Creates the storage folder.
  86. private func prepareDirectory() throws {
  87. let fileManager = config.fileManager
  88. let path = directoryURL.path
  89. guard !fileManager.fileExists(atPath: path) else { return }
  90. do {
  91. try fileManager.createDirectory(
  92. atPath: path,
  93. withIntermediateDirectories: true,
  94. attributes: nil)
  95. } catch {
  96. self.storageReady = false
  97. throw KingfisherError.cacheError(reason: .cannotCreateDirectory(path: path, error: error))
  98. }
  99. }
  100. /// Stores a value to the storage under the specified key and expiration policy.
  101. /// - Parameters:
  102. /// - value: The value to be stored.
  103. /// - key: The key to which the `value` will be stored. If there is already a value under the key,
  104. /// the old value will be overwritten by `value`.
  105. /// - expiration: The expiration policy used by this store action.
  106. /// - writeOptions: Data writing options used the new files.
  107. /// - Throws: An error during converting the value to a data format or during writing it to disk.
  108. public func store(
  109. value: T,
  110. forKey key: String,
  111. expiration: StorageExpiration? = nil,
  112. writeOptions: Data.WritingOptions = []) throws
  113. {
  114. guard storageReady else {
  115. throw KingfisherError.cacheError(reason: .diskStorageIsNotReady(cacheURL: directoryURL))
  116. }
  117. let expiration = expiration ?? config.expiration
  118. // The expiration indicates that already expired, no need to store.
  119. guard !expiration.isExpired else { return }
  120. let data: Data
  121. do {
  122. data = try value.toData()
  123. } catch {
  124. throw KingfisherError.cacheError(reason: .cannotConvertToData(object: value, error: error))
  125. }
  126. let fileURL = cacheFileURL(forKey: key)
  127. do {
  128. try data.write(to: fileURL, options: writeOptions)
  129. } catch {
  130. if error.isFolderMissing {
  131. // The whole cache folder is deleted. Try to recreate it and write file again.
  132. do {
  133. try prepareDirectory()
  134. try data.write(to: fileURL, options: writeOptions)
  135. } catch {
  136. throw KingfisherError.cacheError(
  137. reason: .cannotCreateCacheFile(fileURL: fileURL, key: key, data: data, error: error)
  138. )
  139. }
  140. } else {
  141. throw KingfisherError.cacheError(
  142. reason: .cannotCreateCacheFile(fileURL: fileURL, key: key, data: data, error: error)
  143. )
  144. }
  145. }
  146. let now = Date()
  147. let attributes: [FileAttributeKey : Any] = [
  148. // The last access date.
  149. .creationDate: now.fileAttributeDate,
  150. // The estimated expiration date.
  151. .modificationDate: expiration.estimatedExpirationSinceNow.fileAttributeDate
  152. ]
  153. do {
  154. try config.fileManager.setAttributes(attributes, ofItemAtPath: fileURL.path)
  155. } catch {
  156. try? config.fileManager.removeItem(at: fileURL)
  157. throw KingfisherError.cacheError(
  158. reason: .cannotSetCacheFileAttribute(
  159. filePath: fileURL.path,
  160. attributes: attributes,
  161. error: error
  162. )
  163. )
  164. }
  165. maybeCachedCheckingQueue.async {
  166. self.maybeCached?.insert(fileURL.lastPathComponent)
  167. }
  168. }
  169. /// Gets a value from the storage.
  170. /// - Parameters:
  171. /// - key: The cache key of value.
  172. /// - extendingExpiration: The expiration policy used by this getting action.
  173. /// - Throws: An error during converting the data to a value or during operation of disk files.
  174. /// - Returns: The value under `key` if it is valid and found in the storage. Otherwise, `nil`.
  175. public func value(forKey key: String, extendingExpiration: ExpirationExtending = .cacheTime) throws -> T? {
  176. return try value(forKey: key, referenceDate: Date(), actuallyLoad: true, extendingExpiration: extendingExpiration)
  177. }
  178. func value(
  179. forKey key: String,
  180. referenceDate: Date,
  181. actuallyLoad: Bool,
  182. extendingExpiration: ExpirationExtending) throws -> T?
  183. {
  184. guard storageReady else {
  185. throw KingfisherError.cacheError(reason: .diskStorageIsNotReady(cacheURL: directoryURL))
  186. }
  187. let fileManager = config.fileManager
  188. let fileURL = cacheFileURL(forKey: key)
  189. let filePath = fileURL.path
  190. let fileMaybeCached = maybeCachedCheckingQueue.sync {
  191. return maybeCached?.contains(fileURL.lastPathComponent) ?? true
  192. }
  193. guard fileMaybeCached else {
  194. return nil
  195. }
  196. guard fileManager.fileExists(atPath: filePath) else {
  197. return nil
  198. }
  199. let meta: FileMeta
  200. do {
  201. let resourceKeys: Set<URLResourceKey> = [.contentModificationDateKey, .creationDateKey]
  202. meta = try FileMeta(fileURL: fileURL, resourceKeys: resourceKeys)
  203. } catch {
  204. throw KingfisherError.cacheError(
  205. reason: .invalidURLResource(error: error, key: key, url: fileURL))
  206. }
  207. if meta.expired(referenceDate: referenceDate) {
  208. return nil
  209. }
  210. if !actuallyLoad { return T.empty }
  211. do {
  212. let data = try Data(contentsOf: fileURL)
  213. let obj = try T.fromData(data)
  214. metaChangingQueue.async {
  215. meta.extendExpiration(with: fileManager, extendingExpiration: extendingExpiration)
  216. }
  217. return obj
  218. } catch {
  219. throw KingfisherError.cacheError(reason: .cannotLoadDataFromDisk(url: fileURL, error: error))
  220. }
  221. }
  222. /// Whether there is valid cached data under a given key.
  223. /// - Parameter key: The cache key of value.
  224. /// - Returns: If there is valid data under the key, `true`. Otherwise, `false`.
  225. ///
  226. /// - Note:
  227. /// This method does not actually load the data from disk, so it is faster than directly loading the cached value
  228. /// by checking the nullability of `value(forKey:extendingExpiration:)` method.
  229. ///
  230. public func isCached(forKey key: String) -> Bool {
  231. return isCached(forKey: key, referenceDate: Date())
  232. }
  233. /// Whether there is valid cached data under a given key and a reference date.
  234. /// - Parameters:
  235. /// - key: The cache key of value.
  236. /// - referenceDate: A reference date to check whether the cache is still valid.
  237. /// - Returns: If there is valid data under the key, `true`. Otherwise, `false`.
  238. ///
  239. /// - Note:
  240. /// If you pass `Date()` to `referenceDate`, this method is identical to `isCached(forKey:)`. Use the
  241. /// `referenceDate` to determine whether the cache is still valid for a future date.
  242. public func isCached(forKey key: String, referenceDate: Date) -> Bool {
  243. do {
  244. let result = try value(
  245. forKey: key,
  246. referenceDate: referenceDate,
  247. actuallyLoad: false,
  248. extendingExpiration: .none
  249. )
  250. return result != nil
  251. } catch {
  252. return false
  253. }
  254. }
  255. /// Removes a value from a specified key.
  256. /// - Parameter key: The cache key of value.
  257. /// - Throws: An error during removing the value.
  258. public func remove(forKey key: String) throws {
  259. let fileURL = cacheFileURL(forKey: key)
  260. try removeFile(at: fileURL)
  261. }
  262. func removeFile(at url: URL) throws {
  263. try config.fileManager.removeItem(at: url)
  264. }
  265. /// Removes all values in this storage.
  266. /// - Throws: An error during removing the values.
  267. public func removeAll() throws {
  268. try removeAll(skipCreatingDirectory: false)
  269. }
  270. func removeAll(skipCreatingDirectory: Bool) throws {
  271. try config.fileManager.removeItem(at: directoryURL)
  272. if !skipCreatingDirectory {
  273. try prepareDirectory()
  274. }
  275. }
  276. /// The URL of the cached file with a given computed `key`.
  277. ///
  278. /// - Parameter key: The final computed key used when caching the image. Please note that usually this is not
  279. /// the `cacheKey` of an image `Source`. It is the computed key with processor identifier considered.
  280. ///
  281. /// - Note:
  282. /// This method does not guarantee there is an image already cached in the returned URL. It just gives your
  283. /// the URL that the image should be if it exists in disk storage, with the give key.
  284. ///
  285. public func cacheFileURL(forKey key: String) -> URL {
  286. let fileName = cacheFileName(forKey: key)
  287. return directoryURL.appendingPathComponent(fileName, isDirectory: false)
  288. }
  289. func cacheFileName(forKey key: String) -> String {
  290. if config.usesHashedFileName {
  291. let hashedKey = key.kf.md5
  292. if let ext = config.pathExtension {
  293. return "\(hashedKey).\(ext)"
  294. } else if config.autoExtAfterHashedFileName,
  295. let ext = key.kf.ext {
  296. return "\(hashedKey).\(ext)"
  297. }
  298. return hashedKey
  299. } else {
  300. if let ext = config.pathExtension {
  301. return "\(key).\(ext)"
  302. }
  303. return key
  304. }
  305. }
  306. func allFileURLs(for propertyKeys: [URLResourceKey]) throws -> [URL] {
  307. let fileManager = config.fileManager
  308. guard let directoryEnumerator = fileManager.enumerator(
  309. at: directoryURL, includingPropertiesForKeys: propertyKeys, options: .skipsHiddenFiles) else
  310. {
  311. throw KingfisherError.cacheError(reason: .fileEnumeratorCreationFailed(url: directoryURL))
  312. }
  313. guard let urls = directoryEnumerator.allObjects as? [URL] else {
  314. throw KingfisherError.cacheError(reason: .invalidFileEnumeratorContent(url: directoryURL))
  315. }
  316. return urls
  317. }
  318. /// Removes all expired values from this storage.
  319. /// - Throws: A file manager error during removing the file.
  320. /// - Returns: The URLs for removed files.
  321. public func removeExpiredValues() throws -> [URL] {
  322. return try removeExpiredValues(referenceDate: Date())
  323. }
  324. func removeExpiredValues(referenceDate: Date) throws -> [URL] {
  325. let propertyKeys: [URLResourceKey] = [
  326. .isDirectoryKey,
  327. .contentModificationDateKey
  328. ]
  329. let urls = try allFileURLs(for: propertyKeys)
  330. let keys = Set(propertyKeys)
  331. let expiredFiles = urls.filter { fileURL in
  332. do {
  333. let meta = try FileMeta(fileURL: fileURL, resourceKeys: keys)
  334. if meta.isDirectory {
  335. return false
  336. }
  337. return meta.expired(referenceDate: referenceDate)
  338. } catch {
  339. return true
  340. }
  341. }
  342. try expiredFiles.forEach { url in
  343. try removeFile(at: url)
  344. }
  345. return expiredFiles
  346. }
  347. /// Removes all size exceeded values from this storage.
  348. /// - Throws: A file manager error during removing the file.
  349. /// - Returns: The URLs for removed files.
  350. ///
  351. /// - Note: This method checks `config.sizeLimit` and remove cached files in an LRU (Least Recently Used) way.
  352. func removeSizeExceededValues() throws -> [URL] {
  353. if config.sizeLimit == 0 { return [] } // Back compatible. 0 means no limit.
  354. var size = try totalSize()
  355. if size < config.sizeLimit { return [] }
  356. let propertyKeys: [URLResourceKey] = [
  357. .isDirectoryKey,
  358. .creationDateKey,
  359. .fileSizeKey
  360. ]
  361. let keys = Set(propertyKeys)
  362. let urls = try allFileURLs(for: propertyKeys)
  363. var pendings: [FileMeta] = urls.compactMap { fileURL in
  364. guard let meta = try? FileMeta(fileURL: fileURL, resourceKeys: keys) else {
  365. return nil
  366. }
  367. return meta
  368. }
  369. // Sort by last access date. Most recent file first.
  370. pendings.sort(by: FileMeta.lastAccessDate)
  371. var removed: [URL] = []
  372. let target = config.sizeLimit / 2
  373. while size > target, let meta = pendings.popLast() {
  374. size -= UInt(meta.fileSize)
  375. try removeFile(at: meta.url)
  376. removed.append(meta.url)
  377. }
  378. return removed
  379. }
  380. /// Gets the total file size of the folder in bytes.
  381. public func totalSize() throws -> UInt {
  382. let propertyKeys: [URLResourceKey] = [.fileSizeKey]
  383. let urls = try allFileURLs(for: propertyKeys)
  384. let keys = Set(propertyKeys)
  385. let totalSize: UInt = urls.reduce(0) { size, fileURL in
  386. do {
  387. let meta = try FileMeta(fileURL: fileURL, resourceKeys: keys)
  388. return size + UInt(meta.fileSize)
  389. } catch {
  390. return size
  391. }
  392. }
  393. return totalSize
  394. }
  395. }
  396. }
  397. extension DiskStorage {
  398. /// Represents the config used in a `DiskStorage`.
  399. public struct Config {
  400. /// The file size limit on disk of the storage in bytes. 0 means no limit.
  401. public var sizeLimit: UInt
  402. /// The `StorageExpiration` used in this disk storage. Default is `.days(7)`,
  403. /// means that the disk cache would expire in one week.
  404. public var expiration: StorageExpiration = .days(7)
  405. /// The preferred extension of cache item. It will be appended to the file name as its extension.
  406. /// Default is `nil`, means that the cache file does not contain a file extension.
  407. public var pathExtension: String? = nil
  408. /// Default is `true`, means that the cache file name will be hashed before storing.
  409. public var usesHashedFileName = true
  410. /// Default is `false`
  411. /// If set to `true`, image extension will be extracted from original file name and append to
  412. /// the hased file name and used as the cache key on disk.
  413. public var autoExtAfterHashedFileName = false
  414. /// Closure that takes in initial directory path and generates
  415. /// the final disk cache path. You can use it to fully customize your cache path.
  416. public var cachePathBlock: ((_ directory: URL, _ cacheName: String) -> URL)! = {
  417. (directory, cacheName) in
  418. return directory.appendingPathComponent(cacheName, isDirectory: true)
  419. }
  420. let name: String
  421. let fileManager: FileManager
  422. let directory: URL?
  423. /// Creates a config value based on given parameters.
  424. ///
  425. /// - Parameters:
  426. /// - name: The name of cache. It is used as a part of storage folder. It is used to identify the disk
  427. /// storage. Two storages with the same `name` would share the same folder in disk, and it should
  428. /// be prevented.
  429. /// - sizeLimit: The size limit in bytes for all existing files in the disk storage.
  430. /// - fileManager: The `FileManager` used to manipulate files on disk. Default is `FileManager.default`.
  431. /// - directory: The URL where the disk storage should live. The storage will use this as the root folder,
  432. /// and append a path which is constructed by input `name`. Default is `nil`, indicates that
  433. /// the cache directory under user domain mask will be used.
  434. public init(
  435. name: String,
  436. sizeLimit: UInt,
  437. fileManager: FileManager = .default,
  438. directory: URL? = nil)
  439. {
  440. self.name = name
  441. self.fileManager = fileManager
  442. self.directory = directory
  443. self.sizeLimit = sizeLimit
  444. }
  445. }
  446. }
  447. extension DiskStorage {
  448. struct FileMeta {
  449. let url: URL
  450. let lastAccessDate: Date?
  451. let estimatedExpirationDate: Date?
  452. let isDirectory: Bool
  453. let fileSize: Int
  454. static func lastAccessDate(lhs: FileMeta, rhs: FileMeta) -> Bool {
  455. return lhs.lastAccessDate ?? .distantPast > rhs.lastAccessDate ?? .distantPast
  456. }
  457. init(fileURL: URL, resourceKeys: Set<URLResourceKey>) throws {
  458. let meta = try fileURL.resourceValues(forKeys: resourceKeys)
  459. self.init(
  460. fileURL: fileURL,
  461. lastAccessDate: meta.creationDate,
  462. estimatedExpirationDate: meta.contentModificationDate,
  463. isDirectory: meta.isDirectory ?? false,
  464. fileSize: meta.fileSize ?? 0)
  465. }
  466. init(
  467. fileURL: URL,
  468. lastAccessDate: Date?,
  469. estimatedExpirationDate: Date?,
  470. isDirectory: Bool,
  471. fileSize: Int)
  472. {
  473. self.url = fileURL
  474. self.lastAccessDate = lastAccessDate
  475. self.estimatedExpirationDate = estimatedExpirationDate
  476. self.isDirectory = isDirectory
  477. self.fileSize = fileSize
  478. }
  479. func expired(referenceDate: Date) -> Bool {
  480. return estimatedExpirationDate?.isPast(referenceDate: referenceDate) ?? true
  481. }
  482. func extendExpiration(with fileManager: FileManager, extendingExpiration: ExpirationExtending) {
  483. guard let lastAccessDate = lastAccessDate,
  484. let lastEstimatedExpiration = estimatedExpirationDate else
  485. {
  486. return
  487. }
  488. let attributes: [FileAttributeKey : Any]
  489. switch extendingExpiration {
  490. case .none:
  491. // not extending expiration time here
  492. return
  493. case .cacheTime:
  494. let originalExpiration: StorageExpiration =
  495. .seconds(lastEstimatedExpiration.timeIntervalSince(lastAccessDate))
  496. attributes = [
  497. .creationDate: Date().fileAttributeDate,
  498. .modificationDate: originalExpiration.estimatedExpirationSinceNow.fileAttributeDate
  499. ]
  500. case .expirationTime(let expirationTime):
  501. attributes = [
  502. .creationDate: Date().fileAttributeDate,
  503. .modificationDate: expirationTime.estimatedExpirationSinceNow.fileAttributeDate
  504. ]
  505. }
  506. try? fileManager.setAttributes(attributes, ofItemAtPath: url.path)
  507. }
  508. }
  509. }
  510. extension DiskStorage {
  511. struct Creation {
  512. let directoryURL: URL
  513. let cacheName: String
  514. init(_ config: Config) {
  515. let url: URL
  516. if let directory = config.directory {
  517. url = directory
  518. } else {
  519. url = config.fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
  520. }
  521. cacheName = "com.onevcat.Kingfisher.ImageCache.\(config.name)"
  522. directoryURL = config.cachePathBlock(url, cacheName)
  523. }
  524. }
  525. }
  526. fileprivate extension Error {
  527. var isFolderMissing: Bool {
  528. let nsError = self as NSError
  529. guard nsError.domain == NSCocoaErrorDomain, nsError.code == 4 else {
  530. return false
  531. }
  532. guard let underlyingError = nsError.userInfo[NSUnderlyingErrorKey] as? NSError else {
  533. return false
  534. }
  535. guard underlyingError.domain == NSPOSIXErrorDomain, underlyingError.code == 2 else {
  536. return false
  537. }
  538. return true
  539. }
  540. }