Path: blob/master/lib/rammerhead/src/classes/RammerheadMemoryStore.js
5253 views
const RammerheadLogging = require('./RammerheadLogging');1const RammerheadSession = require('./RammerheadSession');2const RammerheadSessionAbstractStore = require('./RammerheadSessionAbstractStore');34class RammerheadSessionMemoryStore extends RammerheadSessionAbstractStore {5/**6* @param {object} options7* @param {RammerheadLogging|undefined} options.logger8* @param {number|null} options.staleTimeout - if inactivity goes beyond this, then the session is deleted. null to disable9* @param {number|null} options.maxToLive - if now - createdAt surpasses maxToLive, then the session is deleted. null to disable10* @param {number} options.cleanupInterval - every cleanupInterval ms will run a cleanup check11*/12constructor({13logger = new RammerheadLogging({ logLevel: 'disabled' }),14staleTimeout = 1000 * 60 * 30, // 30 minutes15maxToLive = 1000 * 60 * 60 * 4, // 4 hours16cleanupInterval = 1000 * 60 * 1 // 1 minute17} = {}) {18super();19this.logger = logger;20this.mapStore = new Map();21setInterval(() => this._cleanupRun(staleTimeout, maxToLive), cleanupInterval).unref();22}2324/**25* @returns {string[]} - list of session ids in store26*/27keys() {28return Array.from(this.mapStore.keys());29}30/**31* @param {string} id32* @returns {boolean}33*/34has(id) {35const exists = this.mapStore.has(id);36this.logger.debug(`(MemoryStore.has) ${id} ${exists}`);37return exists;38}39/**40* @param {string} id41* @param {boolean} updateActiveTimestamp42* @returns {RammerheadSession|undefined}43*/44get(id, updateActiveTimestamp = true) {45if (!this.has(id)) return;46this.logger.debug(`(MemoryStore.get) ${id} ${updateActiveTimestamp}`);4748const session = this.mapStore.get(id);49if (updateActiveTimestamp) session.updateLastUsed();5051return session;52}53/**54* @param {string} id55* @returns {RammerheadSession}56*/57add(id) {58if (this.has(id)) throw new Error('the following session already exists: ' + id);59this.logger.debug(`(MemoryStore.add) ${id}`);60const session = new RammerheadSession({ id });61this.mapStore.set(id, session);62return session;63}64/**65* @param {string} id66* @returns {boolean} - returns true when a delete operation is performed67*/68delete(id) {69return this.mapStore.delete(id);70}71/**72* @param {string} id73* @param {string} serializedSession74*/75addSerializedSession(id, serializedSession) {76this.logger.debug(`(MemoryStore.addSerializedSession) adding serialized session id ${id} to store`);77const session = RammerheadSession.DeserializeSession(id, serializedSession);78session.updateLastUsed();79this.mapStore.set(id, session);80this.logger.debug(`(FileCache.addSerializedSession) added ${id}`);81}8283/**84* @private85* @param {number|null} staleTimeout86* @param {number|null} maxToLive87*/88_cleanupRun(staleTimeout, maxToLive) {89this.logger.debug(`(MemoryStore._cleanupRun) cleanup run. Need to go through ${this.mapStore.size} sessions`);9091const now = Date.now();92for (const [sessionId, session] of this.mapStore) {93if (94(staleTimeout && now - session.lastUsed > staleTimeout) ||95(maxToLive && now - session.createdAt > maxToLive)96) {97this.mapStore.delete(sessionId);98this.logger.debug(`(MemoryStore._cleanupRun) delete ${sessionId}`);99}100}101102this.logger.debug('(MemoryStore._cleanupRun) finished cleanup run');103}104}105106module.exports = RammerheadSessionMemoryStore;107108109