1- import { cache } from 'react ' ;
1+ import type { PythHttpClientResult } from '@pythnetwork/client/lib/PythHttpClient ' ;
22import type { z } from 'zod' ;
33
44import { Cluster , clients , priceFeedsSchema } from "../services/pyth" ;
55import { createChunkedCacheFetcher , fetchAllChunks } from '../utils/cache' ;
66
7- const getDataCached = cache ( async ( cluster : Cluster ) => {
8- return clients [ cluster ] . getData ( ) ;
9- } ) ;
7+
8+ type CachedData = {
9+ data : PythHttpClientResult ;
10+ timestamp : number ;
11+ } ;
12+
13+ const dataCache = new Map < Cluster , CachedData > ( ) ;
14+ const CACHE_EXPIRY_MS = 24 * 60 * 60 ; // 1 day in seconds
15+
16+ const getDataCached = async ( cluster : Cluster ) => {
17+ const now = Date . now ( ) ;
18+ const cached = dataCache . get ( cluster ) ;
19+
20+ // Check if cache exists and is not expired
21+ if ( cached && ( now - cached . timestamp ) < CACHE_EXPIRY_MS * 1000 ) {
22+ return cached . data ;
23+ }
24+
25+ // Fetch fresh data
26+ const data = await clients [ cluster ] . getData ( ) ;
27+ dataCache . set ( cluster , { data, timestamp : now } ) ;
28+ return data ;
29+ } ;
1030
1131const fetchFeeds = createChunkedCacheFetcher ( async ( cluster : Cluster ) => {
1232 const unfilteredData = await getDataCached ( cluster ) ;
@@ -30,7 +50,7 @@ const fetchFeeds = createChunkedCacheFetcher(async (cluster: Cluster) => {
3050 } ) ) ;
3151 const parsedData = priceFeedsSchema . parse ( filteredData ) ;
3252 return parsedData ;
33- } , 'getfeeds ' ) ;
53+ } , 'getFeeds ' ) ;
3454
3555const fetchPublishers = createChunkedCacheFetcher ( async ( cluster : Cluster ) => {
3656 const data = await getDataCached ( cluster ) ;
@@ -40,7 +60,7 @@ const fetchPublishers = createChunkedCacheFetcher(async (cluster: Cluster) => {
4060 result [ key ] = price ?. priceComponents . map ( ( { publisher } ) => publisher . toBase58 ( ) ) ?? [ ] ;
4161 }
4262 return result ;
43- } , 'getpublishers ' ) ;
63+ } , 'getPublishers ' ) ;
4464
4565export const getFeedsCached = async ( cluster : Cluster ) => {
4666 return fetchAllChunks < z . infer < typeof priceFeedsSchema > , [ Cluster ] > ( fetchFeeds , cluster )
0 commit comments