diff --git a/src/browser/HttpClient.zig b/src/browser/HttpClient.zig index c06b19c9..6437e79d 100644 --- a/src/browser/HttpClient.zig +++ b/src/browser/HttpClient.zig @@ -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 }); diff --git a/src/network/cache/Cache.zig b/src/network/cache/Cache.zig index 17cf199c..7a1e8521 100644 --- a/src/network/cache/Cache.zig +++ b/src/network/cache/Cache.zig @@ -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, diff --git a/src/network/cache/FsCache.zig b/src/network/cache/FsCache.zig index 87df4f83..a34c7a83 100644 --- a/src/network/cache/FsCache.zig +++ b/src/network/cache/FsCache.zig @@ -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);