Skip to content

Commit

Permalink
improve: cleanly resolve redis cache layer
Browse files Browse the repository at this point in the history
  • Loading branch information
james-a-morris committed Oct 10, 2023
1 parent 13c2ebd commit 09a7c9d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
33 changes: 3 additions & 30 deletions src/caching/RedisCache.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<void> {
if (!this.redisClient) {
this.redisClient = await getRedis(this.logger, this.redisUrl);
}
this.redisClient = redisClient;
}

public async get<T>(key: string): Promise<T | undefined> {
// 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) {
Expand All @@ -62,19 +45,9 @@ export class RedisCache implements interfaces.CachingMechanismInterface {
}

public async set<T>(key: string, value: T, ttl: number = constants.DEFAULT_CACHING_TTL): Promise<string | undefined> {
// 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;
}
}
2 changes: 2 additions & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
6 changes: 2 additions & 4 deletions src/utils/DepositUtils.ts
Original file line number Diff line number Diff line change
@@ -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}`;
Expand Down Expand Up @@ -129,6 +128,5 @@ export async function queryHistoricalDepositForFill(
spokePoolClient: SpokePoolClient,
fill: Fill
): Promise<DepositWithBlock | undefined> {
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));
}
13 changes: 12 additions & 1 deletion src/utils/RedisUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof createClient>;
Expand Down Expand Up @@ -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<CachingMechanismInterface | undefined> {
const client = await getRedis(logger, url);
if (client) {
return new RedisCache(client, logger);
}
}

export async function setRedisKey(
key: string,
val: string,
Expand Down

0 comments on commit 09a7c9d

Please sign in to comment.