Skip to content

Commit

Permalink
fix: skip redis cache ops if env vars not defined (#1195)
Browse files Browse the repository at this point in the history
  • Loading branch information
dohaki authored Sep 6, 2024
1 parent abec098 commit 4036abd
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions api/_cache.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { kv } from "@vercel/kv";
import { interfaces } from "@across-protocol/sdk";

const {
KV_REST_API_READ_ONLY_TOKEN,
KV_REST_API_TOKEN,
KV_REST_API_URL,
KV_URL,
} = process.env;
const isRedisCacheEnabled =
KV_REST_API_URL && KV_REST_API_TOKEN && KV_REST_API_READ_ONLY_TOKEN && KV_URL;

export class RedisCache implements interfaces.CachingMechanismInterface {
async get<T>(key: string): Promise<T | null> {
if (!isRedisCacheEnabled) {
return null;
}

const value = await kv.get(key);
if (value === null || value === undefined) {
return null;
Expand All @@ -11,6 +24,10 @@ export class RedisCache implements interfaces.CachingMechanismInterface {
}

async set<T>(key: string, value: T, ttl = 10): Promise<string | undefined> {
if (!isRedisCacheEnabled) {
return;
}

try {
if (typeof value === "string") {
try {
Expand Down

0 comments on commit 4036abd

Please sign in to comment.