1+ import { unstable_cache } from "next/cache" ;
12import superjson from "superjson" ;
2- import { z } from ' zod' ;
3+ import { z } from " zod" ;
34
5+ // Your imports
46import { Cluster , clients , priceFeedsSchema } from "../services/pyth" ;
57
6- export const getPublishersForFeed = async (
7- cluster : Cluster ,
8- ) => {
9- "use cache" ;
8+ // Backing implementation: NOT exported, never called directly except by cache wrapper
9+ const _getPublishersForFeed = async ( cluster : Cluster ) => {
1010 const start = performance . now ( ) ;
1111 const data = await clients [ cluster ] . getData ( ) ;
1212 const result : Record < string , string [ ] > = { } ;
@@ -15,56 +15,62 @@ export const getPublishersForFeed = async (
1515 result [ key ] = price ?. priceComponents . map ( ( { publisher } ) => publisher . toBase58 ( ) ) ?? [ ] ;
1616 }
1717 const end = performance . now ( ) ;
18+
1819 // eslint-disable-next-line no-console, @typescript-eslint/restrict-template-expressions
19- console . log ( `getPublishersForFeed: ${ end - start } ms` ) ;
20+ console . log ( `getPublishersForFeed: ${ end - start } ms` , result . length ) ;
2021 return result ;
2122} ;
2223
23- const getFeeds = async ( cluster : Cluster ) => {
24- "use cache" ;
24+ // Next.js unstable_cache wrapper; cluster is used as key
25+ export const getPublishersForFeed = unstable_cache (
26+ _getPublishersForFeed ,
27+ [ ] ,
28+ { revalidate : false }
29+ ) ;
30+
31+ const _getFeeds = async ( cluster : Cluster ) => {
2532 const start = performance . now ( ) ;
2633 const data = await clients [ cluster ] . getData ( ) ;
27-
28- const result = superjson . stringify ( priceFeedsSchema . parse ( data . symbols . filter (
29- ( symbol ) =>
30- data . productFromSymbol . get ( symbol ) ?. display_symbol !== undefined ,
31- ) . map ( ( symbol ) => ( {
32- symbol,
33- product : data . productFromSymbol . get ( symbol ) ,
34- price : {
35- ...data . productPrice . get ( symbol ) ,
36- priceComponents : data . productPrice . get ( symbol ) ?. priceComponents . map ( ( { publisher } ) => ( {
37- publisher : publisher . toBase58 ( ) ,
38- } ) ) ?? [ ] ,
39- } ,
40- } ) ) ) )
41- const end = performance . now ( ) ;
42- // eslint-disable-next-line no-console, @typescript-eslint/restrict-template-expressions
43- console . log ( `getFeeds: ${ end - start } ms` ) ;
44- return result ;
45- }
34+ const parsedData = priceFeedsSchema . parse (
35+ data . symbols
36+ . filter (
37+ ( symbol ) =>
38+ data . productFromSymbol . get ( symbol ) ?. display_symbol !== undefined
39+ )
40+ . map ( ( symbol ) => ( {
41+ symbol,
42+ product : data . productFromSymbol . get ( symbol ) ,
43+ price : {
44+ ...data . productPrice . get ( symbol ) ,
45+ priceComponents :
46+ data . productPrice
47+ . get ( symbol )
48+ ?. priceComponents . map ( ( { publisher } ) => ( {
49+ publisher : publisher . toBase58 ( ) ,
50+ } ) ) ?? [ ] ,
51+ } ,
52+ } ) )
53+ )
54+ const result = superjson . stringify (
55+ parsedData
56+ ) ;
4657
47- export const getFeedsForPublisherCached = async (
48- cluster : Cluster ,
49- publisher : string ,
50- ) => {
51- const start = performance . now ( ) ;
52- const rawFeeds = await getFeeds ( cluster ) ;
53- const feeds = superjson . parse < z . infer < typeof priceFeedsSchema > > ( rawFeeds ) ;
5458 const end = performance . now ( ) ;
5559 // eslint-disable-next-line no-console, @typescript-eslint/restrict-template-expressions
56- console . log ( `getFeedsForPublisherCached: ${ end - start } ms` ) ;
57- return priceFeedsSchema . parse ( feeds . filter ( ( { price } ) =>
58- price . priceComponents . some (
59- ( component ) => component . publisher . toString ( ) === publisher ,
60- ) ,
61- ) ) ;
60+ console . log ( `getFeeds: ${ end - start } ms` , parsedData . length , result . length ) ;
61+ return result ;
6262} ;
6363
64+ export const getFeeds = unstable_cache (
65+ _getFeeds ,
66+ [ ] ,
67+ { revalidate : false }
68+ ) ;
69+
70+ // Now getFeedsCached simply uses the cached version
6471export const getFeedsCached = async ( cluster : Cluster ) => {
65- "use cache" ;
6672 const start = performance . now ( ) ;
67- const rawFeeds = await getFeeds ( cluster ) ;
73+ const rawFeeds = await getFeeds ( cluster ) ; // uses cache
6874 const end = performance . now ( ) ;
6975 // eslint-disable-next-line no-console, @typescript-eslint/restrict-template-expressions
7076 console . log ( `getFeedsCached: ${ end - start } ms` ) ;
@@ -73,9 +79,28 @@ export const getFeedsCached = async (cluster: Cluster) => {
7379
7480export const getPublishersForFeedCached = async ( cluster : Cluster , symbol : string ) => {
7581 const start = performance . now ( ) ;
76- const data = await getPublishersForFeed ( cluster ) ;
82+ const data = await getPublishersForFeed ( cluster ) ; // uses cache
7783 const end = performance . now ( ) ;
7884 // eslint-disable-next-line no-console, @typescript-eslint/restrict-template-expressions
7985 console . log ( `getPublishersForFeedCached: ${ end - start } ms` ) ;
8086 return data [ symbol ] ;
81- } ;
87+ } ;
88+
89+ export const getFeedsForPublisherCached = async (
90+ cluster : Cluster ,
91+ publisher : string
92+ ) => {
93+ const start = performance . now ( ) ;
94+ const rawFeeds = await getFeeds ( cluster ) ; // uses cache
95+ const feeds = superjson . parse < z . infer < typeof priceFeedsSchema > > ( rawFeeds ) ;
96+ const end = performance . now ( ) ;
97+ // eslint-disable-next-line no-console, @typescript-eslint/restrict-template-expressions
98+ console . log ( `getFeedsForPublisherCached: ${ end - start } ms.` ) ;
99+ return priceFeedsSchema . parse (
100+ feeds . filter ( ( { price } ) =>
101+ price . priceComponents . some (
102+ ( component ) => component . publisher . toString ( ) === publisher
103+ )
104+ )
105+ ) ;
106+ } ;
0 commit comments