const std = @import("std"); const config = @import("../../config.zig"); const Storage = @import("../root.zig"); pub const Session = @import("session.zig"); const Self = @This(); pub const empty: Self = .{ .cache = .empty, }; cache: std.StringHashMapUnmanaged(Session), pub fn add(self: *Self, storage: *Storage, info: Storage.User.Info) !*Session { const session = try Session.init(storage.allocator, info); errdefer session.deinit(storage.allocator); try self.cache.put(storage.allocator, session.fingerprint, session); return self.cache.getPtr(session.fingerprint) orelse unreachable; } pub fn renew(self: *Self, storage: *Storage, fingerprint: []const u8) !*Session { if (self.cache.get(fingerprint)) |s| { var session = s; try session.reset(); try self.cache.put(storage.allocator, session.fingerprint, session); _ = self.cache.remove(fingerprint); return self.cache.getPtr(session.fingerprint) orelse unreachable; } return error.SessionNotFound; } pub fn remove(self: *Self, storage: *Storage, fingerprint: []const u8) void { if (self.cache.getPtr(fingerprint)) |session| { session.deinit(storage.allocator); _ = self.cache.remove(fingerprint); } } pub fn get(self: *Self, storage: *Storage, fingerprint: []const u8) ?*Session { if (self.cache.getPtr(fingerprint)) |session| { const now = std.time.Instant.now() catch return null; const since = now.since(session.age); if (since > config.session_expires_after) { std.debug.print("here\n", .{}); session.deinit(storage.allocator); _ = self.cache.remove(fingerprint); return null; } return session; } return null; }