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

Commit

Permalink
perf: use redis cache in gwRPC
Browse files Browse the repository at this point in the history
  • Loading branch information
Flouse committed Dec 3, 2021
1 parent e3ecb57 commit 80b8e3c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
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';
31 changes: 15 additions & 16 deletions packages/api-server/src/methods/modules/gw.ts
Original file line number Diff line number Diff line change
@@ -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<Hash, HexU32>;
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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 80b8e3c

Please sign in to comment.