Add lru ttl cache implementation (#5997)

* lru ttl cache implementation

* single export lru

* fix lint

* fix hanging tests

* clear intervals in RPCcore

* clear linting issues

* checking export issue on deno ci

* lint

* deno update and rebase

* fixes to stats

* fixes to lint
This commit is contained in:
filvecchiato
2024-10-16 08:39:46 -04:00
committed by GitHub
parent 127122990e
commit a3e1499f20
7 changed files with 135 additions and 41 deletions
+13 -11
View File
@@ -11,6 +11,7 @@ import type { RpcCoreStats, RpcInterfaceMethod } from './types/index.js';
import { Observable, publishReplay, refCount } from 'rxjs';
import { DEFAULT_CAPACITY, LRUCache } from '@polkadot/rpc-provider';
import { rpcDefinitions } from '@polkadot/types';
import { hexToU8a, isFunction, isNull, isUndefined, lazyMethod, logger, memoize, objectSpread, u8aConcat, u8aToU8a } from '@polkadot/util';
@@ -93,9 +94,8 @@ export class RpcCore {
readonly #instanceId: string;
readonly #isPedantic: boolean;
readonly #registryDefault: Registry;
readonly #storageCache = new Map<string, Codec>();
readonly #storageCache: LRUCache;
#storageCacheHits = 0;
#storageCacheSize = 0;
#getBlockRegistry?: (blockHash: Uint8Array) => Promise<{ registry: Registry }>;
#getBlockHash?: (blockNumber: AnyNumber) => Promise<Uint8Array>;
@@ -123,7 +123,7 @@ export class RpcCore {
// these are the base keys (i.e. part of jsonrpc)
this.sections.push(...sectionNames);
this.#storageCache = new LRUCache(DEFAULT_CAPACITY);
// decorate all interfaces, defined and user on this instance
this.addUserInterfaces(userRpc);
}
@@ -145,7 +145,9 @@ export class RpcCore {
/**
* @description Manually disconnect from the attached provider
*/
public disconnect (): Promise<void> {
public async disconnect (): Promise<void> {
await this.#storageCache.clearInterval();
return this.provider.disconnect();
}
@@ -160,7 +162,7 @@ export class RpcCore {
...stats,
core: {
cacheHits: this.#storageCacheHits,
cacheSize: this.#storageCacheSize
cacheSize: this.#storageCache.length
}
}
: undefined;
@@ -469,7 +471,7 @@ export class RpcCore {
// - if a single result value, don't fill - it is not an update hole
// - fallback to an empty option in all cases
if (isNotFound && withCache) {
const cached = this.#storageCache.get(hexKey);
const cached = this.#storageCache.get(hexKey) as Codec | undefined;
if (cached) {
this.#storageCacheHits++;
@@ -487,15 +489,15 @@ export class RpcCore {
: u8aToU8a(value);
const codec = this._newType(registry, blockHash, key, input, isEmpty, entryIndex);
// store the retrieved result - the only issue with this cache is that there is no
// clearing of it, so very long running processes (not just a couple of hours, longer)
// will increase memory beyond what is allowed.
this.#storageCache.set(hexKey, codec);
this.#storageCacheSize++;
this._setToCache(hexKey, codec);
return codec;
}
private _setToCache (key: string, value: Codec): void {
this.#storageCache.set(key, value);
}
private _newType (registry: Registry, blockHash: Uint8Array | string | null | undefined, key: StorageKey, input: string | Uint8Array | null, isEmpty: boolean, entryIndex = -1): Codec {
// single return value (via state.getStorage), decode the value based on the
// outputType that we have specified. Fallback to Raw on nothing