*Make dht cache optional, but enabled by default

This commit is contained in:
Derrick Hammer 2022-12-19 15:28:22 -05:00
parent 1e3b0e46fe
commit af4e6155bc
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 23 additions and 9 deletions

View File

@ -29,6 +29,7 @@ config.inject({
logLevel: "info",
pluginDir: path.resolve(configDir, "..", "plugins"),
plugins: ["core"],
cache: true,
});
config.load();

View File

@ -18,6 +18,7 @@ import { Mutex } from "async-mutex";
import { RPCCache } from "./cache";
// @ts-ignore
import jsonStringify from "json-stringify-deterministic";
import config from "../../config";
const sodium = require("sodium-universal");
let server: RPCServer;
@ -54,9 +55,16 @@ export class RPCServer extends EventEmitter {
>();
private pendingRequests: Map<string, Mutex> = new Map<string, Mutex>();
private _cache: RPCCache = new RPCCache(this);
private _cache?: RPCCache;
get cache(): RPCCache {
constructor() {
super();
if (config.bool("cache")) {
this._cache = new RPCCache(this);
}
}
get cache(): RPCCache | undefined {
return this._cache;
}
@ -137,7 +145,7 @@ export class RPCServer extends EventEmitter {
}
return crypto
.sign(Buffer.from(raw), this._cache.swarm.keyPair.secretKey)
.sign(Buffer.from(raw), this._cache?.swarm.keyPair.secretKey)
.toString("hex");
}
@ -201,8 +209,8 @@ export class RPCServer extends EventEmitter {
}
method = method as RPCMethod;
if (method.cacheable) {
this.cache.addItem(request, rpcResult);
if (config.bool("cache") && method.cacheable) {
this.cache?.addItem(request, rpcResult);
}
this.getRequestLock(request)?.release();
@ -211,9 +219,14 @@ export class RPCServer extends EventEmitter {
}
private getCachedRequest(request: RPCRequest): RPCCacheItem | boolean {
if (!config.bool("cache")) {
return false;
}
const req = RPCServer.hashQuery(request);
if (this._cache.data.has(req)) {
return this._cache.data.get<RPCCacheItem>(req) as RPCCacheItem;
if (this._cache?.data.has(req)) {
return this._cache?.data.get<RPCCacheItem>(req) as RPCCacheItem;
}
return false;
@ -256,8 +269,8 @@ export class RPCServer extends EventEmitter {
if (lock.isLocked()) {
await lock.waitForUnlock();
if (this._cache.data.has(reqId)) {
return this._cache.data.get<RPCCacheItem>(reqId) as RPCCacheItem;
if (this._cache?.data.has(reqId)) {
return this._cache?.data.get<RPCCacheItem>(reqId) as RPCCacheItem;
}
}