fix cache key (rpc-provider) (#5745)

This commit is contained in:
Ermal Kaleci
2024-03-04 11:57:45 -05:00
committed by GitHub
parent df2039f54d
commit ea2c942ece
2 changed files with 8 additions and 6 deletions
+4 -3
View File
@@ -3,7 +3,7 @@
import type { JsonRpcResponse, ProviderInterface, ProviderInterfaceCallback, ProviderInterfaceEmitCb, ProviderInterfaceEmitted, ProviderStats } from '../types.js'; 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 { fetch } from '@polkadot/x-fetch';
import { RpcCoder } from '../coder/index.js'; import { RpcCoder } from '../coder/index.js';
@@ -125,15 +125,16 @@ export class HttpProvider implements ProviderInterface {
this.#stats.total.requests++; this.#stats.total.requests++;
const [, body] = this.#coder.encodeJson(method, params); const [, body] = this.#coder.encodeJson(method, params);
const cacheKey = isCacheable ? `${method}::${stringify(params)}` : '';
let resultPromise: Promise<T> | null = isCacheable let resultPromise: Promise<T> | null = isCacheable
? this.#callCache.get(body) ? this.#callCache.get(cacheKey)
: null; : null;
if (!resultPromise) { if (!resultPromise) {
resultPromise = this.#send(body); resultPromise = this.#send(body);
if (isCacheable) { if (isCacheable) {
this.#callCache.set(body, resultPromise); this.#callCache.set(cacheKey, resultPromise);
} }
} else { } else {
this.#stats.total.cached++; this.#stats.total.cached++;
+4 -3
View File
@@ -6,7 +6,7 @@ import type { EndpointStats, JsonRpcResponse, ProviderInterface, ProviderInterfa
import { EventEmitter } from 'eventemitter3'; 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 { xglobal } from '@polkadot/x-global';
import { WebSocket } from '@polkadot/x-ws'; import { WebSocket } from '@polkadot/x-ws';
@@ -312,15 +312,16 @@ export class WsProvider implements ProviderInterface {
this.#stats.total.requests++; this.#stats.total.requests++;
const [id, body] = this.#coder.encodeJson(method, params); const [id, body] = this.#coder.encodeJson(method, params);
const cacheKey = isCacheable ? `${method}::${stringify(params)}` : '';
let resultPromise: Promise<T> | null = isCacheable let resultPromise: Promise<T> | null = isCacheable
? this.#callCache.get(body) ? this.#callCache.get(cacheKey)
: null; : null;
if (!resultPromise) { if (!resultPromise) {
resultPromise = this.#send(id, body, method, params, subscription); resultPromise = this.#send(id, body, method, params, subscription);
if (isCacheable) { if (isCacheable) {
this.#callCache.set(body, resultPromise); this.#callCache.set(cacheKey, resultPromise);
} }
} else { } else {
this.#endpointStats.cached++; this.#endpointStats.cached++;