From 513f5ee786cfa92126191bf56db53207a36f87af Mon Sep 17 00:00:00 2001 From: james-a-morris Date: Wed, 4 Oct 2023 09:22:46 -0400 Subject: [PATCH] improve: cleanly resolve redis cache layer --- package.json | 2 +- src/caching/RedisCache.ts | 33 +++------------------------------ src/interfaces/index.ts | 2 ++ src/utils/DepositUtils.ts | 6 ++---- src/utils/RedisUtils.ts | 13 ++++++++++++- 5 files changed, 20 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index 963fc6d2bb..5a087f20f2 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,8 @@ "node": ">=16.18.0" }, "dependencies": { - "@across-protocol/contracts-v2": "2.4.3", "@across-protocol/constants-v2": "1.0.4", + "@across-protocol/contracts-v2": "2.4.3", "@across-protocol/sdk-v2": "0.17.1", "@arbitrum/sdk": "^3.1.3", "@defi-wonderland/smock": "^2.3.5", diff --git a/src/caching/RedisCache.ts b/src/caching/RedisCache.ts index 4777c66e96..0f14c0f5c2 100644 --- a/src/caching/RedisCache.ts +++ b/src/caching/RedisCache.ts @@ -1,5 +1,5 @@ import { interfaces, constants } from "@across-protocol/sdk-v2"; -import { RedisClient, getRedis, objectWithBigNumberReviver, setRedisKey, winston } from "../utils"; +import { RedisClient, objectWithBigNumberReviver, setRedisKey, winston } from "../utils"; /** * RedisCache is a caching mechanism that uses Redis as the backing store. It is used by the @@ -27,29 +27,12 @@ export class RedisCache implements interfaces.CachingMechanismInterface { * @param redisUrl The URL of the redis server to connect to. * @param logger The logger to use to log debug messages. */ - constructor(redisUrl: string, logger?: winston.Logger) { + constructor(redisClient: RedisClient, logger?: winston.Logger) { this.logger = logger; - this.redisUrl = redisUrl; - this.redisClient = undefined; - } - - /** - * The instantiate method is used to instantiate the redis client. It is called lazily - * when the `get` or `set` methods are called. - * @returns A promise that resolves when the redis client has been instantiated. - * @throws An error if the redis client could not be instantiated. - */ - public async instantiate(): Promise { - if (!this.redisClient) { - this.redisClient = await getRedis(this.logger, this.redisUrl); - } + this.redisClient = redisClient; } public async get(key: string): Promise { - // Instantiate the redis client if it has not been instantiated yet. - if (!this.redisClient) { - await this.instantiate(); - } // Get the value from redis. const result = await this.redisClient.get(key); if (result) { @@ -62,19 +45,9 @@ export class RedisCache implements interfaces.CachingMechanismInterface { } public async set(key: string, value: T, ttl: number = constants.DEFAULT_CACHING_TTL): Promise { - // Instantiate the redis client if it has not been instantiated yet. - if (!this.redisClient) { - await this.instantiate(); - } // Call the setRedisKey function to set the value in redis. await setRedisKey(key, JSON.stringify(value), this.redisClient, ttl); // Return key to indicate that the value was set successfully. return key; } - - static resolveFromRedisClient(redisClient: RedisClient, logger?: winston.Logger): RedisCache { - const cache = new RedisCache(redisClient.options.url, logger); - cache.redisClient = redisClient; - return cache; - } } diff --git a/src/interfaces/index.ts b/src/interfaces/index.ts index d71b294159..bee0729876 100644 --- a/src/interfaces/index.ts +++ b/src/interfaces/index.ts @@ -72,3 +72,5 @@ export const isUbaInflow = interfaces.isUbaInflow; export const isUbaOutflow = interfaces.isUbaOutflow; export const outflowIsFill = interfaces.outflowIsFill; export const outflowIsRefund = interfaces.outflowIsRefund; + +export type CachingMechanismInterface = interfaces.CachingMechanismInterface; diff --git a/src/utils/DepositUtils.ts b/src/utils/DepositUtils.ts index dee800d273..5e156546c4 100644 --- a/src/utils/DepositUtils.ts +++ b/src/utils/DepositUtils.ts @@ -1,9 +1,8 @@ import { Deposit, DepositWithBlock, Fill, UnfilledDeposit, UnfilledDepositsForOriginChain } from "../interfaces"; import { SpokePoolClient } from "../clients"; -import { assign, toBN, isFirstFillForDeposit, getRedis } from "./"; +import { assign, toBN, isFirstFillForDeposit, getRedisCache } from "./"; import { getBlockRangeForChain } from "../dataworker/DataworkerUtils"; import { utils, typechain } from "@across-protocol/sdk-v2"; -import { RedisCache } from "../caching/RedisCache"; export function getDepositPath(deposit: Deposit): string { return `${deposit.originToken}-->${deposit.destinationChainId}`; @@ -129,6 +128,5 @@ export async function queryHistoricalDepositForFill( spokePoolClient: SpokePoolClient, fill: Fill ): Promise { - const cache = RedisCache.resolveFromRedisClient(await getRedis(spokePoolClient.logger), spokePoolClient.logger); - return utils.queryHistoricalDepositForFill(spokePoolClient, fill, cache); + return utils.queryHistoricalDepositForFill(spokePoolClient, fill, await getRedisCache(spokePoolClient.logger)); } diff --git a/src/utils/RedisUtils.ts b/src/utils/RedisUtils.ts index 4f70b54fb2..76efb3038b 100644 --- a/src/utils/RedisUtils.ts +++ b/src/utils/RedisUtils.ts @@ -2,8 +2,9 @@ import { assert, toBN, BigNumberish } from "./"; import { REDIS_URL_DEFAULT } from "../common/Constants"; import { createClient } from "redis4"; import winston from "winston"; -import { Deposit, Fill } from "../interfaces"; +import { Deposit, Fill, CachingMechanismInterface } from "../interfaces"; import dotenv from "dotenv"; +import { RedisCache } from "../caching/RedisCache"; dotenv.config(); export type RedisClient = ReturnType; @@ -43,6 +44,16 @@ export async function getRedis(logger?: winston.Logger, url = REDIS_URL): Promis return redisClients[url]; } +export async function getRedisCache( + logger?: winston.Logger, + url?: string +): Promise { + const client = await getRedis(logger, url); + if (client) { + return new RedisCache(client, logger); + } +} + export async function setRedisKey( key: string, val: string,