Storage.swift 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 estimatedExpirationSince(_ 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 estimatedExpirationSinceNow: Date {
  51. return estimatedExpirationSince(Date())
  52. }
  53. var timeInterval: TimeInterval {
  54. switch self {
  55. case .never: return .infinity
  56. case .seconds(let seconds): return seconds
  57. case .days(let days): return TimeInterval(60 * 60 * 24 * days)
  58. case .date(let ref): return ref.timeIntervalSinceNow
  59. }
  60. }
  61. }
  62. protocol StorageBackend {
  63. associatedtype ValueType
  64. associatedtype KeyType
  65. func store(
  66. value: ValueType,
  67. forKey key: KeyType,
  68. expiration: StorageExpiration?) throws
  69. func value(forKey key: KeyType) throws -> ValueType?
  70. func remove(forKey key: String) throws
  71. func removeAll() throws
  72. func isCached(forKey key: String) -> Bool
  73. }
  74. /// Represents types which cost in memory can be calculated.
  75. public protocol CacheCostCalculatable {
  76. var cacheCost: Int { get }
  77. }
  78. /// Represents types which can be converted to and from data.
  79. public protocol DataTransformable {
  80. func toData() throws -> Data
  81. static func fromData(_ data: Data) throws -> Self
  82. static var empty: Self { get }
  83. }