Storage.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // Storage.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. /// Constants for certain time intervals.
  28. struct TimeConstants {
  29. // Seconds in a day, a.k.a 86,400s, roughly.
  30. static let secondsInOneDay = 86_400
  31. }
  32. /// Represents the expiration strategy utilized in storage.
  33. public enum StorageExpiration: Sendable {
  34. /// The item never expires.
  35. case never
  36. /// The item expires after a duration of the provided number of seconds from now.
  37. case seconds(TimeInterval)
  38. /// The item expires after a duration of the provided number of days from now.
  39. case days(Int)
  40. /// The item expires after a specified date.
  41. case date(Date)
  42. /// Indicates that the item has already expired.
  43. ///
  44. /// Use this to bypass the cache.
  45. case expired
  46. func estimatedExpirationSince(_ date: Date) -> Date {
  47. switch self {
  48. case .never:
  49. return .distantFuture
  50. case .seconds(let seconds):
  51. return date.addingTimeInterval(seconds)
  52. case .days(let days):
  53. let duration: TimeInterval = TimeInterval(TimeConstants.secondsInOneDay * days)
  54. return date.addingTimeInterval(duration)
  55. case .date(let ref):
  56. return ref
  57. case .expired:
  58. return .distantPast
  59. }
  60. }
  61. var estimatedExpirationSinceNow: Date {
  62. estimatedExpirationSince(Date())
  63. }
  64. var isExpired: Bool {
  65. timeInterval <= 0
  66. }
  67. var timeInterval: TimeInterval {
  68. switch self {
  69. case .never: return .infinity
  70. case .seconds(let seconds): return seconds
  71. case .days(let days): return TimeInterval(TimeConstants.secondsInOneDay * days)
  72. case .date(let ref): return ref.timeIntervalSinceNow
  73. case .expired: return -(.infinity)
  74. }
  75. }
  76. }
  77. /// Represents the expiration extension strategy used in storage after access.
  78. public enum ExpirationExtending: Sendable {
  79. /// The item expires after the original time, without extension after access.
  80. case none
  81. /// The item expiration extends to the original cache time after each access.
  82. case cacheTime
  83. /// The item expiration extends by the provided time after each access.
  84. case expirationTime(_ expiration: StorageExpiration)
  85. }
  86. /// Represents types for which the memory cost can be calculated.
  87. public protocol CacheCostCalculable {
  88. var cacheCost: Int { get }
  89. }
  90. /// Represents types that can be converted to and from data.
  91. public protocol DataTransformable {
  92. /// Converts the current value to a `Data` representation.
  93. /// - Returns: The data object which can represent the value of the conforming type.
  94. /// - Throws: If any error happens during the conversion.
  95. func toData() throws -> Data
  96. /// Convert some data to the value.
  97. /// - Parameter data: The data object which should represent the conforming value.
  98. /// - Returns: The converted value of the conforming type.
  99. /// - Throws: If any error happens during the conversion.
  100. static func fromData(_ data: Data) throws -> Self
  101. /// An empty object of `Self`.
  102. ///
  103. /// > In the cache, when the data is not actually loaded, this value will be returned as a placeholder.
  104. /// > This variable should be returned quickly without any heavy operation inside.
  105. static var empty: Self { get }
  106. }