rankmycache
Version:
An easy-to-use cache providing service.
66 lines • 1.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.InMemoryAdapter = void 0;
class InMemoryAdapter {
constructor() {
this.client = {};
}
async get(key) {
const data = this.client[key];
if (!data || data instanceof Set) {
return null;
}
return JSON.parse(data);
}
async set(key, data) {
const encodedData = JSON.stringify(data);
this.client[key] = encodedData;
}
async delete(key) {
delete this.client[key];
}
handleError(err) {
if (err) {
return null;
}
return null;
}
async getSetMembers(key) {
const foundSet = this.client[key];
return ((foundSet &&
foundSet instanceof Set &&
Array.from(foundSet).map(String)) ||
null);
}
async addToSet(key, value) {
const valuesToAdd = Array.isArray(value) ? value : [value];
const foundSet = this.client[key];
if (foundSet && foundSet instanceof Set) {
valuesToAdd.map((value) => foundSet.add(value));
}
else {
this.client[key] = new Set(valuesToAdd);
}
}
async removeFromSet(key, value) {
const valuesToRemove = Array.isArray(value) ? value : [value];
const foundSet = this.client[key];
if (foundSet && foundSet instanceof Set) {
valuesToRemove.map((value) => foundSet.delete(value));
if (foundSet.size === 0) {
delete this.client[key];
}
}
}
async isSetMember(key, value) {
const foundSet = this.client[key];
return foundSet && foundSet instanceof Set && foundSet.has(value);
}
async expire(key, ttl) {
setTimeout(() => {
delete this.client[key];
}, ttl * 1000);
}
}
exports.InMemoryAdapter = InMemoryAdapter;
//# sourceMappingURL=in-memory-adapter.js.map