Storage.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. public enum StorageExpiration {
  28. case never
  29. case seconds(TimeInterval)
  30. case days(Int)
  31. case date(Date)
  32. func dateSince(_ date: Date) -> Date {
  33. switch self {
  34. case .never: return .distantFuture
  35. case .seconds(let seconds): return date.addingTimeInterval(seconds)
  36. case .days(let days): return date.addingTimeInterval(TimeInterval(60 * 60 * 24 * days))
  37. case .date(let ref): return ref
  38. }
  39. }
  40. var timeInterval: TimeInterval {
  41. switch self {
  42. case .never: return .infinity
  43. case .seconds(let seconds): return seconds
  44. case .days(let days): return TimeInterval(60 * 60 * 24 * days)
  45. case .date(let ref): return ref.timeIntervalSinceNow
  46. }
  47. }
  48. }
  49. protocol Storage {
  50. associatedtype ValueType
  51. associatedtype KeyType
  52. func store(
  53. value: ValueType,
  54. forKey key: KeyType,
  55. expiration: StorageExpiration?) throws
  56. func value(forKey key: KeyType) throws -> ValueType?
  57. func remove(forKey key: String) throws
  58. func removeAll() throws
  59. func isCached(forKey key: String) -> Bool
  60. }
  61. class StorageObject<T> {
  62. let value: T
  63. let expiration: StorageExpiration
  64. private(set) var estimatedExpiration: Date
  65. init(_ value: T, expiration: StorageExpiration) {
  66. self.value = value
  67. self.expiration = expiration
  68. self.estimatedExpiration = expiration.dateSince(Date())
  69. }
  70. func extendExpiration() {
  71. self.estimatedExpiration = expiration.dateSince(Date())
  72. }
  73. }
  74. public protocol CacheCostCalculatable {
  75. var cacheCost: Int { get }
  76. }
  77. public protocol DataTransformable {
  78. func toData() throws -> Data
  79. static func fromData(_ data: Data) throws -> Self
  80. static var empty: Self { get }
  81. }