mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-06-11 09:35:59 -04:00
add Cache.revalidate + supporting logic
This commit is contained in:
22
src/network/cache/Cache.zig
vendored
22
src/network/cache/Cache.zig
vendored
@@ -55,6 +55,12 @@ pub fn evict(self: *Cache, url: []const u8) void {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn revalidate(self: *Cache, arena: std.mem.Allocator, url: []const u8, timestamp: i64) !void {
|
||||
return switch (self.kind) {
|
||||
inline else => |*c| c.revalidate(arena, url, timestamp),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn clear(self: *Cache) !void {
|
||||
return switch (self.kind) {
|
||||
inline else => |*c| c.clear(),
|
||||
@@ -122,10 +128,13 @@ pub const CachedMetadata = struct {
|
||||
cache_control: CacheControl,
|
||||
/// Response Headers
|
||||
headers: []const Http.Header,
|
||||
|
||||
/// These are Request Headers used by Vary.
|
||||
vary_headers: []const Http.Header,
|
||||
|
||||
// Validators for conditional requests.
|
||||
etag: ?[]const u8 = null,
|
||||
last_modified: ?[]const u8 = null,
|
||||
|
||||
pub fn format(self: CachedMetadata, writer: *std.Io.Writer) !void {
|
||||
try writer.print("url={s} | status={d} | content_type={s} | max_age={d} | vary=[", .{
|
||||
self.url,
|
||||
@@ -145,6 +154,15 @@ pub const CachedMetadata = struct {
|
||||
}
|
||||
try writer.print("]", .{});
|
||||
}
|
||||
|
||||
pub fn isStale(self: CachedMetadata, timestamp: i64) bool {
|
||||
const age = (timestamp - self.stored_at) + @as(i64, @intCast(self.age_at_store));
|
||||
return age >= @as(i64, @intCast(self.cache_control.max_age));
|
||||
}
|
||||
|
||||
pub fn hasValidators(self: CachedMetadata) bool {
|
||||
return self.etag != null or self.last_modified != null;
|
||||
}
|
||||
};
|
||||
|
||||
pub const CacheRequest = struct {
|
||||
@@ -172,8 +190,10 @@ pub const CachedData = union(enum) {
|
||||
pub const CachedResponse = struct {
|
||||
metadata: CachedMetadata,
|
||||
data: CachedData,
|
||||
expired: bool,
|
||||
|
||||
pub fn format(self: *const CachedResponse, writer: *std.Io.Writer) !void {
|
||||
try writer.print("expired={}, ", .{self.expired});
|
||||
try writer.print("metadata=(", .{});
|
||||
try self.metadata.format(writer);
|
||||
try writer.print("), data=", .{});
|
||||
|
||||
Reference in New Issue
Block a user