Storage.swift 4.1 KB

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