Compare commits

...

3 Commits

Author SHA1 Message Date
Derrick Hammer 616b74a820
*Wrap cache delete in try/catch
ci/woodpecker/push/woodpecker Pipeline failed Details
2022-11-28 02:06:39 -05:00
Derrick Hammer 4bb0636a8d
*Unneeded import 2022-11-28 02:04:35 -05:00
Derrick Hammer 69fd9a14ef
*Switch to node cache 2022-11-28 02:03:50 -05:00
2 changed files with 14 additions and 9 deletions

View File

@ -14,6 +14,7 @@ import { RPCServer } from "./server";
import orderedJSON from "ordered-json";
// @ts-ignore
import crypto from "hypercore-crypto";
import NodeCache from "node-cache";
export class RPCCache extends EventEmitter {
private dhtCache?: DHTCache;
@ -25,9 +26,9 @@ export class RPCCache extends EventEmitter {
return this._swarm;
}
private _data: RPCCacheData = {};
private _data: NodeCache = new NodeCache({ stdTTL: 60 * 60 * 24 });
get data(): RPCCacheData {
get data(): NodeCache {
return this._data;
}
@ -38,6 +39,11 @@ export class RPCCache extends EventEmitter {
this.dhtCache = new DHTCache(this._swarm, {
protocol: "lumeweb.rpccache",
});
this._data.on("del", (key: string) => {
try {
this.deleteItem(key);
} catch {}
});
}
public signResponse(item: RPCCacheItem): string {
@ -89,7 +95,7 @@ export class RPCCache extends EventEmitter {
item.signature = this.signResponse(item);
this.dhtCache?.addItem(queryHash);
this._data[queryHash] = item;
this._data.set(queryHash, item);
}
public deleteItem(queryHash: string): boolean {
@ -100,7 +106,7 @@ export class RPCCache extends EventEmitter {
}
this.dhtCache?.removeItem(queryHash);
delete this._data[queryHash];
this._data.del(queryHash);
return true;
}

View File

@ -12,7 +12,6 @@ import b4a from "b4a";
import { get as getSwarm, SecretStream } from "../swarm";
// @ts-ignore
import c from "compact-encoding";
import DHTCache from "@lumeweb/dht-cache";
// @ts-ignore
import crypto from "hypercore-crypto";
// @ts-ignore
@ -201,8 +200,8 @@ export class RPCServer extends EventEmitter {
private getCachedRequest(request: RPCRequest): RPCCacheItem | boolean {
const req = RPCServer.hashQuery(request);
if (RPCServer.hashQuery(request) in this._cache.data) {
return this._cache.data[req] as RPCCacheItem;
if (this._cache.data.has(req)) {
return this._cache.data.get<RPCCacheItem>(req) as RPCCacheItem;
}
return false;
@ -245,8 +244,8 @@ export class RPCServer extends EventEmitter {
if (lock.isLocked()) {
await lock.waitForUnlock();
if (reqId in this._cache.data) {
return this._cache.data[reqId] as RPCCacheItem;
if (this._cache.data.has(reqId)) {
return this._cache.data.get<RPCCacheItem>(reqId) as RPCCacheItem;
}
}