Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map URL to cached data #23

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions Source/Turbo/Path Configuration/PathConfigurationLoader.swift
Original file line number Diff line number Diff line change
@@ -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?
@@ -35,7 +34,7 @@ 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() {
if let data = cachedData(for: url) {
loadData(data)
}

@@ -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
@@ -94,8 +95,8 @@ final class PathConfigurationLoader {
return directory.appendingPathComponent(cacheDirectory)
}

var configurationCacheURL: URL {
cacheDirectoryURL.appendingPathComponent(configurationCacheFilename)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the configurationCacheFilename constant be removed above now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, removed!

func configurationCacheURL(for url: URL) -> URL {
cacheDirectoryURL.appendingPathComponent(url.lastPathComponent)
}

// MARK: - File
@@ -113,17 +114,17 @@ final class PathConfigurationLoader {

// MARK: - Data

private func loadData(_ data: Data, cache: Bool = false) {
private func loadData(_ data: Data, cache: Bool = false, for url: URL? = nil) {
do {
guard let json = try JSONSerialization.jsonObject(with: data) as? [String: Any] else {
throw JSONDecodingError.invalidJSON
}

let config = try PathConfigurationDecoder(json: json)

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

updateHandler(with: config)
4 changes: 2 additions & 2 deletions Tests/Turbo/PathConfigurationLoaderTests.swift
Original file line number Diff line number Diff line change
@@ -55,7 +55,7 @@ class PathConfigurationLoaderTests: XCTestCase {
wait(for: [expectation])

XCTAssertTrue(handlerCalled)
XCTAssertTrue(FileManager.default.fileExists(atPath: loader.configurationCacheURL.path))
XCTAssertTrue(FileManager.default.fileExists(atPath: loader.configurationCacheURL(for: serverURL).path))
}

private func stubRequest(for loader: PathConfigurationLoader) -> XCTestExpectation {
@@ -64,7 +64,7 @@ class PathConfigurationLoaderTests: XCTestCase {
return HTTPStubsResponse(jsonObject: json, statusCode: 200, headers: [:])
}

clearCache(loader.configurationCacheURL)
clearCache(loader.configurationCacheURL(for: serverURL))

return expectation(description: "Wait for configuration to load.")
}