|
| 1 | +import { unstable_cache } from "next/cache"; |
1 | 2 | import { parse, } from "superjson"; |
2 | 3 | import { z } from "zod"; |
3 | 4 |
|
4 | 5 | import { PUBLIC_URL, VERCEL_AUTOMATION_BYPASS_SECRET } from '../config/server'; |
5 | 6 | import { Cluster, priceFeedsSchema } from "../services/pyth"; |
6 | 7 | import { DEFAULT_CACHE_TTL } from "../utils/cache"; |
| 8 | +import { getFeedsCached as _getFeedsCached } from "./pyth/get-feeds"; |
7 | 9 |
|
8 | 10 | export async function getPublishersForFeedCached( |
9 | 11 | cluster: Cluster, |
@@ -36,19 +38,39 @@ export async function getFeedsForPublisherCached( |
36 | 38 | return parse<z.infer<typeof priceFeedsSchema>>(rawData); |
37 | 39 | } |
38 | 40 |
|
| 41 | +const _privateGetFeeds =unstable_cache(async (cluster: Cluster) => { |
| 42 | + const start = Date.now(); |
| 43 | + const feeds = await _getFeedsCached(cluster) as Omit<z.infer<typeof priceFeedsSchema>[number], 'price'>[]; |
| 44 | + const end = Date.now(); |
| 45 | + // eslint-disable-next-line no-console |
| 46 | + console.log("internal getFeedsCached", cluster, end - start); |
| 47 | + return feeds.map((feed) => ({ |
| 48 | + ...feed, |
| 49 | + price: undefined, |
| 50 | + })); |
| 51 | + // const data = await fetch(`${PUBLIC_URL}/api/pyth/get-feeds?cluster=${cluster.toString()}&excludePriceComponents=true`, { |
| 52 | + // next: { |
| 53 | + // revalidate: DEFAULT_CACHE_TTL, |
| 54 | + // }, |
| 55 | + // headers: { |
| 56 | + // 'x-vercel-protection-bypass': VERCEL_AUTOMATION_BYPASS_SECRET, |
| 57 | + // }, |
| 58 | + // }); |
| 59 | + // const dataJson = await data.text(); |
| 60 | + // const feeds: Omit<z.infer<typeof priceFeedsSchema>[0], "price">[] = parse(dataJson); |
| 61 | + // return feeds; |
| 62 | +}, [], { |
| 63 | + revalidate: DEFAULT_CACHE_TTL, |
| 64 | +}); |
| 65 | + |
39 | 66 | export const getFeedsCached = async (cluster: Cluster) => { |
40 | | - const data = await fetch(`${PUBLIC_URL}/api/pyth/get-feeds?cluster=${cluster.toString()}&excludePriceComponents=true`, { |
41 | | - next: { |
42 | | - revalidate: DEFAULT_CACHE_TTL, |
43 | | - }, |
44 | | - headers: { |
45 | | - 'x-vercel-protection-bypass': VERCEL_AUTOMATION_BYPASS_SECRET, |
46 | | - }, |
47 | | - }); |
48 | | - const dataJson = await data.text(); |
49 | | - const feeds: Omit<z.infer<typeof priceFeedsSchema>[0], "price">[] = parse(dataJson); |
50 | | - return feeds; |
51 | | -} |
| 67 | + const start = Date.now(); |
| 68 | + const data = await _privateGetFeeds(cluster); |
| 69 | + const end = Date.now(); |
| 70 | + // eslint-disable-next-line no-console |
| 71 | + console.log("getFeedsCached", end - start); |
| 72 | + return data; |
| 73 | +}; |
52 | 74 |
|
53 | 75 | export const getFeedForSymbolCached = async ({symbol, cluster = Cluster.Pythnet}: {symbol: string, cluster?: Cluster}): Promise<z.infer<typeof priceFeedsSchema>[0] | undefined> => { |
54 | 76 | const data = await fetch(`${PUBLIC_URL}/api/pyth/get-feeds/${encodeURIComponent(symbol)}?cluster=${cluster.toString()}`, { |
|
0 commit comments