DiskStorage.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. throw KingfisherError.cacheError(
  131. reason: .cannotCreateCacheFile(fileURL: fileURL, key: key, data: data, error: error)
  132. )
  133. }
  134. let now = Date()
  135. let attributes: [FileAttributeKey : Any] = [
  136. // The last access date.
  137. .creationDate: now.fileAttributeDate,
  138. // The estimated expiration date.
  139. .modificationDate: expiration.estimatedExpirationSinceNow.fileAttributeDate
  140. ]
  141. do {
  142. try config.fileManager.setAttributes(attributes, ofItemAtPath: fileURL.path)
  143. } catch {
  144. try? config.fileManager.removeItem(at: fileURL)
  145. throw KingfisherError.cacheError(
  146. reason: .cannotSetCacheFileAttribute(
  147. filePath: fileURL.path,
  148. attributes: attributes,
  149. error: error
  150. )
  151. )
  152. }
  153. maybeCachedCheckingQueue.async {
  154. self.maybeCached?.insert(fileURL.lastPathComponent)
  155. }
  156. }
  157. /// Gets a value from the storage.
  158. /// - Parameters:
  159. /// - key: The cache key of value.
  160. /// - extendingExpiration: The expiration policy used by this getting action.
  161. /// - Throws: An error during converting the data to a value or during operation of disk files.
  162. /// - Returns: The value under `key` if it is valid and found in the storage. Otherwise, `nil`.
  163. public func value(forKey key: String, extendingExpiration: ExpirationExtending = .cacheTime) throws -> T? {
  164. return try value(forKey: key, referenceDate: Date(), actuallyLoad: true, extendingExpiration: extendingExpiration)
  165. }
  166. func value(
  167. forKey key: String,
  168. referenceDate: Date,
  169. actuallyLoad: Bool,
  170. extendingExpiration: ExpirationExtending) throws -> T?
  171. {
  172. guard storageReady else {
  173. throw KingfisherError.cacheError(reason: .diskStorageIsNotReady(cacheURL: directoryURL))
  174. }
  175. let fileManager = config.fileManager
  176. let fileURL = cacheFileURL(forKey: key)
  177. let filePath = fileURL.path
  178. let fileMaybeCached = maybeCachedCheckingQueue.sync {
  179. return maybeCached?.contains(fileURL.lastPathComponent) ?? true
  180. }
  181. guard fileMaybeCached else {
  182. return nil
  183. }
  184. guard fileManager.fileExists(atPath: filePath) else {
  185. return nil
  186. }
  187. let meta: FileMeta
  188. do {
  189. let resourceKeys: Set<URLResourceKey> = [.contentModificationDateKey, .creationDateKey]
  190. meta = try FileMeta(fileURL: fileURL, resourceKeys: resourceKeys)
  191. } catch {
  192. throw KingfisherError.cacheError(
  193. reason: .invalidURLResource(error: error, key: key, url: fileURL))
  194. }
  195. if meta.expired(referenceDate: referenceDate) {
  196. return nil
  197. }
  198. if !actuallyLoad { return T.empty }
  199. do {
  200. let data = try Data(contentsOf: fileURL)
  201. let obj = try T.fromData(data)
  202. metaChangingQueue.async {
  203. meta.extendExpiration(with: fileManager, extendingExpiration: extendingExpiration)
  204. }
  205. return obj
  206. } catch {
  207. throw KingfisherError.cacheError(reason: .cannotLoadDataFromDisk(url: fileURL, error: error))
  208. }
  209. }
  210. /// Whether there is valid cached data under a given key.
  211. /// - Parameter key: The cache key of value.
  212. /// - Returns: If there is valid data under the key, `true`. Otherwise, `false`.
  213. ///
  214. /// - Note:
  215. /// This method does not actually load the data from disk, so it is faster than directly loading the cached value
  216. /// by checking the nullability of `value(forKey:extendingExpiration:)` method.
  217. ///
  218. public func isCached(forKey key: String) -> Bool {
  219. return isCached(forKey: key, referenceDate: Date())
  220. }
  221. /// Whether there is valid cached data under a given key and a reference date.
  222. /// - Parameters:
  223. /// - key: The cache key of value.
  224. /// - referenceDate: A reference date to check whether the cache is still valid.
  225. /// - Returns: If there is valid data under the key, `true`. Otherwise, `false`.
  226. ///
  227. /// - Note:
  228. /// If you pass `Date()` to `referenceDate`, this method is identical to `isCached(forKey:)`. Use the
  229. /// `referenceDate` to determine whether the cache is still valid for a future date.
  230. public func isCached(forKey key: String, referenceDate: Date) -> Bool {
  231. do {
  232. let result = try value(
  233. forKey: key,
  234. referenceDate: referenceDate,
  235. actuallyLoad: false,
  236. extendingExpiration: .none
  237. )
  238. return result != nil
  239. } catch {
  240. return false
  241. }
  242. }
  243. /// Removes a value from a specified key.
  244. /// - Parameter key: The cache key of value.
  245. /// - Throws: An error during removing the value.
  246. public func remove(forKey key: String) throws {
  247. let fileURL = cacheFileURL(forKey: key)
  248. try removeFile(at: fileURL)
  249. }
  250. func removeFile(at url: URL) throws {
  251. try config.fileManager.removeItem(at: url)
  252. }
  253. /// Removes all values in this storage.
  254. /// - Throws: An error during removing the values.
  255. public func removeAll() throws {
  256. try removeAll(skipCreatingDirectory: false)
  257. }
  258. func removeAll(skipCreatingDirectory: Bool) throws {
  259. try config.fileManager.removeItem(at: directoryURL)
  260. if !skipCreatingDirectory {
  261. try prepareDirectory()
  262. }
  263. }
  264. /// The URL of the cached file with a given computed `key`.
  265. ///
  266. /// - Parameter key: The final computed key used when caching the image. Please note that usually this is not
  267. /// the `cacheKey` of an image `Source`. It is the computed key with processor identifier considered.
  268. ///
  269. /// - Note:
  270. /// This method does not guarantee there is an image already cached in the returned URL. It just gives your
  271. /// the URL that the image should be if it exists in disk storage, with the give key.
  272. ///
  273. public func cacheFileURL(forKey key: String) -> URL {
  274. let fileName = cacheFileName(forKey: key)
  275. return directoryURL.appendingPathComponent(fileName, isDirectory: false)
  276. }
  277. func cacheFileName(forKey key: String) -> String {
  278. if config.usesHashedFileName {
  279. let hashedKey = key.kf.md5
  280. if let ext = config.pathExtension {
  281. return "\(hashedKey).\(ext)"
  282. } else if config.autoExtAfterHashedFileName,
  283. let ext = key.kf.ext {
  284. return "\(hashedKey).\(ext)"
  285. }
  286. return hashedKey
  287. } else {
  288. if let ext = config.pathExtension {
  289. return "\(key).\(ext)"
  290. }
  291. return key
  292. }
  293. }
  294. func allFileURLs(for propertyKeys: [URLResourceKey]) throws -> [URL] {
  295. let fileManager = config.fileManager
  296. guard let directoryEnumerator = fileManager.enumerator(
  297. at: directoryURL, includingPropertiesForKeys: propertyKeys, options: .skipsHiddenFiles) else
  298. {
  299. throw KingfisherError.cacheError(reason: .fileEnumeratorCreationFailed(url: directoryURL))
  300. }
  301. guard let urls = directoryEnumerator.allObjects as? [URL] else {
  302. throw KingfisherError.cacheError(reason: .invalidFileEnumeratorContent(url: directoryURL))
  303. }
  304. return urls
  305. }
  306. /// Removes all expired values from this storage.
  307. /// - Throws: A file manager error during removing the file.
  308. /// - Returns: The URLs for removed files.
  309. public func removeExpiredValues() throws -> [URL] {
  310. return try removeExpiredValues(referenceDate: Date())
  311. }
  312. func removeExpiredValues(referenceDate: Date) throws -> [URL] {
  313. let propertyKeys: [URLResourceKey] = [
  314. .isDirectoryKey,
  315. .contentModificationDateKey
  316. ]
  317. let urls = try allFileURLs(for: propertyKeys)
  318. let keys = Set(propertyKeys)
  319. let expiredFiles = urls.filter { fileURL in
  320. do {
  321. let meta = try FileMeta(fileURL: fileURL, resourceKeys: keys)
  322. if meta.isDirectory {
  323. return false
  324. }
  325. return meta.expired(referenceDate: referenceDate)
  326. } catch {
  327. return true
  328. }
  329. }
  330. try expiredFiles.forEach { url in
  331. try removeFile(at: url)
  332. }
  333. return expiredFiles
  334. }
  335. /// Removes all size exceeded values from this storage.
  336. /// - Throws: A file manager error during removing the file.
  337. /// - Returns: The URLs for removed files.
  338. ///
  339. /// - Note: This method checks `config.sizeLimit` and remove cached files in an LRU (Least Recently Used) way.
  340. func removeSizeExceededValues() throws -> [URL] {
  341. if config.sizeLimit == 0 { return [] } // Back compatible. 0 means no limit.
  342. var size = try totalSize()
  343. if size < config.sizeLimit { return [] }
  344. let propertyKeys: [URLResourceKey] = [
  345. .isDirectoryKey,
  346. .creationDateKey,
  347. .fileSizeKey
  348. ]
  349. let keys = Set(propertyKeys)
  350. let urls = try allFileURLs(for: propertyKeys)
  351. var pendings: [FileMeta] = urls.compactMap { fileURL in
  352. guard let meta = try? FileMeta(fileURL: fileURL, resourceKeys: keys) else {
  353. return nil
  354. }
  355. return meta
  356. }
  357. // Sort by last access date. Most recent file first.
  358. pendings.sort(by: FileMeta.lastAccessDate)
  359. var removed: [URL] = []
  360. let target = config.sizeLimit / 2
  361. while size > target, let meta = pendings.popLast() {
  362. size -= UInt(meta.fileSize)
  363. try removeFile(at: meta.url)
  364. removed.append(meta.url)
  365. }
  366. return removed
  367. }
  368. /// Gets the total file size of the folder in bytes.
  369. public func totalSize() throws -> UInt {
  370. let propertyKeys: [URLResourceKey] = [.fileSizeKey]
  371. let urls = try allFileURLs(for: propertyKeys)
  372. let keys = Set(propertyKeys)
  373. let totalSize: UInt = urls.reduce(0) { size, fileURL in
  374. do {
  375. let meta = try FileMeta(fileURL: fileURL, resourceKeys: keys)
  376. return size + UInt(meta.fileSize)
  377. } catch {
  378. return size
  379. }
  380. }
  381. return totalSize
  382. }
  383. }
  384. }
  385. extension DiskStorage {
  386. /// Represents the config used in a `DiskStorage`.
  387. public struct Config {
  388. /// The file size limit on disk of the storage in bytes. 0 means no limit.
  389. public var sizeLimit: UInt
  390. /// The `StorageExpiration` used in this disk storage. Default is `.days(7)`,
  391. /// means that the disk cache would expire in one week.
  392. public var expiration: StorageExpiration = .days(7)
  393. /// The preferred extension of cache item. It will be appended to the file name as its extension.
  394. /// Default is `nil`, means that the cache file does not contain a file extension.
  395. public var pathExtension: String? = nil
  396. /// Default is `true`, means that the cache file name will be hashed before storing.
  397. public var usesHashedFileName = true
  398. /// Default is `false`
  399. /// If set to `true`, image extension will be extracted from original file name and append to
  400. /// the hased file name and used as the cache key on disk.
  401. public var autoExtAfterHashedFileName = false
  402. /// Closure that takes in initial directory path and generates
  403. /// the final disk cache path. You can use it to fully customize your cache path.
  404. public var cachePathBlock: ((_ directory: URL, _ cacheName: String) -> URL)! = {
  405. (directory, cacheName) in
  406. return directory.appendingPathComponent(cacheName, isDirectory: true)
  407. }
  408. let name: String
  409. let fileManager: FileManager
  410. let directory: URL?
  411. /// Creates a config value based on given parameters.
  412. ///
  413. /// - Parameters:
  414. /// - name: The name of cache. It is used as a part of storage folder. It is used to identify the disk
  415. /// storage. Two storages with the same `name` would share the same folder in disk, and it should
  416. /// be prevented.
  417. /// - sizeLimit: The size limit in bytes for all existing files in the disk storage.
  418. /// - fileManager: The `FileManager` used to manipulate files on disk. Default is `FileManager.default`.
  419. /// - directory: The URL where the disk storage should live. The storage will use this as the root folder,
  420. /// and append a path which is constructed by input `name`. Default is `nil`, indicates that
  421. /// the cache directory under user domain mask will be used.
  422. public init(
  423. name: String,
  424. sizeLimit: UInt,
  425. fileManager: FileManager = .default,
  426. directory: URL? = nil)
  427. {
  428. self.name = name
  429. self.fileManager = fileManager
  430. self.directory = directory
  431. self.sizeLimit = sizeLimit
  432. }
  433. }
  434. }
  435. extension DiskStorage {
  436. struct FileMeta {
  437. let url: URL
  438. let lastAccessDate: Date?
  439. let estimatedExpirationDate: Date?
  440. let isDirectory: Bool
  441. let fileSize: Int
  442. static func lastAccessDate(lhs: FileMeta, rhs: FileMeta) -> Bool {
  443. return lhs.lastAccessDate ?? .distantPast > rhs.lastAccessDate ?? .distantPast
  444. }
  445. init(fileURL: URL, resourceKeys: Set<URLResourceKey>) throws {
  446. let meta = try fileURL.resourceValues(forKeys: resourceKeys)
  447. self.init(
  448. fileURL: fileURL,
  449. lastAccessDate: meta.creationDate,
  450. estimatedExpirationDate: meta.contentModificationDate,
  451. isDirectory: meta.isDirectory ?? false,
  452. fileSize: meta.fileSize ?? 0)
  453. }
  454. init(
  455. fileURL: URL,
  456. lastAccessDate: Date?,
  457. estimatedExpirationDate: Date?,
  458. isDirectory: Bool,
  459. fileSize: Int)
  460. {
  461. self.url = fileURL
  462. self.lastAccessDate = lastAccessDate
  463. self.estimatedExpirationDate = estimatedExpirationDate
  464. self.isDirectory = isDirectory
  465. self.fileSize = fileSize
  466. }
  467. func expired(referenceDate: Date) -> Bool {
  468. return estimatedExpirationDate?.isPast(referenceDate: referenceDate) ?? true
  469. }
  470. func extendExpiration(with fileManager: FileManager, extendingExpiration: ExpirationExtending) {
  471. guard let lastAccessDate = lastAccessDate,
  472. let lastEstimatedExpiration = estimatedExpirationDate else
  473. {
  474. return
  475. }
  476. let attributes: [FileAttributeKey : Any]
  477. switch extendingExpiration {
  478. case .none:
  479. // not extending expiration time here
  480. return
  481. case .cacheTime:
  482. let originalExpiration: StorageExpiration =
  483. .seconds(lastEstimatedExpiration.timeIntervalSince(lastAccessDate))
  484. attributes = [
  485. .creationDate: Date().fileAttributeDate,
  486. .modificationDate: originalExpiration.estimatedExpirationSinceNow.fileAttributeDate
  487. ]
  488. case .expirationTime(let expirationTime):
  489. attributes = [
  490. .creationDate: Date().fileAttributeDate,
  491. .modificationDate: expirationTime.estimatedExpirationSinceNow.fileAttributeDate
  492. ]
  493. }
  494. try? fileManager.setAttributes(attributes, ofItemAtPath: url.path)
  495. }
  496. }
  497. }
  498. extension DiskStorage {
  499. struct Creation {
  500. let directoryURL: URL
  501. let cacheName: String
  502. init(_ config: Config) {
  503. let url: URL
  504. if let directory = config.directory {
  505. url = directory
  506. } else {
  507. url = config.fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
  508. }
  509. cacheName = "com.onevcat.Kingfisher.ImageCache.\(config.name)"
  510. directoryURL = config.cachePathBlock(url, cacheName)
  511. }
  512. }
  513. }