Skip to content

Commit

Permalink
Merge pull request #215 from hotwired/cache-based-on-url
Browse files Browse the repository at this point in the history
Cache based on url
  • Loading branch information
olivaresf authored Jun 4, 2024
2 parents 5f42ee4 + d0c7e68 commit 97d0219
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
27 changes: 14 additions & 13 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 Down Expand Up @@ -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)
}

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 @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions Tests/PathConfigurationLoaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.")
}
Expand Down

0 comments on commit 97d0219

Please sign in to comment.