-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b6356e
commit 806dd93
Showing
3 changed files
with
143 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// | ||
// Cache.swift | ||
// | ||
// | ||
// Created by Andrew Barba on 9/13/23. | ||
// | ||
|
||
public struct Cache: Sendable { | ||
|
||
public static func getOrSet(_ key: String, _ handler: () async throws -> (FetchResponse, CachePolicy)) async throws -> Entry { | ||
let trx = try await Fastly.Cache.getOrSet(key) { | ||
let (res, cachePolicy) = try await handler() | ||
guard let header = res.headers[.contentLength], let length = Int(header) else { | ||
let bytes = try await res.bytes() | ||
return (.bytes(bytes), cachePolicy) | ||
} | ||
return await (.body(res.body.body, length: length), cachePolicy) | ||
} | ||
return try .init(trx) | ||
} | ||
|
||
public static func getOrSet(_ key: String, _ handler: () async throws -> ([UInt8], CachePolicy)) async throws -> Entry { | ||
let trx = try await Fastly.Cache.getOrSet(key) { | ||
let (bytes, cachePolicy) = try await handler() | ||
return (.bytes(bytes), cachePolicy) | ||
} | ||
return try .init(trx) | ||
} | ||
|
||
public static func getOrSet(_ key: String, _ handler: () async throws -> (String, CachePolicy)) async throws -> Entry { | ||
let trx = try await Fastly.Cache.getOrSet(key) { | ||
let (text, cachePolicy) = try await handler() | ||
return (.bytes(.init(text.utf8)), cachePolicy) | ||
} | ||
return try .init(trx) | ||
} | ||
|
||
public static func getOrSet(_ key: String, _ handler: () async throws -> (Data, CachePolicy)) async throws -> Entry { | ||
let trx = try await Fastly.Cache.getOrSet(key) { | ||
let (data, cachePolicy) = try await handler() | ||
return (.bytes(data.bytes), cachePolicy) | ||
} | ||
return try .init(trx) | ||
} | ||
|
||
public static func getOrSet(_ key: String, _ handler: () async throws -> ([String: Sendable], CachePolicy)) async throws -> Entry { | ||
let trx = try await Fastly.Cache.getOrSet(key) { | ||
let (json, cachePolicy) = try await handler() | ||
let data = try JSONSerialization.data(withJSONObject: json) | ||
return (.bytes(data.bytes), cachePolicy) | ||
} | ||
return try .init(trx) | ||
} | ||
|
||
public static func getOrSet(_ key: String, _ handler: () async throws -> ([Sendable], CachePolicy)) async throws -> Entry { | ||
let trx = try await Fastly.Cache.getOrSet(key) { | ||
let (json, cachePolicy) = try await handler() | ||
let data = try JSONSerialization.data(withJSONObject: json) | ||
return (.bytes(data.bytes), cachePolicy) | ||
} | ||
return try .init(trx) | ||
} | ||
} | ||
|
||
extension Cache { | ||
|
||
public struct Entry: Sendable { | ||
|
||
public let body: ReadableBody | ||
|
||
public let age: Int | ||
|
||
public let hits: Int | ||
|
||
public let contentLength: Int | ||
|
||
internal init(_ trx: Fastly.Cache.Transaction) throws { | ||
self.body = try ReadableWasiBody(trx.getBody()) | ||
self.age = try trx.getAge() | ||
self.hits = try trx.getHits() | ||
self.contentLength = try trx.getLength() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters