*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";
|
import orderedJSON from "ordered-json";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import crypto from "hypercore-crypto";
|
import crypto from "hypercore-crypto";
|
||||||
|
import NodeCache from "node-cache";
|
||||||
|
|
||||||
export class RPCCache extends EventEmitter {
|
export class RPCCache extends EventEmitter {
|
||||||
private dhtCache?: DHTCache;
|
private dhtCache?: DHTCache;
|
||||||
|
@ -25,9 +26,9 @@ export class RPCCache extends EventEmitter {
|
||||||
return this._swarm;
|
return this._swarm;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _data: RPCCacheData = {};
|
private _data: NodeCache = new NodeCache({ stdTTL: 60 * 60 * 24 });
|
||||||
|
|
||||||
get data(): RPCCacheData {
|
get data(): NodeCache {
|
||||||
return this._data;
|
return this._data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,6 +39,9 @@ export class RPCCache extends EventEmitter {
|
||||||
this.dhtCache = new DHTCache(this._swarm, {
|
this.dhtCache = new DHTCache(this._swarm, {
|
||||||
protocol: "lumeweb.rpccache",
|
protocol: "lumeweb.rpccache",
|
||||||
});
|
});
|
||||||
|
this._data.on("del", (key: string) => {
|
||||||
|
this.deleteItem(key);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public signResponse(item: RPCCacheItem): string {
|
public signResponse(item: RPCCacheItem): string {
|
||||||
|
@ -89,7 +93,7 @@ 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[queryHash] = item;
|
this._data.set(queryHash, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
public deleteItem(queryHash: string): boolean {
|
public deleteItem(queryHash: string): boolean {
|
||||||
|
@ -100,7 +104,7 @@ export class RPCCache extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.dhtCache?.removeItem(queryHash);
|
this.dhtCache?.removeItem(queryHash);
|
||||||
delete this._data[queryHash];
|
this._data.del(queryHash);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -201,8 +201,8 @@ export class RPCServer extends EventEmitter {
|
||||||
|
|
||||||
private getCachedRequest(request: RPCRequest): RPCCacheItem | boolean {
|
private getCachedRequest(request: RPCRequest): RPCCacheItem | boolean {
|
||||||
const req = RPCServer.hashQuery(request);
|
const req = RPCServer.hashQuery(request);
|
||||||
if (RPCServer.hashQuery(request) in this._cache.data) {
|
if (this._cache.data.has(req)) {
|
||||||
return this._cache.data[req] as RPCCacheItem;
|
return this._cache.data.get<RPCCacheItem>(req) as RPCCacheItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -245,8 +245,8 @@ export class RPCServer extends EventEmitter {
|
||||||
|
|
||||||
if (lock.isLocked()) {
|
if (lock.isLocked()) {
|
||||||
await lock.waitForUnlock();
|
await lock.waitForUnlock();
|
||||||
if (reqId in this._cache.data) {
|
if (this._cache.data.has(reqId)) {
|
||||||
return this._cache.data[reqId] as RPCCacheItem;
|
return this._cache.data.get<RPCCacheItem>(reqId) as RPCCacheItem;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue