Skip to content

Instantly share code, notes, and snippets.

@davbeck
Created May 11, 2022
Embed
What would you like to do?
func fetch<Result: Codable>(_ endpoint: Endpoint<Result>, cacheBehavior: CacheBehavior = .cacheElseNetwork) async throws -> Result {
switch cacheBehavior {
case .cacheElseNetwork:
if let cached = self.cache[endpoint.cacheKey] {
return cached.payload
} else {
return try await self.networkFetch(endpoint)
}
case .networkOnly:
return try await self.networkFetch(endpoint)
case .cacheOnly:
if let cached = self.cache[endpoint.cacheKey] {
return cached.payload
} else {
throw Error.cacheMiss
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment