use CacheRequest instead of key

This commit is contained in:
Muki Kiboigo
2026-03-26 16:20:38 -07:00
parent 368ac00573
commit def6e564d2
3 changed files with 15 additions and 10 deletions

View File

@@ -336,7 +336,7 @@ fn processRequest(self: *Client, req: Request) !void {
const arena = try self.network.app.arena_pool.acquire();
defer self.network.app.arena_pool.release(arena);
if (cache.get(arena, req.url)) |cached| {
if (cache.get(arena, .{ .url = req.url })) |cached| {
log.debug(.browser, "http.cache.get", .{
.url = req.url,
.found = true,
@@ -978,7 +978,7 @@ fn processMessages(self: *Client) !bool {
log.err(.browser, "http cache", .{ .key = cache_key, .metadata = metadata });
cache.put(
cache_key,
.{ .url = cache_key },
metadata,
transfer.body.items,
) catch |err| log.warn(.http, "cache put failed", .{ .err = err });

View File

@@ -28,15 +28,15 @@ kind: union(enum) {
fs: FsCache,
},
pub fn get(self: *Cache, arena: std.mem.Allocator, key: []const u8) ?CachedResponse {
pub fn get(self: *Cache, arena: std.mem.Allocator, req: CacheRequest) ?CachedResponse {
return switch (self.kind) {
inline else => |*c| c.get(arena, key),
inline else => |*c| c.get(arena, req),
};
}
pub fn put(self: *Cache, key: []const u8, metadata: CachedMetadata, body: []const u8) !void {
pub fn put(self: *Cache, req: CacheRequest, metadata: CachedMetadata, body: []const u8) !void {
return switch (self.kind) {
inline else => |*c| c.put(key, metadata, body),
inline else => |*c| c.put(req, metadata, body),
};
}
@@ -189,6 +189,10 @@ pub const CachedMetadata = struct {
}
};
pub const CacheRequest = struct {
url: []const u8,
};
pub const CachedData = union(enum) {
buffer: []const u8,
file: std.fs.File,

View File

@@ -19,6 +19,7 @@
const std = @import("std");
const Cache = @import("Cache.zig");
const Http = @import("../http.zig");
const CacheRequest = Cache.CacheRequest;
const CachedMetadata = Cache.CachedMetadata;
const CachedResponse = Cache.CachedResponse;
@@ -96,8 +97,8 @@ pub fn cache(self: *FsCache) Cache {
return Cache.init(self);
}
pub fn get(self: *FsCache, arena: std.mem.Allocator, key: []const u8) ?Cache.CachedResponse {
const hashed_key = hashKey(key);
pub fn get(self: *FsCache, arena: std.mem.Allocator, req: CacheRequest) ?Cache.CachedResponse {
const hashed_key = hashKey(req.url);
const meta_p = metaPath(&hashed_key);
const body_p = bodyPath(&hashed_key);
@@ -141,8 +142,8 @@ pub fn get(self: *FsCache, arena: std.mem.Allocator, key: []const u8) ?Cache.Cac
};
}
pub fn put(self: *FsCache, key: []const u8, meta: CachedMetadata, body: []const u8) !void {
const hashed_key = hashKey(key);
pub fn put(self: *FsCache, req: CacheRequest, meta: CachedMetadata, body: []const u8) !void {
const hashed_key = hashKey(req.url);
const meta_p = metaPath(&hashed_key);
const meta_tmp_p = metaTmpPath(&hashed_key);
const body_p = bodyPath(&hashed_key);