From 4036abdd114641324b9afe835b03c2ce62ea022e Mon Sep 17 00:00:00 2001 From: Dong-Ha Kim Date: Fri, 6 Sep 2024 21:28:57 +0800 Subject: [PATCH] fix: skip redis cache ops if env vars not defined (#1195) --- api/_cache.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/api/_cache.ts b/api/_cache.ts index cb92e0e1c..b170042e3 100644 --- a/api/_cache.ts +++ b/api/_cache.ts @@ -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(key: string): Promise { + if (!isRedisCacheEnabled) { + return null; + } + const value = await kv.get(key); if (value === null || value === undefined) { return null; @@ -11,6 +24,10 @@ export class RedisCache implements interfaces.CachingMechanismInterface { } async set(key: string, value: T, ttl = 10): Promise { + if (!isRedisCacheEnabled) { + return; + } + try { if (typeof value === "string") { try {