feat: cache everything in node store not only wasm
This commit is contained in:
@@ -1,22 +1,22 @@
|
||||
import type { AsyncCache } from '@nodarium/types';
|
||||
import { openDB, type IDBPDatabase } from 'idb';
|
||||
|
||||
export class IndexDBCache implements AsyncCache<ArrayBuffer> {
|
||||
export class IndexDBCache implements AsyncCache<unknown> {
|
||||
|
||||
size: number = 100;
|
||||
|
||||
db: Promise<IDBPDatabase<ArrayBuffer>>;
|
||||
private _cache = new Map<string, ArrayBuffer>();
|
||||
db: Promise<IDBPDatabase<unknown>>;
|
||||
private _cache = new Map<string, unknown>();
|
||||
|
||||
constructor(id: string) {
|
||||
this.db = openDB<ArrayBuffer>('cache/' + id, 1, {
|
||||
this.db = openDB<unknown>('cache/' + id, 1, {
|
||||
upgrade(db) {
|
||||
db.createObjectStore('keyval');
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async get(key: string) {
|
||||
async get<T>(key: string): Promise<T> {
|
||||
let res = this._cache.get(key);
|
||||
if (!res) {
|
||||
res = await (await this.db).get('keyval', key);
|
||||
@@ -24,13 +24,33 @@ export class IndexDBCache implements AsyncCache<ArrayBuffer> {
|
||||
if (res) {
|
||||
this._cache.set(key, res);
|
||||
}
|
||||
return res;
|
||||
return res as T;
|
||||
}
|
||||
async set(key: string, value: ArrayBuffer) {
|
||||
|
||||
async getArrayBuffer(key: string) {
|
||||
const res = await this.get(key);
|
||||
if (!res) return;
|
||||
if (res instanceof ArrayBuffer) {
|
||||
return res;
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
async getString(key: string) {
|
||||
const res = await this.get(key);
|
||||
if (!res) return;
|
||||
if (typeof res === "string") {
|
||||
return res;
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
async set(key: string, value: unknown) {
|
||||
this._cache.set(key, value);
|
||||
const db = await this.db;
|
||||
await db.put('keyval', value, key);
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.db.then(db => db.clear('keyval'));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user