1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
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;
}
|