-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d7fb75a
commit 95b49d3
Showing
3 changed files
with
54 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { ApiDecoration } from '@polkadot/api/types'; | ||
import { ApiPromise } from '@polkadot/api'; | ||
import LRUCache from 'lru-cache'; | ||
|
||
class ApiAtCache { | ||
#apiAtCache: LRUCache<string, ApiDecoration<'promise'>>; | ||
|
||
constructor(maxCacheSize: number = 100) { | ||
this.#apiAtCache = new LRUCache<string, ApiDecoration<'promise'>>({ | ||
max: maxCacheSize, | ||
}); | ||
} | ||
|
||
getApiAt = async ( | ||
api: ApiPromise, | ||
blockHash: string | ||
): Promise<ApiDecoration<'promise'>> => { | ||
const cached = this.#apiAtCache.get(blockHash); | ||
if (cached) return cached; | ||
|
||
const apiAt = await api.at(blockHash); | ||
|
||
// do we need to check for finalization here? | ||
// ApiAt is only a decoration and the actuall result is from rpc call, so should be fine? | ||
this.#apiAtCache.set(blockHash, apiAt); | ||
|
||
return apiAt; | ||
}; | ||
} | ||
|
||
export const apiCache = new ApiAtCache(100); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters