DiskStorage.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. /// Creates a disk storage with the given `DiskStorage.Config`.
  44. ///
  45. /// - Parameter config: The config used for this disk storage.
  46. /// - Throws: An error if the folder for storage cannot be got or created.
  47. public init(config: Config) throws {
  48. self.config = config
  49. let url: URL
  50. if let directory = config.directory {
  51. url = directory
  52. } else {
  53. url = try config.fileManager.url(
  54. for: .cachesDirectory,
  55. in: .userDomainMask,
  56. appropriateFor: nil,
  57. create: true)
  58. }
  59. let cacheName = "com.onevcat.Kingfisher.ImageCache.\(config.name)"
  60. directoryURL = config.cachePathBlock(url, cacheName)
  61. metaChangingQueue = DispatchQueue(label: cacheName)
  62. try prepareDirectory()
  63. }
  64. // Creates the storage folder.
  65. func prepareDirectory() throws {
  66. let fileManager = config.fileManager
  67. let path = directoryURL.path
  68. guard !fileManager.fileExists(atPath: path) else { return }
  69. do {
  70. try fileManager.createDirectory(
  71. atPath: path,
  72. withIntermediateDirectories: true,
  73. attributes: nil)
  74. } catch {
  75. throw KingfisherError.cacheError(reason: .cannotCreateDirectory(path: path, error: error))
  76. }
  77. }
  78. func store(
  79. value: T,
  80. forKey key: String,
  81. expiration: StorageExpiration? = nil) throws
  82. {
  83. let expiration = expiration ?? config.expiration
  84. // The expiration indicates that already expired, no need to store.
  85. guard !expiration.isExpired else { return }
  86. let data: Data
  87. do {
  88. data = try value.toData()
  89. } catch {
  90. throw KingfisherError.cacheError(reason: .cannotConvertToData(object: value, error: error))
  91. }
  92. let fileURL = cacheFileURL(forKey: key)
  93. let now = Date()
  94. let attributes: [FileAttributeKey : Any] = [
  95. // The last access date.
  96. .creationDate: now.fileAttributeDate,
  97. // The estimated expiration date.
  98. .modificationDate: expiration.estimatedExpirationSinceNow.fileAttributeDate
  99. ]
  100. config.fileManager.createFile(atPath: fileURL.path, contents: data, attributes: attributes)
  101. }
  102. func value(forKey key: String, extendingExpiration: ExpirationExtending = .cacheTime) throws -> T? {
  103. return try value(forKey: key, referenceDate: Date(), actuallyLoad: true, extendingExpiration: extendingExpiration)
  104. }
  105. func value(
  106. forKey key: String,
  107. referenceDate: Date,
  108. actuallyLoad: Bool,
  109. extendingExpiration: ExpirationExtending) throws -> T?
  110. {
  111. let fileManager = config.fileManager
  112. let fileURL = cacheFileURL(forKey: key)
  113. let filePath = fileURL.path
  114. guard fileManager.fileExists(atPath: filePath) else {
  115. return nil
  116. }
  117. let meta: FileMeta
  118. do {
  119. let resourceKeys: Set<URLResourceKey> = [.contentModificationDateKey, .creationDateKey]
  120. meta = try FileMeta(fileURL: fileURL, resourceKeys: resourceKeys)
  121. } catch {
  122. throw KingfisherError.cacheError(
  123. reason: .invalidURLResource(error: error, key: key, url: fileURL))
  124. }
  125. if meta.expired(referenceDate: referenceDate) {
  126. return nil
  127. }
  128. if !actuallyLoad { return T.empty }
  129. do {
  130. let data = try Data(contentsOf: fileURL)
  131. let obj = try T.fromData(data)
  132. metaChangingQueue.async { meta.extendExpiration(with: fileManager, extendingExpiration: extendingExpiration) }
  133. return obj
  134. } catch {
  135. throw KingfisherError.cacheError(reason: .cannotLoadDataFromDisk(url: fileURL, error: error))
  136. }
  137. }
  138. func isCached(forKey key: String) -> Bool {
  139. return isCached(forKey: key, referenceDate: Date())
  140. }
  141. func isCached(forKey key: String, referenceDate: Date) -> Bool {
  142. do {
  143. guard let _ = try value(forKey: key, referenceDate: referenceDate, actuallyLoad: false, extendingExpiration: .none) else {
  144. return false
  145. }
  146. return true
  147. } catch {
  148. return false
  149. }
  150. }
  151. func remove(forKey key: String) throws {
  152. let fileURL = cacheFileURL(forKey: key)
  153. try removeFile(at: fileURL)
  154. }
  155. func removeFile(at url: URL) throws {
  156. try config.fileManager.removeItem(at: url)
  157. }
  158. func removeAll() throws {
  159. try removeAll(skipCreatingDirectory: false)
  160. }
  161. func removeAll(skipCreatingDirectory: Bool) throws {
  162. try config.fileManager.removeItem(at: directoryURL)
  163. if !skipCreatingDirectory {
  164. try prepareDirectory()
  165. }
  166. }
  167. /// The URL of the cached file with a given computed `key`.
  168. ///
  169. /// - Note:
  170. /// This method does not guarantee there is an image already cached in the returned URL. It just gives your
  171. /// the URL that the image should be if it exists in disk storage, with the give key.
  172. ///
  173. /// - Parameter key: The final computed key used when caching the image. Please note that usually this is not
  174. /// the `cacheKey` of an image `Source`. It is the computed key with processor identifier considered.
  175. public func cacheFileURL(forKey key: String) -> URL {
  176. let fileName = cacheFileName(forKey: key)
  177. return directoryURL.appendingPathComponent(fileName)
  178. }
  179. func cacheFileName(forKey key: String) -> String {
  180. if config.usesHashedFileName {
  181. let hashedKey = key.kf.md5
  182. if let ext = config.pathExtension {
  183. return "\(hashedKey).\(ext)"
  184. }
  185. return hashedKey
  186. } else {
  187. if let ext = config.pathExtension {
  188. return "\(key).\(ext)"
  189. }
  190. return key
  191. }
  192. }
  193. func allFileURLs(for propertyKeys: [URLResourceKey]) throws -> [URL] {
  194. let fileManager = config.fileManager
  195. guard let directoryEnumerator = fileManager.enumerator(
  196. at: directoryURL, includingPropertiesForKeys: propertyKeys, options: .skipsHiddenFiles) else
  197. {
  198. throw KingfisherError.cacheError(reason: .fileEnumeratorCreationFailed(url: directoryURL))
  199. }
  200. guard let urls = directoryEnumerator.allObjects as? [URL] else {
  201. throw KingfisherError.cacheError(reason: .invalidFileEnumeratorContent(url: directoryURL))
  202. }
  203. return urls
  204. }
  205. func removeExpiredValues(referenceDate: Date = Date()) throws -> [URL] {
  206. let propertyKeys: [URLResourceKey] = [
  207. .isDirectoryKey,
  208. .contentModificationDateKey
  209. ]
  210. let urls = try allFileURLs(for: propertyKeys)
  211. let keys = Set(propertyKeys)
  212. let expiredFiles = urls.filter { fileURL in
  213. do {
  214. let meta = try FileMeta(fileURL: fileURL, resourceKeys: keys)
  215. if meta.isDirectory {
  216. return false
  217. }
  218. return meta.expired(referenceDate: referenceDate)
  219. } catch {
  220. return true
  221. }
  222. }
  223. try expiredFiles.forEach { url in
  224. try removeFile(at: url)
  225. }
  226. return expiredFiles
  227. }
  228. func removeSizeExceededValues() throws -> [URL] {
  229. if config.sizeLimit == 0 { return [] } // Back compatible. 0 means no limit.
  230. var size = try totalSize()
  231. if size < config.sizeLimit { return [] }
  232. let propertyKeys: [URLResourceKey] = [
  233. .isDirectoryKey,
  234. .creationDateKey,
  235. .fileSizeKey
  236. ]
  237. let keys = Set(propertyKeys)
  238. let urls = try allFileURLs(for: propertyKeys)
  239. var pendings: [FileMeta] = urls.compactMap { fileURL in
  240. guard let meta = try? FileMeta(fileURL: fileURL, resourceKeys: keys) else {
  241. return nil
  242. }
  243. return meta
  244. }
  245. // Sort by last access date. Most recent file first.
  246. pendings.sort(by: FileMeta.lastAccessDate)
  247. var removed: [URL] = []
  248. let target = config.sizeLimit / 2
  249. while size > target, let meta = pendings.popLast() {
  250. size -= UInt(meta.fileSize)
  251. try removeFile(at: meta.url)
  252. removed.append(meta.url)
  253. }
  254. return removed
  255. }
  256. /// Get the total file size of the folder in bytes.
  257. func totalSize() throws -> UInt {
  258. let propertyKeys: [URLResourceKey] = [.fileSizeKey]
  259. let urls = try allFileURLs(for: propertyKeys)
  260. let keys = Set(propertyKeys)
  261. let totalSize: UInt = urls.reduce(0) { size, fileURL in
  262. do {
  263. let meta = try FileMeta(fileURL: fileURL, resourceKeys: keys)
  264. return size + UInt(meta.fileSize)
  265. } catch {
  266. return size
  267. }
  268. }
  269. return totalSize
  270. }
  271. }
  272. }
  273. extension DiskStorage {
  274. /// Represents the config used in a `DiskStorage`.
  275. public struct Config {
  276. /// The file size limit on disk of the storage in bytes. 0 means no limit.
  277. public var sizeLimit: UInt
  278. /// The `StorageExpiration` used in this disk storage. Default is `.days(7)`,
  279. /// means that the disk cache would expire in one week.
  280. public var expiration: StorageExpiration = .days(7)
  281. /// The preferred extension of cache item. It will be appended to the file name as its extension.
  282. /// Default is `nil`, means that the cache file does not contain a file extension.
  283. public var pathExtension: String? = nil
  284. /// Default is `true`, means that the cache file name will be hashed before storing.
  285. public var usesHashedFileName = true
  286. let name: String
  287. let fileManager: FileManager
  288. let directory: URL?
  289. var cachePathBlock: ((_ directory: URL, _ cacheName: String) -> URL)! = {
  290. (directory, cacheName) in
  291. return directory.appendingPathComponent(cacheName, isDirectory: true)
  292. }
  293. /// Creates a config value based on given parameters.
  294. ///
  295. /// - Parameters:
  296. /// - name: The name of cache. It is used as a part of storage folder. It is used to identify the disk
  297. /// storage. Two storages with the same `name` would share the same folder in disk, and it should
  298. /// be prevented.
  299. /// - sizeLimit: The size limit in bytes for all existing files in the disk storage.
  300. /// - fileManager: The `FileManager` used to manipulate files on disk. Default is `FileManager.default`.
  301. /// - directory: The URL where the disk storage should live. The storage will use this as the root folder,
  302. /// and append a path which is constructed by input `name`. Default is `nil`, indicates that
  303. /// the cache directory under user domain mask will be used.
  304. public init(
  305. name: String,
  306. sizeLimit: UInt,
  307. fileManager: FileManager = .default,
  308. directory: URL? = nil)
  309. {
  310. self.name = name
  311. self.fileManager = fileManager
  312. self.directory = directory
  313. self.sizeLimit = sizeLimit
  314. }
  315. }
  316. }
  317. extension DiskStorage {
  318. struct FileMeta {
  319. let url: URL
  320. let lastAccessDate: Date?
  321. let estimatedExpirationDate: Date?
  322. let isDirectory: Bool
  323. let fileSize: Int
  324. static func lastAccessDate(lhs: FileMeta, rhs: FileMeta) -> Bool {
  325. return lhs.lastAccessDate ?? .distantPast > rhs.lastAccessDate ?? .distantPast
  326. }
  327. init(fileURL: URL, resourceKeys: Set<URLResourceKey>) throws {
  328. let meta = try fileURL.resourceValues(forKeys: resourceKeys)
  329. self.init(
  330. fileURL: fileURL,
  331. lastAccessDate: meta.creationDate,
  332. estimatedExpirationDate: meta.contentModificationDate,
  333. isDirectory: meta.isDirectory ?? false,
  334. fileSize: meta.fileSize ?? 0)
  335. }
  336. init(
  337. fileURL: URL,
  338. lastAccessDate: Date?,
  339. estimatedExpirationDate: Date?,
  340. isDirectory: Bool,
  341. fileSize: Int)
  342. {
  343. self.url = fileURL
  344. self.lastAccessDate = lastAccessDate
  345. self.estimatedExpirationDate = estimatedExpirationDate
  346. self.isDirectory = isDirectory
  347. self.fileSize = fileSize
  348. }
  349. func expired(referenceDate: Date) -> Bool {
  350. return estimatedExpirationDate?.isPast(referenceDate: referenceDate) ?? true
  351. }
  352. func extendExpiration(with fileManager: FileManager, extendingExpiration: ExpirationExtending) {
  353. guard let lastAccessDate = lastAccessDate,
  354. let lastEstimatedExpiration = estimatedExpirationDate else
  355. {
  356. return
  357. }
  358. let attributes: [FileAttributeKey : Any]
  359. switch extendingExpiration {
  360. case .none:
  361. // not extending expiration time here
  362. return
  363. case .cacheTime:
  364. let originalExpiration: StorageExpiration =
  365. .seconds(lastEstimatedExpiration.timeIntervalSince(lastAccessDate))
  366. attributes = [
  367. .creationDate: Date().fileAttributeDate,
  368. .modificationDate: originalExpiration.estimatedExpirationSinceNow.fileAttributeDate
  369. ]
  370. case .expirationTime(let expirationTime):
  371. attributes = [
  372. .creationDate: Date().fileAttributeDate,
  373. .modificationDate: expirationTime.estimatedExpirationSinceNow.fileAttributeDate
  374. ]
  375. }
  376. try? fileManager.setAttributes(attributes, ofItemAtPath: url.path)
  377. }
  378. }
  379. }