Skip to content

Commit b9eaee7

Browse files
committed
chore: test with unstable_cache
1 parent 1872162 commit b9eaee7

File tree

5 files changed

+70
-42
lines changed

5 files changed

+70
-42
lines changed

apps/insights/src/app/price-feeds/[slug]/layout.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Metadata } from "next";
22
import { notFound } from "next/navigation";
33
import type { ReactNode } from "react";
44

5-
import { getFeedsCached } from "../../../server/pyth/get-feeds";
5+
import { getFeedsCached } from "../../../server/pyth";
66
import { Cluster } from "../../../services/pyth";
77

88
export { PriceFeedLayout as default } from "../../../components/PriceFeed/layout";

apps/insights/src/components/Root/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from "../../config/server";
1212
import { LivePriceDataProvider } from "../../hooks/use-live-price-data";
1313
import { getPublishersCached } from '../../server/clickhouse';
14-
import { getFeedsCached } from '../../server/pyth/get-feeds';
14+
import { getFeedsCached } from '../../server/pyth';
1515
import { Cluster } from "../../services/pyth";
1616
import { PriceFeedIcon } from "../PriceFeedIcon";
1717
import { PublisherIcon } from "../PublisherIcon";

apps/insights/src/config/server.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Disable the following rule because this file is the intended place to declare
22
// and load all env variables.
3-
/* eslint-disable n/no-process-env, @typescript-eslint/no-non-null-assertion, turbo/no-undeclared-env-vars */
3+
/* eslint-disable n/no-process-env */
44

55
import { Redis } from 'ioredis';
66
import "server-only";
@@ -63,10 +63,7 @@ export const ENABLE_ACCESSIBILITY_REPORTING =
6363
let redisClient: Redis | undefined;
6464

6565
export function getRedis(): Redis {
66-
const url = process.env.REDIS_URL;
67-
if (!url) {
68-
throw new Error('REDIS_URL must be set');
69-
}
66+
const url = demand("REDIS_URL");
7067
if(redisClient) {
7168
return redisClient;
7269
}
@@ -76,13 +73,14 @@ export function getRedis(): Redis {
7673

7774
export const PUBLIC_URL = (() => {
7875
if (IS_PRODUCTION_SERVER) {
79-
80-
return `https://${process.env.VERCEL_PROJECT_PRODUCTION_URL!}`;
76+
const productionUrl = demand("VERCEL_PROJECT_PRODUCTION_URL");
77+
return `https://${productionUrl}`;
8178
} else if (IS_PREVIEW_SERVER) {
82-
return `https://${process.env.VERCEL_URL!}`;
79+
const previewUrl = demand("VERCEL_URL");
80+
return `https://${previewUrl}`;
8381
} else {
8482
return `http://localhost:3003`;
8583
}
8684
})();
8785

88-
export const VERCEL_AUTOMATION_BYPASS_SECRET = process.env.VERCEL_AUTOMATION_BYPASS_SECRET!;
86+
export const VERCEL_AUTOMATION_BYPASS_SECRET = demand("VERCEL_AUTOMATION_BYPASS_SECRET");

apps/insights/src/server/pyth.ts

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { parse, } from "superjson";
1+
import { unstable_cache } from "next/cache";
2+
import { parse,stringify } from "superjson";
23
import { z } from "zod";
34

45
import { PUBLIC_URL, VERCEL_AUTOMATION_BYPASS_SECRET } from '../config/server';
56
import { Cluster, priceFeedsSchema } from "../services/pyth";
67
import { DEFAULT_CACHE_TTL } from "../utils/cache";
8+
import { getFeedsCached as _getFeedsCached } from "./pyth/get-feeds";
79

8-
// Convenience helpers matching your previous functions
910
export async function getPublishersForFeedCached(
1011
cluster: Cluster,
1112
symbol: string
@@ -37,35 +38,63 @@ export async function getFeedsForPublisherCached(
3738
return parse<z.infer<typeof priceFeedsSchema>>(rawData);
3839
}
3940

41+
const _privateGetFeeds =unstable_cache(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+
}));
52+
// const data = await fetch(`${PUBLIC_URL}/api/pyth/get-feeds?cluster=${cluster.toString()}&excludePriceComponents=true`, {
53+
// next: {
54+
// revalidate: DEFAULT_CACHE_TTL,
55+
// },
56+
// headers: {
57+
// 'x-vercel-protection-bypass': VERCEL_AUTOMATION_BYPASS_SECRET,
58+
// },
59+
// });
60+
// const dataJson = await data.text();
61+
// const feeds: Omit<z.infer<typeof priceFeedsSchema>[0], "price">[] = parse(dataJson);
62+
// return feeds;
63+
}, ['getFeedsCached__'], {
64+
revalidate: DEFAULT_CACHE_TTL,
65+
});
66+
4067
export const getFeedsCached = async (cluster: Cluster) => {
41-
const data = await fetch(`${PUBLIC_URL}/api/pyth/get-feeds?cluster=${cluster.toString()}&excludePriceComponents=true`, {
42-
next: {
43-
revalidate: DEFAULT_CACHE_TTL,
44-
},
45-
headers: {
46-
'x-vercel-protection-bypass': VERCEL_AUTOMATION_BYPASS_SECRET,
47-
},
48-
});
49-
const dataJson = await data.text();
50-
const feeds: Omit<z.infer<typeof priceFeedsSchema>[0], "price">[] = parse(dataJson);
51-
return feeds;
68+
const start = Date.now();
69+
const data = await _privateGetFeeds(cluster);
70+
const end = Date.now();
71+
// eslint-disable-next-line no-console
72+
console.log("getFeedsCached", end - start);
73+
return data;
74+
};
75+
76+
const _getFeedsForSymbol = async (symbol: string, cluster: Cluster) => {
77+
const feeds = await _getFeedsCached(cluster);
78+
const feed = feeds.find((feed) => feed.symbol === symbol);
79+
return feed;
5280
}
5381

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-
}
6782

68-
const dataJson = await data.text();
69-
const feed: z.infer<typeof priceFeedsSchema>[0] = parse(dataJson);
70-
return feed;
71-
}
83+
84+
export const getFeedForSymbol = unstable_cache(async ({symbol, cluster = Cluster.Pythnet}: {symbol: string, cluster?: Cluster}) => {
85+
const data = await _getFeedsForSymbol(symbol, cluster);
86+
await new Promise((resolve) => setTimeout(resolve, 10_000));
87+
88+
return stringify(data);
89+
}, ['getFeedForSymbolCached__'], {
90+
revalidate: DEFAULT_CACHE_TTL,
91+
});
92+
93+
export const getFeedForSymbolCached = async ({symbol, cluster = Cluster.Pythnet}: {symbol: string, cluster?: Cluster}) => {
94+
const start = Date.now();
95+
const data = await getFeedForSymbol({symbol, cluster});
96+
const end = Date.now();
97+
// eslint-disable-next-line no-console
98+
console.log("getFeedForSymbolCached", symbol, cluster, end - start);
99+
return parse<ReturnType<typeof _getFeedsForSymbol>>(data);
100+
};

apps/insights/turbo.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"DISABLE_ACCESSIBILITY_REPORTING",
1717
"NEXT_PUBLIC_PYTHNET_RPC",
1818
"NEXT_PUBLIC_PYTHTEST_CONFORMANCE_RPC",
19-
"REDIS_URL"
19+
"REDIS_URL",
20+
"VERCEL_PROJECT_PRODUCTION_URL"
2021
]
2122
},
2223
"fix:lint": {

0 commit comments

Comments
 (0)