Storage.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. func dateSince(_ date: Date) -> Date {
  43. switch self {
  44. case .never: return .distantFuture
  45. case .seconds(let seconds): return date.addingTimeInterval(seconds)
  46. case .days(let days): return date.addingTimeInterval(TimeInterval(60 * 60 * 24 * days))
  47. case .date(let ref): return ref
  48. }
  49. }
  50. var timeInterval: TimeInterval {
  51. switch self {
  52. case .never: return .infinity
  53. case .seconds(let seconds): return seconds
  54. case .days(let days): return TimeInterval(60 * 60 * 24 * days)
  55. case .date(let ref): return ref.timeIntervalSinceNow
  56. }
  57. }
  58. }
  59. protocol Storage {
  60. associatedtype ValueType
  61. associatedtype KeyType
  62. func store(
  63. value: ValueType,
  64. forKey key: KeyType,
  65. expiration: StorageExpiration?) throws
  66. func value(forKey key: KeyType) throws -> ValueType?
  67. func remove(forKey key: String) throws
  68. func removeAll() throws
  69. func isCached(forKey key: String) -> Bool
  70. }
  71. class StorageObject<T> {
  72. let value: T
  73. let expiration: StorageExpiration
  74. private(set) var estimatedExpiration: Date
  75. init(_ value: T, expiration: StorageExpiration) {
  76. self.value = value
  77. self.expiration = expiration
  78. self.estimatedExpiration = expiration.dateSince(Date())
  79. }
  80. func extendExpiration() {
  81. self.estimatedExpiration = expiration.dateSince(Date())
  82. }
  83. }
  84. /// Represents types which cost in memory can be calculated.
  85. public protocol CacheCostCalculatable {
  86. var cacheCost: Int { get }
  87. }
  88. /// Represents types which can be converted to and from data.
  89. public protocol DataTransformable {
  90. func toData() throws -> Data
  91. static func fromData(_ data: Data) throws -> Self
  92. static var empty: Self { get }
  93. }