1- import { parse , } from "superjson" ;
1+ import { unstable_cache } from "next/cache" ;
2+ import { parse , stringify } from "superjson" ;
23import { z } from "zod" ;
34
45import { PUBLIC_URL , VERCEL_AUTOMATION_BYPASS_SECRET } from '../config/server' ;
56import { Cluster , priceFeedsSchema } from "../services/pyth" ;
67import { DEFAULT_CACHE_TTL } from "../utils/cache" ;
8+ import { getFeedsCached as _getFeedsCached } from "./pyth/get-feeds" ;
79
8- // Convenience helpers matching your previous functions
910export async function getPublishersForFeedCached (
1011 cluster : Cluster ,
1112 symbol : string
@@ -37,7 +38,17 @@ export async function getFeedsForPublisherCached(
3738 return parse < z . infer < typeof priceFeedsSchema > > ( rawData ) ;
3839}
3940
40- export const getFeedsCached = async ( cluster : Cluster ) => {
41+ const _privateGetFeeds = async ( cluster : Cluster ) => {
42+ // const start = Date.now();
43+ // await new Promise((resolve) => setTimeout(resolve, 10_000));
44+ // const feeds = await _getFeedsCached(cluster) as Omit<z.infer<typeof priceFeedsSchema>[number], 'price'>[];
45+ // const end = Date.now();
46+ // // eslint-disable-next-line no-console
47+ // console.log("internal getFeedsCached", cluster, end - start);
48+ // return feeds.map((feed) => ({
49+ // ...feed,
50+ // price: undefined,
51+ // }));
4152 const data = await fetch ( `${ PUBLIC_URL } /api/pyth/get-feeds?cluster=${ cluster . toString ( ) } &excludePriceComponents=true` , {
4253 next : {
4354 revalidate : DEFAULT_CACHE_TTL ,
@@ -49,23 +60,38 @@ export const getFeedsCached = async (cluster: Cluster) => {
4960 const dataJson = await data . text ( ) ;
5061 const feeds : Omit < z . infer < typeof priceFeedsSchema > [ 0 ] , "price" > [ ] = parse ( dataJson ) ;
5162 return feeds ;
63+ } ;
64+ export const getFeedsCached = async ( cluster : Cluster ) => {
65+ const start = Date . now ( ) ;
66+ const data = await _privateGetFeeds ( cluster ) ;
67+ const end = Date . now ( ) ;
68+ // eslint-disable-next-line no-console
69+ console . log ( "getFeedsCached" , end - start ) ;
70+ return data ;
71+ } ;
72+
73+ const _getFeedsForSymbol = async ( symbol : string , cluster : Cluster ) => {
74+ const feeds = await _getFeedsCached ( cluster ) ;
75+ const feed = feeds . find ( ( feed ) => feed . symbol === symbol ) ;
76+ return feed ;
5277}
5378
54- export const getFeedForSymbolCached = async ( { symbol, cluster = Cluster . Pythnet } : { symbol : string , cluster ?: Cluster } ) : Promise < z . infer < typeof priceFeedsSchema > [ 0 ] | undefined > => {
55- const data = await fetch ( `${ PUBLIC_URL } /api/pyth/get-feeds/${ encodeURIComponent ( symbol ) } ?cluster=${ cluster . toString ( ) } ` , {
56- next : {
57- revalidate : DEFAULT_CACHE_TTL ,
58- } ,
59- headers : {
60- 'x-vercel-protection-bypass' : VERCEL_AUTOMATION_BYPASS_SECRET ,
61- } ,
62- } ) ;
63-
64- if ( ! data . ok ) {
65- return undefined ;
66- }
6779
68- const dataJson = await data . text ( ) ;
69- const feed : z . infer < typeof priceFeedsSchema > [ 0 ] = parse ( dataJson ) ;
70- return feed ;
71- }
80+
81+ export const getFeedForSymbol = unstable_cache ( async ( { symbol, cluster = Cluster . Pythnet } : { symbol : string , cluster ?: Cluster } ) => {
82+ const data = await _getFeedsForSymbol ( symbol , cluster ) ;
83+ await new Promise ( ( resolve ) => setTimeout ( resolve , 10_000 ) ) ;
84+
85+ return stringify ( data ) ;
86+ } , [ 'getFeedForSymbolCached__' ] , {
87+ revalidate : DEFAULT_CACHE_TTL ,
88+ } ) ;
89+
90+ export const getFeedForSymbolCached = async ( { symbol, cluster = Cluster . Pythnet } : { symbol : string , cluster ?: Cluster } ) => {
91+ const start = Date . now ( ) ;
92+ const data = await getFeedForSymbol ( { symbol, cluster} ) ;
93+ const end = Date . now ( ) ;
94+ // eslint-disable-next-line no-console
95+ console . log ( "getFeedForSymbolCached" , symbol , cluster , end - start ) ;
96+ return parse < ReturnType < typeof _getFeedsForSymbol > > ( data ) ;
97+ } ;
0 commit comments