*Add getter for dhtCache

This commit is contained in:
Derrick Hammer 2022-12-04 12:03:36 -05:00
parent 0e6c84c566
commit e35b602133
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 23 additions and 18 deletions

View File

@ -10,9 +10,28 @@ import crypto from "hypercore-crypto";
import NodeCache from "node-cache"; import NodeCache from "node-cache";
export class RPCCache extends EventEmitter { export class RPCCache extends EventEmitter {
private dhtCache?: DHTCache;
private server: RPCServer; private server: RPCServer;
constructor(server: RPCServer) {
super();
this.server = server;
this._swarm = getSwarm();
this._dhtCache = new DHTCache(this._swarm, {
protocol: "lumeweb.rpccache",
});
this._data.on("del", (key: string) => {
try {
this.deleteItem(key);
} catch {}
});
}
private _dhtCache?: DHTCache;
get dhtCache(): DHTCache {
return this._dhtCache as DHTCache;
}
private _swarm?: any; private _swarm?: any;
get swarm(): any { get swarm(): any {
@ -25,20 +44,6 @@ export class RPCCache extends EventEmitter {
return this._data; return this._data;
} }
constructor(server: RPCServer) {
super();
this.server = server;
this._swarm = getSwarm();
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 { public signResponse(item: RPCCacheItem): string {
const field = item.value.signedField || "data"; const field = item.value.signedField || "data";
const updated = item.value.updated; const updated = item.value.updated;
@ -93,18 +98,18 @@ export class RPCCache extends EventEmitter {
item.signature = this.signResponse(item); item.signature = this.signResponse(item);
this.dhtCache?.addItem(queryHash); this._dhtCache?.addItem(queryHash);
this._data.set(queryHash, item); this._data.set(queryHash, item);
} }
public deleteItem(queryHash: string): boolean { public deleteItem(queryHash: string): boolean {
const cache = this.dhtCache?.cache; const cache = this._dhtCache?.cache;
if (!cache?.includes(queryHash)) { if (!cache?.includes(queryHash)) {
throw Error("item does not exist"); throw Error("item does not exist");
} }
this.dhtCache?.removeItem(queryHash); this._dhtCache?.removeItem(queryHash);
this._data.del(queryHash); this._data.del(queryHash);
return true; return true;