From 80b8e3cd9a863d83d2d6dcbcc04c421cc024f55a Mon Sep 17 00:00:00 2001 From: Flouse Date: Fri, 3 Dec 2021 16:00:24 +0000 Subject: [PATCH] perf: use redis cache in gwRPC --- packages/api-server/src/cache/constant.ts | 3 ++ packages/api-server/src/methods/modules/gw.ts | 31 +++++++++---------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/packages/api-server/src/cache/constant.ts b/packages/api-server/src/cache/constant.ts index c33ace03..550a0682 100644 --- a/packages/api-server/src/cache/constant.ts +++ b/packages/api-server/src/cache/constant.ts @@ -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'; \ No newline at end of file diff --git a/packages/api-server/src/methods/modules/gw.ts b/packages/api-server/src/methods/modules/gw.ts index 1986ae4b..493ab541 100644 --- a/packages/api-server/src/methods/modules/gw.ts +++ b/packages/api-server/src/methods/modules/gw.ts @@ -1,23 +1,23 @@ import { RPC } from "ckb-js-toolkit"; import { parseGwRpcError } from "../gw-error"; import { middleware } from "../validator"; -import { Hash, HexNumber } from "@ckb-lumos/base"; -import { HexU32 } from "@godwoken-web3/godwoken"; - -// TODO: use Redis -// import { Store } from "../../cache/store"; -// import { envConfig } from "../../base/env-config"; -// import { CACHE_EXPIRED_TIME_MILSECS } from "../../cache/constant"; +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 scriptHashToAccountIdcache: Map; + private gwCache: Store; constructor() { this.rpc = new RPC(process.env.GODWOKEN_JSON_RPC as string); - // this.cache = new Store(envConfig.redisUrl, true, CACHE_EXPIRED_TIME_MILSECS); - // this.cahce.init(); - this.scriptHashToAccountIdcache = new Map(); + 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); @@ -166,18 +166,17 @@ export class Gw { async get_account_id_by_script_hash(args: any[]) { try { const scriptHash = args[0]; - let result = this.scriptHashToAccountIdcache.get(scriptHash); - if (result !== undefined) { + 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) { + if (result != null) { console.debug(`update cache: ${scriptHash} -> ${result}`); - this.scriptHashToAccountIdcache.set(scriptHash, result); + this.gwCache.insert(`${GW_RPC_KEY}_${scriptHash}`, result); } - return result; } catch (error) { parseGwRpcError(error);