diff --git a/apps/api/src/caching/helpers.ts b/apps/api/src/caching/helpers.ts index 621f997ae..212c4e0c4 100644 --- a/apps/api/src/caching/helpers.ts +++ b/apps/api/src/caching/helpers.ts @@ -1,8 +1,11 @@ import * as Sentry from "@sentry/node"; import { differenceInSeconds } from "date-fns"; +import { LoggerService } from "@src/core"; import MemoryCacheEngine from "./memoryCacheEngine"; +const logger = new LoggerService({ context: "CachingHelpers" }); + export const cacheEngine = new MemoryCacheEngine(); const pendingRequests: { [key: string]: Promise } = {}; @@ -30,20 +33,24 @@ export const Memoize = (options?: MemoizeOptions) => (target: object, propertyNa export async function cacheResponse(seconds: number, key: string, refreshRequest: () => Promise, keepData?: boolean): Promise { const duration = seconds * 1000; const cachedObject = cacheEngine.getFromCache(key) as CachedObject | undefined; - // console.log(`Cache key: ${key}`); + // logger.info(`Cache key: ${key}`); // If first time or expired, must refresh data if not already refreshing const cacheExpired = Math.abs(differenceInSeconds(cachedObject?.date, new Date())) > seconds; if ((!cachedObject || cacheExpired) && !(key in pendingRequests)) { - // console.log(`Making request: ${key}`); + // logger.info(`Making request: ${key}`); pendingRequests[key] = refreshRequest() .then(data => { cacheEngine.storeInCache(key, { date: new Date(), data: data }, keepData ? undefined : duration); return data; }) .catch(err => { - console.error(`Error making cache request ${err}`); - Sentry.captureException(err); + if (cachedObject) { + logger.error(`Error making cache request ${err}`); + Sentry.captureException(err); + } else { + throw err; + } }) .finally(() => { delete pendingRequests[key]; @@ -52,10 +59,10 @@ export async function cacheResponse(seconds: number, key: string, refreshRequ // If there is data in cache, return it even if it is expired. Otherwise, wait for the refresh request to finish if (cachedObject) { - // console.log(`Cache hit: ${key}`); + // logger.info(`Cache hit: ${key}`); return cachedObject.data; } else { - // console.log(`Waiting for pending request: ${key}`); + // logger.info(`Waiting for pending request: ${key}`); return (await pendingRequests[key]) as T; } } diff --git a/apps/api/src/services/external/githubService.ts b/apps/api/src/services/external/githubService.ts index 561a7143e..ea8b59110 100644 --- a/apps/api/src/services/external/githubService.ts +++ b/apps/api/src/services/external/githubService.ts @@ -22,19 +22,25 @@ export function getOctokit() { export const getProviderAttributesSchema = async (): Promise => { // Fetching provider attributes schema const response = await cacheResponse( - 30, + 60 * 5, cacheKeys.getProviderAttributesSchema, - async () => await axios.get("https://raw.githubusercontent.com/akash-network/console/main/config/provider-attributes.json") + async () => await axios.get("https://raw.githubusercontent.com/akash-network/console/main/config/provider-attributes.json"), + true ); return response.data; }; export async function getAuditors() { - const response = await cacheResponse(60 * 5, cacheKeys.getAuditors, async () => { - const res = await axios.get("https://raw.githubusercontent.com/akash-network/console/main/config/auditors.json"); - return res.data; - }); + const response = await cacheResponse( + 60 * 5, + cacheKeys.getAuditors, + async () => { + const res = await axios.get("https://raw.githubusercontent.com/akash-network/console/main/config/auditors.json"); + return res.data; + }, + true + ); return response; }