diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2023-06-12 11:52:10 +0200 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2023-06-13 16:25:10 +0200 |
| commit | b6a59eb7d162c97dd8827eeed46cfca7d2eef171 (patch) | |
| tree | e527fc18d4eff5699bad03b7acbb685972a2b5fd /ios/RelayCache | |
| parent | e84905793ac54c4768a416f76dd4589e1f66fcf1 (diff) | |
| download | mullvadvpn-b6a59eb7d162c97dd8827eeed46cfca7d2eef171.tar.xz mullvadvpn-b6a59eb7d162c97dd8827eeed46cfca7d2eef171.zip | |
Replace Caching type with FileCache<Content: Codable>
Diffstat (limited to 'ios/RelayCache')
| -rw-r--r-- | ios/RelayCache/CachedRelays.swift | 2 | ||||
| -rw-r--r-- | ios/RelayCache/RelayCache.swift | 32 |
2 files changed, 16 insertions, 18 deletions
diff --git a/ios/RelayCache/CachedRelays.swift b/ios/RelayCache/CachedRelays.swift index 75a775c650..499eb9cde3 100644 --- a/ios/RelayCache/CachedRelays.swift +++ b/ios/RelayCache/CachedRelays.swift @@ -10,7 +10,7 @@ import Foundation import MullvadREST /// A struct that represents the relay cache on disk -public struct CachedRelays: Codable { +public struct CachedRelays: Codable, Equatable { /// E-tag returned by server public let etag: String? diff --git a/ios/RelayCache/RelayCache.swift b/ios/RelayCache/RelayCache.swift index ed1cbf4c9e..abce823786 100644 --- a/ios/RelayCache/RelayCache.swift +++ b/ios/RelayCache/RelayCache.swift @@ -10,26 +10,24 @@ import Foundation import MullvadREST import MullvadTypes -public final class RelayCache: Caching { - public typealias CacheType = CachedRelays +public final class RelayCache { + private let fileCache: any FileCacheProtocol<CachedRelays> - /// Cache file location. - public let cacheFileURL: URL - public static let cacheFileName = "relays.json" + /// Designated initializer + public init(cacheDirectory: URL) { + fileCache = FileCache(fileURL: cacheDirectory.appendingPathComponent("relays.json", isDirectory: false)) + } - public init(cacheFolder: URL) { - let cacheFileURL = cacheFolder.appendingPathComponent( - Self.cacheFileName, - isDirectory: false - ) - self.cacheFileURL = cacheFileURL + /// Initializer that accepts a custom FileCache implementation. Used in tests. + init(fileCache: some FileCacheProtocol<CachedRelays>) { + self.fileCache = fileCache } /// Safely read the cache file from disk using file coordinator and fallback to prebundled /// relays in case if the relay cache file is missing. public func read() throws -> CachedRelays { do { - return try readFromDisk() + return try fileCache.read() } catch { if error is DecodingError || (error as? CocoaError)?.code == .fileReadNoSuchFile { return try readPrebundledRelays() @@ -41,16 +39,16 @@ public final class RelayCache: Caching { /// Safely write the cache file on disk using file coordinator. public func write(record: CachedRelays) throws { - try writeToDisk(record) + try fileCache.write(record) } /// Read pre-bundled relays file from disk. private func readPrebundledRelays() throws -> CachedRelays { - guard let prebundledRelaysFileURL = Bundle(for: Self.self) - .url(forResource: "relays", withExtension: "json") else { throw POSIXError(.ENOENT) } + guard let prebundledRelaysFileURL = Bundle(for: Self.self).url(forResource: "relays", withExtension: "json") + else { throw CocoaError(.fileNoSuchFile) } + let data = try Data(contentsOf: prebundledRelaysFileURL) - let relays = try REST.Coding.makeJSONDecoder() - .decode(REST.ServerRelaysResponse.self, from: data) + let relays = try REST.Coding.makeJSONDecoder().decode(REST.ServerRelaysResponse.self, from: data) return CachedRelays( relays: relays, |
