api: implement redis/memory store for cache

This commit is contained in:
jj
2024-10-30 19:06:46 +00:00
parent 5e92b649a3
commit 18acad19b9
6 changed files with 199 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
const _stores = new Set();
export class Store {
id;
constructor(name) {
name = name.toUpperCase();
if (_stores.has(name))
throw `${name} store already exists`;
_stores.add(name);
this.id = name;
}
async _get(_key) { throw "needs implementation" }
async get(key) {
if (typeof key !== 'string') {
key = key.toString();
}
const val = await this._get(key);
if (val === null)
return null;
return val;
}
async _set(_key, _val, _exp_sec = -1) { throw "needs implementation" }
set(key, val, exp_sec = -1) {
if (typeof key !== 'string') {
key = key.toString();
}
exp_sec = Math.round(exp_sec);
return this._set(key, val, exp_sec);
}
};