check expiration whenever we get entry in CorsStore

This commit is contained in:
Muki Kiboigo committed 2026-09-23 07:44:54 -07:00
1 parent cc42b9f9c3
commit b74d11e803
1 file changed
+14 -8
+14 -8
View File
@@ -135,13 +135,8 @@ pub fn deinit(self: *CorsStore) void {
self.map.deinit();
}
pub fn get(self: *CorsStore, key: Key) !?Entry {
const cache_key = try key.build(self.allocator);
defer self.allocator.free(cache_key);
self.mutex.lockUncancelable(lp.io);
defer self.mutex.unlock(lp.io);
// Caller is expected to be holding mutex.
fn getWithExpiration(self: *CorsStore, cache_key: []const u8) ?*Entry {
const entry = self.map.get(cache_key) orelse return null;
if (entry.expires_at <= lp.datetime.milliTimestamp(.real)) {
@@ -152,6 +147,17 @@ pub fn get(self: *CorsStore, key: Key) !?Entry {
return null;
}
return entry;
}
pub fn get(self: *CorsStore, key: Key) !?Entry {
const cache_key = try key.build(self.allocator);
defer self.allocator.free(cache_key);
self.mutex.lockUncancelable(lp.io);
defer self.mutex.unlock(lp.io);
const entry = self.getWithExpiration(cache_key) orelse return null;
return try entry.dupe(self.allocator);
}
@@ -170,7 +176,7 @@ pub fn put(self: *CorsStore, key: Key, entry: Entry) !void {
self.mutex.lockUncancelable(lp.io);
defer self.mutex.unlock(lp.io);
if (self.map.get(cache_key)) |existing| {
if (self.getWithExpiration(cache_key)) |existing| {
const merged = try existing.merge(self.allocator, entry);
existing.deinit(self.allocator);
existing.* = merged;