Skip to content

Commit e84c58d

Browse files
authored
Add rpcCacheCapacity option to ApiOptions (#6020)
1 parent 8efc552 commit e84c58d

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

packages/api/src/base/Decorate.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export abstract class Decorate<ApiType extends ApiTypes> extends Events {
153153
this._rpcCore = new RpcCore(this.#instanceId, this.#registry, {
154154
isPedantic: this._options.isPedantic,
155155
provider,
156+
rpcCacheCapacity: this._options.rpcCacheCapacity,
156157
userRpc: this._options.rpc
157158
}) as (RpcCore & RpcInterface);
158159
this._isConnected = new BehaviorSubject(this._rpcCore.provider.isConnected);

packages/api/src/types/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ export interface ApiOptions extends RegisteredTypes {
8080
* @description User-defined RPC methods
8181
*/
8282
rpc?: Record<string, Record<string, DefinitionRpc | DefinitionRpcSub>>;
83+
/**
84+
* @description Defines the size of the cache for the rpc-core. Defaults to 1024 * 10 * 10.
85+
*/
86+
rpcCacheCapacity?: number;
8387
/**
8488
* @description Overrides for state_call usage (this will be removed in some future version)
8589
*/

packages/rpc-core/src/bundle.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { RpcCoreStats, RpcInterfaceMethod } from './types/index.js';
1111

1212
import { Observable, publishReplay, refCount } from 'rxjs';
1313

14-
import { DEFAULT_CAPACITY, LRUCache } from '@polkadot/rpc-provider';
14+
import { LRUCache } from '@polkadot/rpc-provider';
1515
import { rpcDefinitions } from '@polkadot/types';
1616
import { hexToU8a, isFunction, isNull, isUndefined, lazyMethod, logger, memoize, objectSpread, u8aConcat, u8aToU8a } from '@polkadot/util';
1717

@@ -33,6 +33,7 @@ type MemoizedRpcInterfaceMethod = Memoized<RpcInterfaceMethod> & {
3333
interface Options {
3434
isPedantic?: boolean;
3535
provider: ProviderInterface;
36+
rpcCacheCapacity?: number;
3637
userRpc?: Record<string, Record<string, DefinitionRpc | DefinitionRpcSub>>;
3738
}
3839

@@ -47,6 +48,8 @@ const EMPTY_META = {
4748
}
4849
};
4950

51+
const RPC_CORE_DEFAULT_CAPACITY = 1024 * 10 * 10;
52+
5053
// utility method to create a nicely-formatted error
5154
/** @internal */
5255
function logErrorMessage (method: string, { noErrorLog, params, type }: DefinitionRpc, error: Error): void {
@@ -109,7 +112,7 @@ export class RpcCore {
109112
* Default constructor for the core RPC handler
110113
* @param {ProviderInterface} provider An API provider using any of the supported providers (HTTP, SC or WebSocket)
111114
*/
112-
constructor (instanceId: string, registry: Registry, { isPedantic = true, provider, userRpc = {} }: Options) {
115+
constructor (instanceId: string, registry: Registry, { isPedantic = true, provider, rpcCacheCapacity, userRpc = {} }: Options) {
113116
if (!provider || !isFunction(provider.send)) {
114117
throw new Error('Expected Provider to API create');
115118
}
@@ -123,7 +126,7 @@ export class RpcCore {
123126

124127
// these are the base keys (i.e. part of jsonrpc)
125128
this.sections.push(...sectionNames);
126-
this.#storageCache = new LRUCache(DEFAULT_CAPACITY * 10 * 10);
129+
this.#storageCache = new LRUCache(rpcCacheCapacity || RPC_CORE_DEFAULT_CAPACITY);
127130
// decorate all interfaces, defined and user on this instance
128131
this.addUserInterfaces(userRpc);
129132
}

0 commit comments

Comments
 (0)