diff --git a/packages/rpc-provider/src/http/index.ts b/packages/rpc-provider/src/http/index.ts index 085ccb90ba15..794702b65268 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 6528b00d782b..1cf65fee610d 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++;