Storage.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 some time intervals
  28. struct TimeConstants {
  29. static let secondsInOneMinute = 60
  30. static let minutesInOneHour = 60
  31. static let hoursInOneDay = 24
  32. static let secondsInOneDay = 86_400
  33. }
  34. /// Represents the expiration strategy used in storage.
  35. ///
  36. /// - never: The item never expires.
  37. /// - seconds: The item expires after a time duration of given seconds from now.
  38. /// - days: The item expires after a time duration of given days from now.
  39. /// - date: The item expires after a given date.
  40. public enum StorageExpiration {
  41. /// The item never expires.
  42. case never
  43. /// The item expires after a time duration of given seconds from now.
  44. case seconds(TimeInterval)
  45. /// The item expires after a time duration of given days from now.
  46. case days(Int)
  47. /// The item expires after a given date.
  48. case date(Date)
  49. /// Indicates the item is already expired. Use this to skip cache.
  50. case expired
  51. func estimatedExpirationSince(_ date: Date) -> Date {
  52. switch self {
  53. case .never: return .distantFuture
  54. case .seconds(let seconds):
  55. return date.addingTimeInterval(seconds)
  56. case .days(let days):
  57. let duration = TimeInterval(TimeConstants.secondsInOneDay) * TimeInterval(days)
  58. return date.addingTimeInterval(duration)
  59. case .date(let ref):
  60. return ref
  61. case .expired:
  62. return .distantPast
  63. }
  64. }
  65. var estimatedExpirationSinceNow: Date {
  66. return estimatedExpirationSince(Date())
  67. }
  68. var isExpired: Bool {
  69. return timeInterval <= 0
  70. }
  71. var timeInterval: TimeInterval {
  72. switch self {
  73. case .never: return .infinity
  74. case .seconds(let seconds): return seconds
  75. case .days(let days): return TimeInterval(TimeConstants.secondsInOneDay) * TimeInterval(days)
  76. case .date(let ref): return ref.timeIntervalSinceNow
  77. case .expired: return -(.infinity)
  78. }
  79. }
  80. }
  81. /// Represents the expiration extending strategy used in storage to after access.
  82. ///
  83. /// - none: The item expires after the original time, without extending after access.
  84. /// - cacheTime: The item expiration extends by the original cache time after each access.
  85. /// - expirationTime: The item expiration extends by the provided time after each access.
  86. public enum ExpirationExtending {
  87. /// The item expires after the original time, without extending after access.
  88. case none
  89. /// The item expiration extends by the original cache time after each access.
  90. case cacheTime
  91. /// The item expiration extends by the provided time after each access.
  92. case expirationTime(_ expiration: StorageExpiration)
  93. }
  94. /// Represents types which cost in memory can be calculated.
  95. public protocol CacheCostCalculable {
  96. var cacheCost: Int { get }
  97. }
  98. /// Represents types which can be converted to and from data.
  99. public protocol DataTransformable {
  100. func toData() throws -> Data
  101. static func fromData(_ data: Data) throws -> Self
  102. static var empty: Self { get }
  103. }