From 68d6b8db88be468f3ec3ffc02ddbf7ae94c76a7b Mon Sep 17 00:00:00 2001 From: Ermal Kaleci Date: Sun, 29 Oct 2023 20:03:45 +0100 Subject: [PATCH] fix cache key --- packages/rpc-provider/src/http/index.ts | 7 ++++--- packages/rpc-provider/src/ws/index.ts | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/rpc-provider/src/http/index.ts b/packages/rpc-provider/src/http/index.ts index 42d4f004d0e3..ad6ec7750758 100644 --- a/packages/rpc-provider/src/http/index.ts +++ b/packages/rpc-provider/src/http/index.ts @@ -3,7 +3,7 @@ import type { JsonRpcResponse, ProviderInterface, ProviderInterfaceCallback, ProviderInterfaceEmitCb, ProviderInterfaceEmitted, ProviderStats } from '../types.js'; -import { logger, noop } from '@polkadot/util'; +import { logger, noop, stringify } from '@polkadot/util'; import { fetch } from '@polkadot/x-fetch'; import { RpcCoder } from '../coder/index.js'; @@ -125,15 +125,16 @@ export class HttpProvider implements ProviderInterface { this.#stats.total.requests++; const [, body] = this.#coder.encodeJson(method, params); + const cacheKey = isCacheable ? `${method}::${stringify(params)}` : ''; let resultPromise: Promise | null = isCacheable - ? this.#callCache.get(body) + ? this.#callCache.get(cacheKey) : null; if (!resultPromise) { resultPromise = this.#send(body); if (isCacheable) { - this.#callCache.set(body, resultPromise); + this.#callCache.set(cacheKey, resultPromise); } } else { this.#stats.total.cached++; diff --git a/packages/rpc-provider/src/ws/index.ts b/packages/rpc-provider/src/ws/index.ts index 6ba926715fa5..e2bb170a5e17 100644 --- a/packages/rpc-provider/src/ws/index.ts +++ b/packages/rpc-provider/src/ws/index.ts @@ -6,7 +6,7 @@ import type { EndpointStats, JsonRpcResponse, ProviderInterface, ProviderInterfa import { EventEmitter } from 'eventemitter3'; -import { isChildClass, isNull, isUndefined, logger, noop, objectSpread } from '@polkadot/util'; +import { isChildClass, isNull, isUndefined, logger, noop, objectSpread, stringify } from '@polkadot/util'; import { xglobal } from '@polkadot/x-global'; import { WebSocket } from '@polkadot/x-ws'; @@ -312,15 +312,16 @@ export class WsProvider implements ProviderInterface { this.#stats.total.requests++; const [id, body] = this.#coder.encodeJson(method, params); + const cacheKey = isCacheable ? `${method}::${stringify(params)}` : ''; let resultPromise: Promise | null = isCacheable - ? this.#callCache.get(body) + ? this.#callCache.get(cacheKey) : null; if (!resultPromise) { resultPromise = this.#send(id, body, method, params, subscription); if (isCacheable) { - this.#callCache.set(body, resultPromise); + this.#callCache.set(cacheKey, resultPromise); } } else { this.#endpointStats.cached++;