You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
3.2 KiB

  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. /// Represents the expiration strategy used in storage.
  28. ///
  29. /// - never: The item never expires.
  30. /// - seconds: The item expires after a time duration of given seconds from now.
  31. /// - days: The item expires after a time duration of given days from now.
  32. /// - date: The item expires after a given date.
  33. public enum StorageExpiration {
  34. /// The item never expires.
  35. case never
  36. /// The item expires after a time duration of given seconds from now.
  37. case seconds(TimeInterval)
  38. /// The item expires after a time duration of given days from now.
  39. case days(Int)
  40. /// The item expires after a given date.
  41. case date(Date)
  42. /// Indicates the item is already expired. Use this to skip cache.
  43. case expired
  44. func estimatedExpirationSince(_ date: Date) -> Date {
  45. switch self {
  46. case .never: return .distantFuture
  47. case .seconds(let seconds): return date.addingTimeInterval(seconds)
  48. case .days(let days): return date.addingTimeInterval(TimeInterval(60 * 60 * 24 * days))
  49. case .date(let ref): return ref
  50. case .expired: return .distantPast
  51. }
  52. }
  53. var estimatedExpirationSinceNow: Date {
  54. return estimatedExpirationSince(Date())
  55. }
  56. var isExpired: Bool {
  57. return timeInterval <= 0
  58. }
  59. var timeInterval: TimeInterval {
  60. switch self {
  61. case .never: return .infinity
  62. case .seconds(let seconds): return seconds
  63. case .days(let days): return TimeInterval(60 * 60 * 24 * days)
  64. case .date(let ref): return ref.timeIntervalSinceNow
  65. case .expired: return -(.infinity)
  66. }
  67. }
  68. }
  69. /// Represents types which cost in memory can be calculated.
  70. public protocol CacheCostCalculable {
  71. var cacheCost: Int { get }
  72. }
  73. /// Represents types which can be converted to and from data.
  74. public protocol DataTransformable {
  75. func toData() throws -> Data
  76. static func fromData(_ data: Data) throws -> Self
  77. static var empty: Self { get }
  78. }