Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #95 from Flouse/cache-get_account_id_by_script_hash
Browse files Browse the repository at this point in the history
Cache get account id by script hash
  • Loading branch information
RetricSu authored Dec 3, 2021
2 parents de1e7b4 + 3ab1b03 commit 852e9a2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/api-server/src/cache/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
export const CACHE_EXPIRED_TIME_MILSECS = 5 * 60 * 1000; // milsec, default 5 minutes
// limit redis store filter size
export const MAX_FILTER_TOPIC_ARRAY_LENGTH = 20;

// The Cache Key Prfixs
export const GW_RPC_KEY = "gwRPC";
24 changes: 23 additions & 1 deletion packages/api-server/src/methods/modules/gw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@ import { RPC } from "ckb-js-toolkit";
import { parseGwRpcError } from "../gw-error";
import { middleware } from "../validator";
import { HexNumber } from "@ckb-lumos/base";
import { Store } from "../../cache/store";
import { envConfig } from "../../base/env-config";
import { CACHE_EXPIRED_TIME_MILSECS, GW_RPC_KEY } from "../../cache/constant";

export class Gw {
private rpc: RPC;
private gwCache: Store;

constructor() {
this.rpc = new RPC(process.env.GODWOKEN_JSON_RPC as string);
this.gwCache = new Store(
envConfig.redisUrl,
true,
CACHE_EXPIRED_TIME_MILSECS
);
this.gwCache.init();

this.ping = middleware(this.ping.bind(this), 0);
this.get_tip_block_hash = middleware(this.get_tip_block_hash.bind(this), 0);
Expand Down Expand Up @@ -154,7 +165,18 @@ export class Gw {
*/
async get_account_id_by_script_hash(args: any[]) {
try {
const result = await this.rpc.gw_get_account_id_by_script_hash(...args);
const scriptHash = args[0];
let result = await this.gwCache.get(`${GW_RPC_KEY}_${scriptHash}`);
if (result != null) {
console.debug(`using cache: ${scriptHash} -> ${result}`);
return result;
}

result = await this.rpc.gw_get_account_id_by_script_hash(...args);
if (result != null) {
console.debug(`update cache: ${scriptHash} -> ${result}`);
this.gwCache.insert(`${GW_RPC_KEY}_${scriptHash}`, result);
}
return result;
} catch (error) {
parseGwRpcError(error);
Expand Down

0 comments on commit 852e9a2

Please sign in to comment.