Skip to content

Commit

Permalink
Use URL filename as cache key
Browse files Browse the repository at this point in the history
  • Loading branch information
olivaresf committed May 29, 2024
1 parent 5f42ee4 commit 7227492
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions Source/Path Configuration/PathConfigurationLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ typealias PathConfigurationLoaderCompletionHandler = (PathConfigurationDecoder)

final class PathConfigurationLoader {
private let cacheDirectory = "Turbo"
private let configurationCacheFilename = "path-configuration.json"
private let sources: [PathConfiguration.Source]
private let options: PathConfigurationLoaderOptions?
private var completionHandler: PathConfigurationLoaderCompletionHandler?
Expand All @@ -20,7 +19,7 @@ final class PathConfigurationLoader {
for source in sources {
switch source {
case .data(let data):
loadData(data)
loadData(data, for: .PathDataTemporaryURL)
case .file(let url):
loadFile(url)
case .server(let url):
Expand All @@ -35,8 +34,8 @@ final class PathConfigurationLoader {
precondition(!url.isFileURL, "URL provided for server is a file url")

// Immediately load most recent cached version if available
if let data = cachedData() {
loadData(data)
if let data = cachedData(for: url) {
loadData(data, for: url)
}

let session = options?.urlSessionConfiguration.map { URLSession(configuration: $0) } ?? URLSession.shared
Expand All @@ -50,29 +49,31 @@ final class PathConfigurationLoader {
return
}

self?.loadData(data, cache: true)
self?.loadData(data, cache: true, for: url)
}.resume()
}

// MARK: - Caching

private func cacheRemoteData(_ data: Data) {
private func cacheRemoteData(_ data: Data, for url: URL) {
createCacheDirectoryIfNeeded()

do {
try data.write(to: configurationCacheURL)
let url = configurationCacheURL(for: url)
try data.write(to: url)
} catch {
debugPrint("[path-configuration-loader] error caching file error: \(error)")
}
}

private func cachedData() -> Data? {
guard FileManager.default.fileExists(atPath: configurationCacheURL.path) else {
private func cachedData(for url: URL) -> Data? {
let cachedURL = configurationCacheURL(for: url)
guard FileManager.default.fileExists(atPath: cachedURL.path) else {
return nil
}

do {
return try Data(contentsOf: configurationCacheURL)
return try Data(contentsOf: cachedURL)
} catch {
debugPrint("[path-configuration-loader] *** error loading cached data: \(error)")
return nil
Expand All @@ -94,8 +95,8 @@ final class PathConfigurationLoader {
return directory.appendingPathComponent(cacheDirectory)
}

var configurationCacheURL: URL {
turboCacheDirectoryURL.appendingPathComponent(configurationCacheFilename)
func configurationCacheURL(for url: URL) -> URL {
turboCacheDirectoryURL.appendingPathComponent(url.lastPathComponent)
}

// MARK: - File
Expand All @@ -105,15 +106,15 @@ final class PathConfigurationLoader {

do {
let data = try Data(contentsOf: url)
loadData(data)
loadData(data, for: url)
} catch {
debugPrint("[path-configuration] *** error loading configuration from file: \(url), error: \(error)")
}
}

// MARK: - Data

private func loadData(_ data: Data, cache: Bool = false) {
private func loadData(_ data: Data, cache: Bool = false, for url: URL) {
do {
guard let json = try JSONSerialization.jsonObject(with: data) as? [String: Any] else {
throw JSONDecodingError.invalidJSON
Expand All @@ -123,7 +124,7 @@ final class PathConfigurationLoader {

if cache {
// Only cache once we ensure we have valid data
cacheRemoteData(data)
cacheRemoteData(data, for: url)
}

updateHandler(with: config)
Expand All @@ -144,3 +145,7 @@ final class PathConfigurationLoader {
}
}
}

private extension URL {
static let PathDataTemporaryURL = URL(string: "https://localhost/path-configuration.json")!
}

0 comments on commit 7227492

Please sign in to comment.