Storage.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // Storage.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 2018/10/15.
  6. //
  7. // Copyright (c) 2018年 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. protocol StorageBackend {
  70. associatedtype ValueType
  71. associatedtype KeyType
  72. func store(
  73. value: ValueType,
  74. forKey key: KeyType,
  75. expiration: StorageExpiration?) throws
  76. func value(forKey key: KeyType) throws -> ValueType?
  77. func remove(forKey key: String) throws
  78. func removeAll() throws
  79. func isCached(forKey key: String) -> Bool
  80. }
  81. /// Represents types which cost in memory can be calculated.
  82. public protocol CacheCostCalculatable {
  83. var cacheCost: Int { get }
  84. }
  85. /// Represents types which can be converted to and from data.
  86. public protocol DataTransformable {
  87. func toData() throws -> Data
  88. static func fromData(_ data: Data) throws -> Self
  89. static var empty: Self { get }
  90. }