*Switch to node cache
This commit is contained in:
parent
0387316e4f
commit
69fd9a14ef
|
@ -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,9 @@ export class RPCCache extends EventEmitter {
|
|||
this.dhtCache = new DHTCache(this._swarm, {
|
||||
protocol: "lumeweb.rpccache",
|
||||
});
|
||||
this._data.on("del", (key: string) => {
|
||||
this.deleteItem(key);
|
||||
});
|
||||
}
|
||||
|
||||
public signResponse(item: RPCCacheItem): string {
|
||||
|
@ -89,7 +93,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 +104,7 @@ export class RPCCache extends EventEmitter {
|
|||
}
|
||||
|
||||
this.dhtCache?.removeItem(queryHash);
|
||||
delete this._data[queryHash];
|
||||
this._data.del(queryHash);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -201,8 +201,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 +245,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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue