Skip to content

Commit

Permalink
Optional length
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBarba committed Sep 28, 2023
1 parent a3f736e commit fea2fe1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
5 changes: 1 addition & 4 deletions Sources/Compute/Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ 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)
}
let length = res.headers[.contentLength].flatMap(Int.init)
return await (.body(res.body.body, length: length), cachePolicy)
}
return try .init(trx)
Expand Down
22 changes: 15 additions & 7 deletions Sources/Compute/Fastly/FastlyCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import ComputeRuntime
extension Fastly {
public struct Cache: Sendable {

public static func getOrSet(_ key: String, _ handler: () async throws -> (HandlerData, CachePolicy)) async throws -> Transaction {
public typealias InsertResult = (data: HandlerData, cachePolicy: CachePolicy)

public static func getOrSet(_ key: String, _ handler: () async throws -> InsertResult) async throws -> Transaction {
// Open the transaction
let trx = try Transaction.lookup(key)

Expand Down Expand Up @@ -54,10 +56,10 @@ extension Fastly {

extension Fastly.Cache {
public enum HandlerData {
case body(_ body: Fastly.Body, length: Int)
case body(_ body: Fastly.Body, length: Int? = nil)
case bytes(_ bytes: [UInt8])

public var length: Int {
public var length: Int? {
switch self {
case .body(_, let length):
return length
Expand Down Expand Up @@ -85,14 +87,20 @@ extension Fastly.Cache {
return Transaction(handle)
}

public func insertAndStreamBack(cachePolicy: CachePolicy, length: Int) throws -> (body: Fastly.Body, transaction: Transaction) {
public func insertAndStreamBack(cachePolicy: CachePolicy, length: Int?) throws -> (body: Fastly.Body, transaction: Transaction) {
var bodyHandle: WasiHandle = 0
var cacheHandle: WasiHandle = 0
let options: CacheWriteOptions = [.staleWhileRevalidateNs, .length]
var options: CacheWriteOptions = []
var config = CacheWriteConfig()
config.max_age_ns = .init(cachePolicy.maxAge) * 1_000_000_000
config.stale_while_revalidate_ns = .init(cachePolicy.staleMaxAge) * 1_000_000_000
config.length = .init(length)
if cachePolicy.staleMaxAge > 0 {
options.insert(.staleWhileRevalidateNs)
config.stale_while_revalidate_ns = .init(cachePolicy.staleMaxAge) * 1_000_000_000
}
if let length {
options.insert(.length)
config.length = .init(length)
}
try wasi(fastly_cache__cache_transaction_insert_and_stream_back(handle, options.rawValue, &config, &bodyHandle, &cacheHandle))
return (.init(bodyHandle), .init(cacheHandle))
}
Expand Down

0 comments on commit fea2fe1

Please sign in to comment.