skip body dupe on expired without validators on Cache.get

This commit is contained in:
Muki Kiboigo committed 2026-08-03 18:17:43 -07:00
1 parent 04e93cd592
commit 3bfec6d7ff
1 file changed
+22 -10
+22 -10
View File
@@ -198,16 +198,6 @@ pub fn get(self: *SqliteCache, arena: std.mem.Allocator, req: CacheRequest) !?Ca
try headers.append(arena, .{ .name = name, .value = value });
}
var body_entry = try conn.row(
"select data from body where url = $1",
.{req.url},
) orelse {
log.debug(.cache, "miss", .{ .url = req.url, .reason = "missing body " });
return null;
};
defer body_entry.deinit();
const body = try arena.dupe(u8, body_entry.get(Blob, 0).data);
const metadata = CachedMetadata{
.url = try arena.dupeZ(u8, req.url),
.content_type = content_type,
@@ -225,6 +215,28 @@ pub fn get(self: *SqliteCache, arena: std.mem.Allocator, req: CacheRequest) !?Ca
};
const expired = metadata.isStale(req.timestamp);
// If expired with no validators, this entry is going to get evicted anyways so we can
// skip the body fetch + dupe.
if (expired and !metadata.hasValidators()) {
log.debug(.cache, "hit", .{ .url = req.url, .expired = true, .body = "skipped" });
return .{
.metadata = metadata,
.data = .{ .buffer = "" },
.expired = true,
};
}
var body_entry = try conn.row(
"select data from body where url = $1",
.{req.url},
) orelse {
log.debug(.cache, "miss", .{ .url = req.url, .reason = "missing body " });
return null;
};
defer body_entry.deinit();
const body = try arena.dupe(u8, body_entry.get(Blob, 0).data);
log.debug(.cache, "hit", .{ .url = req.url, .expired = expired });
return .{