Storage.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 = secondsInOneMinute * minutesInOneHour * hoursInOneDay
  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): return date.addingTimeInterval(seconds)
  55. case .days(let days): return date.addingTimeInterval(TimeInterval(TimeConstants.secondsInOneDay * days))
  56. case .date(let ref): return ref
  57. case .expired: return .distantPast
  58. }
  59. }
  60. var estimatedExpirationSinceNow: Date {
  61. return estimatedExpirationSince(Date())
  62. }
  63. var isExpired: Bool {
  64. return timeInterval <= 0
  65. }
  66. var timeInterval: TimeInterval {
  67. switch self {
  68. case .never: return .infinity
  69. case .seconds(let seconds): return seconds
  70. case .days(let days): return TimeInterval(TimeConstants.secondsInOneDay * days)
  71. case .date(let ref): return ref.timeIntervalSinceNow
  72. case .expired: return -(.infinity)
  73. }
  74. }
  75. }
  76. /// Represents the expiration extending strategy used in storage to after access.
  77. ///
  78. /// - none: The item expires after the original time, without extending after access.
  79. /// - cacheTime: The item expiration extends by the original cache time after each access.
  80. /// - expirationTime: The item expiration extends by the privided time after each access.
  81. public enum ExpirationExtending {
  82. /// The item expires after the original time, without extending after access.
  83. case none
  84. /// The item expiration extends by the original cache time after each access.
  85. case cacheTime
  86. /// The item expiration extends by the provided time after each access.
  87. case expirationTime(_ expiration: StorageExpiration)
  88. }
  89. /// Represents types which cost in memory can be calculated.
  90. public protocol CacheCostCalculable {
  91. var cacheCost: Int { get }
  92. }
  93. /// Represents types which can be converted to and from data.
  94. public protocol DataTransformable {
  95. func toData() throws -> Data
  96. static func fromData(_ data: Data) throws -> Self
  97. static var empty: Self { get }
  98. }