1
- import { cache } from 'react ' ;
1
+ import type { PythHttpClientResult } from '@pythnetwork/client/lib/PythHttpClient ' ;
2
2
import type { z } from 'zod' ;
3
3
4
4
import { Cluster , clients , priceFeedsSchema } from "../services/pyth" ;
5
5
import { createChunkedCacheFetcher , fetchAllChunks } from '../utils/cache' ;
6
6
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
+ } ;
10
30
11
31
const fetchFeeds = createChunkedCacheFetcher ( async ( cluster : Cluster ) => {
12
32
const unfilteredData = await getDataCached ( cluster ) ;
@@ -30,7 +50,7 @@ const fetchFeeds = createChunkedCacheFetcher(async (cluster: Cluster) => {
30
50
} ) ) ;
31
51
const parsedData = priceFeedsSchema . parse ( filteredData ) ;
32
52
return parsedData ;
33
- } , 'getfeeds ' ) ;
53
+ } , 'getFeeds ' ) ;
34
54
35
55
const fetchPublishers = createChunkedCacheFetcher ( async ( cluster : Cluster ) => {
36
56
const data = await getDataCached ( cluster ) ;
@@ -40,7 +60,7 @@ const fetchPublishers = createChunkedCacheFetcher(async (cluster: Cluster) => {
40
60
result [ key ] = price ?. priceComponents . map ( ( { publisher } ) => publisher . toBase58 ( ) ) ?? [ ] ;
41
61
}
42
62
return result ;
43
- } , 'getpublishers ' ) ;
63
+ } , 'getPublishers ' ) ;
44
64
45
65
export const getFeedsCached = async ( cluster : Cluster ) => {
46
66
return fetchAllChunks < z . infer < typeof priceFeedsSchema > , [ Cluster ] > ( fetchFeeds , cluster )
0 commit comments