@dazejs/framework
Version:
Daze.js - A powerful web framework for Node.js
65 lines • 1.89 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MemoryCacheStore = void 0;
const store_1 = require("./store");
class MemoryCacheStore extends store_1.CacheStore {
constructor() {
super();
this.storage = new Map();
}
async get(key) {
var _a;
if (!this.storage.has(key))
return;
const item = this.storage.get(key);
const expiresAt = (_a = item.expiresAt) !== null && _a !== void 0 ? _a : 0;
if (expiresAt !== 0 && Date.now() > expiresAt) {
this.remove(key);
return;
}
return item.value;
}
async set(key, value, seconds) {
const item = {
value,
expiresAt: seconds ? Date.now() + seconds * 1000 : 0
};
this.storage.set(key, item);
return true;
}
async add(key, value, seconds) {
if (await this.get(key))
return false;
return this.set(key, value, seconds);
}
async increment(key, value = 1) {
const existing = await this.get(key);
if (existing !== undefined && existing !== null) {
value = existing + 1;
const item = this.storage.get(key);
item.value = value;
this.storage.set(key, item);
}
await this.forever(key, value);
return value;
}
async decrement(key, value = 1) {
return this.increment(key, value * -1);
}
async forever(key, value) {
return this.set(key, value, 0);
}
async remove(key) {
if (this.storage.has(key)) {
this.storage.delete(key);
return true;
}
return false;
}
async flush() {
this.storage = new Map();
return true;
}
}
exports.MemoryCacheStore = MemoryCacheStore;
//# sourceMappingURL=memory.js.map