Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable api cache #486

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions .yarn/patches/@polkadot-rpc-provider-npm-10.10.1-c60ba50fe2.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
diff --git a/http/index.js b/http/index.js
index 63d7d0439956851f8794e10defbbd944fdae2766..13a08f7451bcb5838b19a64d47182a1dd8a7a813 100644
--- a/http/index.js
+++ b/http/index.js
@@ -1,4 +1,4 @@
-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';
import defaults from '../defaults.js';
@@ -99,13 +99,14 @@ export class HttpProvider {
async send(method, params, isCacheable) {
this.__internal__stats.total.requests++;
const [, body] = this.__internal__coder.encodeJson(method, params);
+ const cacheKey = isCacheable ? `${method}::${stringify(params)}` : '';
let resultPromise = isCacheable
- ? this.__internal__callCache.get(body)
+ ? this.__internal__callCache.get(cacheKey)
: null;
if (!resultPromise) {
resultPromise = this.__internal__send(body);
if (isCacheable) {
- this.__internal__callCache.set(body, resultPromise);
+ this.__internal__callCache.set(cacheKey, resultPromise);
}
}
else {
diff --git a/ws/index.js b/ws/index.js
index 879299b5d960a9bce6572a0e585ede29935db2a7..9ea7f184dd0c65dfe37585dd811120838ca580b9 100644
--- a/ws/index.js
+++ b/ws/index.js
@@ -1,5 +1,5 @@
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';
import { RpcCoder } from '../coder/index.js';
@@ -395,13 +395,14 @@ export class WsProvider {
this.__internal__endpointStats.requests++;
this.__internal__stats.total.requests++;
const [id, body] = this.__internal__coder.encodeJson(method, params);
+ const cacheKey = isCacheable ? `${method}::${stringify(params)}` : '';
let resultPromise = isCacheable
- ? this.__internal__callCache.get(body)
+ ? this.__internal__callCache.get(cacheKey)
: null;
if (!resultPromise) {
resultPromise = this.__internal__send(id, body, method, params, subscription);
if (isCacheable) {
- this.__internal__callCache.set(body, resultPromise);
+ this.__internal__callCache.set(cacheKey, resultPromise);
}
}
else {
167 changes: 0 additions & 167 deletions .yarn/patches/typeorm-npm-0.3.17-f8c2578e7f.patch

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@
"wasm-pack": "^0.12.1"
},
"resolutions": {
"typeorm@^0.3.17": "patch:typeorm@npm%3A0.3.17#./.yarn/patches/typeorm-npm-0.3.17-f8c2578e7f.patch"
"@polkadot/rpc-provider@10.10.1": "patch:@polkadot/rpc-provider@npm%3A10.10.1#./.yarn/patches/@polkadot-rpc-provider-npm-10.10.1-c60ba50fe2.patch"
}
}
13 changes: 7 additions & 6 deletions packages/core/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,16 @@ export class Api {
return this.#provider.send<HexString | null>(
'chain_getBlockHash',
Number.isInteger(blockNumber) ? [blockNumber] : [],
!!blockNumber,
)
}

async getHeader(hash?: string) {
return this.#provider.send<Header | null>('chain_getHeader', hash ? [hash] : [])
return this.#provider.send<Header | null>('chain_getHeader', hash ? [hash] : [], !!hash)
}

async getBlock(hash?: string) {
return this.#provider.send<SignedBlock | null>('chain_getBlock', hash ? [hash] : [])
return this.#provider.send<SignedBlock | null>('chain_getBlock', hash ? [hash] : [], !!hash)
}

async getStorage(key: string, hash?: string) {
Expand All @@ -122,12 +123,12 @@ export class Api {
// child storage key, use childstate_getStorage
const params = [child, storageKey]
if (hash) params.push(hash as HexString)
return this.#provider.send<HexString | null>('childstate_getStorage', params)
return this.#provider.send<HexString | null>('childstate_getStorage', params, !!hash)
} else {
// main storage key, use state_getStorage
const params = [key]
if (hash) params.push(hash)
return this.#provider.send<HexString | null>('state_getStorage', params)
return this.#provider.send<HexString | null>('state_getStorage', params, !!hash)
}
}

Expand All @@ -139,13 +140,13 @@ export class Api {
const params = [child, storageKey, pageSize, stripChildPrefix(startKey as HexString)]
if (hash) params.push(hash as HexString)
return this.#provider
.send<HexString[]>('childstate_getKeysPaged', params)
.send<HexString[]>('childstate_getKeysPaged', params, !!hash)
.then((keys) => keys.map((key) => prefixedChildKey(child, key)))
} else {
// main storage key, use state_getKeysPaged
const params = [prefix, pageSize, startKey]
if (hash) params.push(hash)
return this.#provider.send<HexString[]>('state_getKeysPaged', params)
return this.#provider.send<HexString[]>('state_getKeysPaged', params, !!hash)
}
}
}
Loading