From 98348d53512a2ecbf70c323310c21a95a32ee3a7 Mon Sep 17 00:00:00 2001 From: Alexandru Cambose Date: Thu, 28 Aug 2025 18:34:48 +0200 Subject: [PATCH 1/3] feat: updated caching --- apps/insights/src/cache.ts | 24 +++++++++---------- apps/insights/src/server/pyth.ts | 10 ++++---- apps/insights/src/services/pyth/get-feeds.ts | 4 ++-- .../src/services/pyth/get-metadata.ts | 10 ++------ .../pyth/get-publishers-for-cluster.ts | 4 ++-- 5 files changed, 22 insertions(+), 30 deletions(-) diff --git a/apps/insights/src/cache.ts b/apps/insights/src/cache.ts index 140670fc9c..9e03887dc5 100644 --- a/apps/insights/src/cache.ts +++ b/apps/insights/src/cache.ts @@ -9,17 +9,20 @@ const transformer = { deserialize: parse, }; -// export const DEFAULT_CACHE_TTL = 3600; // 1 hour -// export const DEFAULT_CACHE_STALE = 86_400; // 24 hours - - -export const DEFAULT_CACHE_TTL = 60; // 1 minute -export const DEFAULT_CACHE_STALE = 60; // 1 minute +/** + * - API routes will be cached for 1hour + * - Cached function will be cached for 10 minutes, + * but if the function is called within 1 hour, it will + * still be served from the cache but also fetch the latest data + */ +export const DEFAULT_NEXT_FETCH_TTL = 3600; // 1 hour +export const DEFAULT_REDIS_CACHE_TTL = 60 * 10; // 10 minutes +export const DEFAULT_REDIS_CACHE_STALE = 3600; // 1 hour export const redisCache: ACDCache = createCache({ transformer, - stale: DEFAULT_CACHE_STALE, - ttl: DEFAULT_CACHE_TTL, + stale: DEFAULT_REDIS_CACHE_STALE, + ttl: DEFAULT_REDIS_CACHE_TTL, storage: { type: "redis", options: { @@ -27,8 +30,3 @@ export const redisCache: ACDCache = createCache({ }, }, }); - -export const memoryOnlyCache: ACDCache = createCache({ - ttl: DEFAULT_CACHE_TTL, - stale: DEFAULT_CACHE_STALE, -}); diff --git a/apps/insights/src/server/pyth.ts b/apps/insights/src/server/pyth.ts index f144faabd7..8b7a5fae20 100644 --- a/apps/insights/src/server/pyth.ts +++ b/apps/insights/src/server/pyth.ts @@ -1,7 +1,7 @@ import { parse } from "superjson"; import { z } from "zod"; -import { DEFAULT_CACHE_TTL } from "../cache"; +import { DEFAULT_NEXT_CACHE_TTL } from "../cache"; import { VERCEL_REQUEST_HEADERS } from "../config/server"; import { getHost } from "../get-host"; import { Cluster, ClusterToName, priceFeedsSchema } from "../services/pyth"; @@ -18,7 +18,7 @@ export async function getPublishersForFeedRequest( const data = await fetch(url, { next: { - revalidate: DEFAULT_CACHE_TTL, + revalidate: DEFAULT_NEXT_CACHE_TTL, }, headers: VERCEL_REQUEST_HEADERS, }); @@ -38,7 +38,7 @@ export async function getFeedsForPublisherRequest( const data = await fetch(url, { next: { - revalidate: DEFAULT_CACHE_TTL, + revalidate: DEFAULT_NEXT_CACHE_TTL, }, headers: VERCEL_REQUEST_HEADERS, }); @@ -54,7 +54,7 @@ export const getFeedsRequest = async (cluster: Cluster) => { const data = await fetch(url, { next: { - revalidate: DEFAULT_CACHE_TTL, + revalidate: DEFAULT_NEXT_CACHE_TTL, }, headers: VERCEL_REQUEST_HEADERS, }); @@ -82,7 +82,7 @@ export const getFeedForSymbolRequest = async ({ const data = await fetch(url, { next: { - revalidate: DEFAULT_CACHE_TTL, + revalidate: DEFAULT_NEXT_CACHE_TTL, }, headers: VERCEL_REQUEST_HEADERS, }); diff --git a/apps/insights/src/services/pyth/get-feeds.ts b/apps/insights/src/services/pyth/get-feeds.ts index 93cc9ff486..128a85abf6 100644 --- a/apps/insights/src/services/pyth/get-feeds.ts +++ b/apps/insights/src/services/pyth/get-feeds.ts @@ -1,9 +1,9 @@ import { Cluster, priceFeedsSchema } from "."; -import { getPythMetadataCached } from "./get-metadata"; +import { getPythMetadata } from "./get-metadata"; import { redisCache } from "../../cache"; const _getFeeds = async (cluster: Cluster) => { - const unfilteredData = await getPythMetadataCached(cluster); + const unfilteredData = await getPythMetadata(cluster); const filtered = unfilteredData.symbols .filter( (symbol) => diff --git a/apps/insights/src/services/pyth/get-metadata.ts b/apps/insights/src/services/pyth/get-metadata.ts index 66466d3c10..097bb12aa5 100644 --- a/apps/insights/src/services/pyth/get-metadata.ts +++ b/apps/insights/src/services/pyth/get-metadata.ts @@ -1,11 +1,5 @@ import { clients, Cluster } from "."; -// import { memoryOnlyCache } from "../../cache"; -export const getPythMetadataCached = async (cluster: Cluster) => { +export const getPythMetadata = async (cluster: Cluster) => { return clients[cluster].getData(); -}; - -// export const getPythMetadataCached = memoryOnlyCache.define( -// "getPythMetadata", -// getPythMetadata, -// ).getPythMetadata; +}; \ No newline at end of file diff --git a/apps/insights/src/services/pyth/get-publishers-for-cluster.ts b/apps/insights/src/services/pyth/get-publishers-for-cluster.ts index 452a369156..0c6fa5e854 100644 --- a/apps/insights/src/services/pyth/get-publishers-for-cluster.ts +++ b/apps/insights/src/services/pyth/get-publishers-for-cluster.ts @@ -1,9 +1,9 @@ import { Cluster } from "."; -import { getPythMetadataCached } from "./get-metadata"; +import { getPythMetadata } from "./get-metadata"; import { redisCache } from "../../cache"; const _getPublishersForCluster = async (cluster: Cluster) => { - const data = await getPythMetadataCached(cluster); + const data = await getPythMetadata(cluster); const result: Record = {}; for (const key of data.productPrice.keys()) { const price = data.productPrice.get(key); From 03dc3225f08d50da4a474a687f6d020bef21fadb Mon Sep 17 00:00:00 2001 From: Alexandru Cambose Date: Thu, 28 Aug 2025 18:43:15 +0200 Subject: [PATCH 2/3] feat: updated caching --- apps/insights/src/cache.ts | 6 +++--- apps/insights/src/server/pyth.ts | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/apps/insights/src/cache.ts b/apps/insights/src/cache.ts index 9e03887dc5..fe774585ff 100644 --- a/apps/insights/src/cache.ts +++ b/apps/insights/src/cache.ts @@ -10,10 +10,10 @@ const transformer = { }; /** - * - API routes will be cached for 1hour + * - API routes will be cached for 1 hour * - Cached function will be cached for 10 minutes, - * but if the function is called within 1 hour, it will - * still be served from the cache but also fetch the latest data + * If the function is called within 1 hour, it will + * still be served from the cache, but also fetch the latest data */ export const DEFAULT_NEXT_FETCH_TTL = 3600; // 1 hour export const DEFAULT_REDIS_CACHE_TTL = 60 * 10; // 10 minutes diff --git a/apps/insights/src/server/pyth.ts b/apps/insights/src/server/pyth.ts index 8b7a5fae20..cff93fcd03 100644 --- a/apps/insights/src/server/pyth.ts +++ b/apps/insights/src/server/pyth.ts @@ -1,7 +1,7 @@ import { parse } from "superjson"; import { z } from "zod"; -import { DEFAULT_NEXT_CACHE_TTL } from "../cache"; +import { DEFAULT_NEXT_FETCH_TTL } from "../cache"; import { VERCEL_REQUEST_HEADERS } from "../config/server"; import { getHost } from "../get-host"; import { Cluster, ClusterToName, priceFeedsSchema } from "../services/pyth"; @@ -18,7 +18,7 @@ export async function getPublishersForFeedRequest( const data = await fetch(url, { next: { - revalidate: DEFAULT_NEXT_CACHE_TTL, + revalidate: DEFAULT_NEXT_FETCH_TTL, }, headers: VERCEL_REQUEST_HEADERS, }); @@ -38,7 +38,7 @@ export async function getFeedsForPublisherRequest( const data = await fetch(url, { next: { - revalidate: DEFAULT_NEXT_CACHE_TTL, + revalidate: DEFAULT_NEXT_FETCH_TTL, }, headers: VERCEL_REQUEST_HEADERS, }); @@ -54,7 +54,7 @@ export const getFeedsRequest = async (cluster: Cluster) => { const data = await fetch(url, { next: { - revalidate: DEFAULT_NEXT_CACHE_TTL, + revalidate: DEFAULT_NEXT_FETCH_TTL, }, headers: VERCEL_REQUEST_HEADERS, }); @@ -82,7 +82,7 @@ export const getFeedForSymbolRequest = async ({ const data = await fetch(url, { next: { - revalidate: DEFAULT_NEXT_CACHE_TTL, + revalidate: DEFAULT_NEXT_FETCH_TTL, }, headers: VERCEL_REQUEST_HEADERS, }); From a4275824d2c55db3a02e7e978d5a64bc2078f0d8 Mon Sep 17 00:00:00 2001 From: Alexandru Cambose Date: Thu, 28 Aug 2025 20:39:56 +0200 Subject: [PATCH 3/3] fix: formatting --- apps/insights/src/cache.ts | 4 ++-- apps/insights/src/services/pyth/get-metadata.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/insights/src/cache.ts b/apps/insights/src/cache.ts index fe774585ff..6b1c830c61 100644 --- a/apps/insights/src/cache.ts +++ b/apps/insights/src/cache.ts @@ -11,8 +11,8 @@ const transformer = { /** * - API routes will be cached for 1 hour - * - Cached function will be cached for 10 minutes, - * If the function is called within 1 hour, it will + * - Cached function will be cached for 10 minutes, + * If the function is called within 1 hour, it will * still be served from the cache, but also fetch the latest data */ export const DEFAULT_NEXT_FETCH_TTL = 3600; // 1 hour diff --git a/apps/insights/src/services/pyth/get-metadata.ts b/apps/insights/src/services/pyth/get-metadata.ts index 097bb12aa5..79559f7870 100644 --- a/apps/insights/src/services/pyth/get-metadata.ts +++ b/apps/insights/src/services/pyth/get-metadata.ts @@ -2,4 +2,4 @@ import { clients, Cluster } from "."; export const getPythMetadata = async (cluster: Cluster) => { return clients[cluster].getData(); -}; \ No newline at end of file +};