Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
QuiteAFancyEmerald
GitHub Repository: QuiteAFancyEmerald/Holy-Unblocker
Path: blob/master/lib/rammerhead/src/classes/RammerheadMemoryStore.js
5253 views
1
const RammerheadLogging = require('./RammerheadLogging');
2
const RammerheadSession = require('./RammerheadSession');
3
const RammerheadSessionAbstractStore = require('./RammerheadSessionAbstractStore');
4
5
class RammerheadSessionMemoryStore extends RammerheadSessionAbstractStore {
6
/**
7
* @param {object} options
8
* @param {RammerheadLogging|undefined} options.logger
9
* @param {number|null} options.staleTimeout - if inactivity goes beyond this, then the session is deleted. null to disable
10
* @param {number|null} options.maxToLive - if now - createdAt surpasses maxToLive, then the session is deleted. null to disable
11
* @param {number} options.cleanupInterval - every cleanupInterval ms will run a cleanup check
12
*/
13
constructor({
14
logger = new RammerheadLogging({ logLevel: 'disabled' }),
15
staleTimeout = 1000 * 60 * 30, // 30 minutes
16
maxToLive = 1000 * 60 * 60 * 4, // 4 hours
17
cleanupInterval = 1000 * 60 * 1 // 1 minute
18
} = {}) {
19
super();
20
this.logger = logger;
21
this.mapStore = new Map();
22
setInterval(() => this._cleanupRun(staleTimeout, maxToLive), cleanupInterval).unref();
23
}
24
25
/**
26
* @returns {string[]} - list of session ids in store
27
*/
28
keys() {
29
return Array.from(this.mapStore.keys());
30
}
31
/**
32
* @param {string} id
33
* @returns {boolean}
34
*/
35
has(id) {
36
const exists = this.mapStore.has(id);
37
this.logger.debug(`(MemoryStore.has) ${id} ${exists}`);
38
return exists;
39
}
40
/**
41
* @param {string} id
42
* @param {boolean} updateActiveTimestamp
43
* @returns {RammerheadSession|undefined}
44
*/
45
get(id, updateActiveTimestamp = true) {
46
if (!this.has(id)) return;
47
this.logger.debug(`(MemoryStore.get) ${id} ${updateActiveTimestamp}`);
48
49
const session = this.mapStore.get(id);
50
if (updateActiveTimestamp) session.updateLastUsed();
51
52
return session;
53
}
54
/**
55
* @param {string} id
56
* @returns {RammerheadSession}
57
*/
58
add(id) {
59
if (this.has(id)) throw new Error('the following session already exists: ' + id);
60
this.logger.debug(`(MemoryStore.add) ${id}`);
61
const session = new RammerheadSession({ id });
62
this.mapStore.set(id, session);
63
return session;
64
}
65
/**
66
* @param {string} id
67
* @returns {boolean} - returns true when a delete operation is performed
68
*/
69
delete(id) {
70
return this.mapStore.delete(id);
71
}
72
/**
73
* @param {string} id
74
* @param {string} serializedSession
75
*/
76
addSerializedSession(id, serializedSession) {
77
this.logger.debug(`(MemoryStore.addSerializedSession) adding serialized session id ${id} to store`);
78
const session = RammerheadSession.DeserializeSession(id, serializedSession);
79
session.updateLastUsed();
80
this.mapStore.set(id, session);
81
this.logger.debug(`(FileCache.addSerializedSession) added ${id}`);
82
}
83
84
/**
85
* @private
86
* @param {number|null} staleTimeout
87
* @param {number|null} maxToLive
88
*/
89
_cleanupRun(staleTimeout, maxToLive) {
90
this.logger.debug(`(MemoryStore._cleanupRun) cleanup run. Need to go through ${this.mapStore.size} sessions`);
91
92
const now = Date.now();
93
for (const [sessionId, session] of this.mapStore) {
94
if (
95
(staleTimeout && now - session.lastUsed > staleTimeout) ||
96
(maxToLive && now - session.createdAt > maxToLive)
97
) {
98
this.mapStore.delete(sessionId);
99
this.logger.debug(`(MemoryStore._cleanupRun) delete ${sessionId}`);
100
}
101
}
102
103
this.logger.debug('(MemoryStore._cleanupRun) finished cleanup run');
104
}
105
}
106
107
module.exports = RammerheadSessionMemoryStore;
108
109