Skip to content

Commit 56465a9

Browse files
authored
Merge pull request #3000 from pyth-network/fix/cache
feat: updated cache TTLs
2 parents 8e8172d + a427582 commit 56465a9

File tree

5 files changed

+21
-29
lines changed

5 files changed

+21
-29
lines changed

apps/insights/src/cache.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,24 @@ const transformer = {
99
deserialize: parse,
1010
};
1111

12-
// export const DEFAULT_CACHE_TTL = 3600; // 1 hour
13-
// export const DEFAULT_CACHE_STALE = 86_400; // 24 hours
14-
15-
16-
export const DEFAULT_CACHE_TTL = 60; // 1 minute
17-
export const DEFAULT_CACHE_STALE = 60; // 1 minute
12+
/**
13+
* - API routes will be cached for 1 hour
14+
* - Cached function will be cached for 10 minutes,
15+
* If the function is called within 1 hour, it will
16+
* still be served from the cache, but also fetch the latest data
17+
*/
18+
export const DEFAULT_NEXT_FETCH_TTL = 3600; // 1 hour
19+
export const DEFAULT_REDIS_CACHE_TTL = 60 * 10; // 10 minutes
20+
export const DEFAULT_REDIS_CACHE_STALE = 3600; // 1 hour
1821

1922
export const redisCache: ACDCache = createCache({
2023
transformer,
21-
stale: DEFAULT_CACHE_STALE,
22-
ttl: DEFAULT_CACHE_TTL,
24+
stale: DEFAULT_REDIS_CACHE_STALE,
25+
ttl: DEFAULT_REDIS_CACHE_TTL,
2326
storage: {
2427
type: "redis",
2528
options: {
2629
client: getRedis(),
2730
},
2831
},
2932
});
30-
31-
export const memoryOnlyCache: ACDCache = createCache({
32-
ttl: DEFAULT_CACHE_TTL,
33-
stale: DEFAULT_CACHE_STALE,
34-
});

apps/insights/src/server/pyth.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { parse } from "superjson";
22
import { z } from "zod";
33

4-
import { DEFAULT_CACHE_TTL } from "../cache";
4+
import { DEFAULT_NEXT_FETCH_TTL } from "../cache";
55
import { VERCEL_REQUEST_HEADERS } from "../config/server";
66
import { getHost } from "../get-host";
77
import { Cluster, ClusterToName, priceFeedsSchema } from "../services/pyth";
@@ -18,7 +18,7 @@ export async function getPublishersForFeedRequest(
1818

1919
const data = await fetch(url, {
2020
next: {
21-
revalidate: DEFAULT_CACHE_TTL,
21+
revalidate: DEFAULT_NEXT_FETCH_TTL,
2222
},
2323
headers: VERCEL_REQUEST_HEADERS,
2424
});
@@ -38,7 +38,7 @@ export async function getFeedsForPublisherRequest(
3838

3939
const data = await fetch(url, {
4040
next: {
41-
revalidate: DEFAULT_CACHE_TTL,
41+
revalidate: DEFAULT_NEXT_FETCH_TTL,
4242
},
4343
headers: VERCEL_REQUEST_HEADERS,
4444
});
@@ -54,7 +54,7 @@ export const getFeedsRequest = async (cluster: Cluster) => {
5454

5555
const data = await fetch(url, {
5656
next: {
57-
revalidate: DEFAULT_CACHE_TTL,
57+
revalidate: DEFAULT_NEXT_FETCH_TTL,
5858
},
5959
headers: VERCEL_REQUEST_HEADERS,
6060
});
@@ -82,7 +82,7 @@ export const getFeedForSymbolRequest = async ({
8282

8383
const data = await fetch(url, {
8484
next: {
85-
revalidate: DEFAULT_CACHE_TTL,
85+
revalidate: DEFAULT_NEXT_FETCH_TTL,
8686
},
8787
headers: VERCEL_REQUEST_HEADERS,
8888
});

apps/insights/src/services/pyth/get-feeds.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Cluster, priceFeedsSchema } from ".";
2-
import { getPythMetadataCached } from "./get-metadata";
2+
import { getPythMetadata } from "./get-metadata";
33
import { redisCache } from "../../cache";
44

55
const _getFeeds = async (cluster: Cluster) => {
6-
const unfilteredData = await getPythMetadataCached(cluster);
6+
const unfilteredData = await getPythMetadata(cluster);
77
const filtered = unfilteredData.symbols
88
.filter(
99
(symbol) =>
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import { clients, Cluster } from ".";
2-
// import { memoryOnlyCache } from "../../cache";
32

4-
export const getPythMetadataCached = async (cluster: Cluster) => {
3+
export const getPythMetadata = async (cluster: Cluster) => {
54
return clients[cluster].getData();
65
};
7-
8-
// export const getPythMetadataCached = memoryOnlyCache.define(
9-
// "getPythMetadata",
10-
// getPythMetadata,
11-
// ).getPythMetadata;

apps/insights/src/services/pyth/get-publishers-for-cluster.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Cluster } from ".";
2-
import { getPythMetadataCached } from "./get-metadata";
2+
import { getPythMetadata } from "./get-metadata";
33
import { redisCache } from "../../cache";
44

55
const _getPublishersForCluster = async (cluster: Cluster) => {
6-
const data = await getPythMetadataCached(cluster);
6+
const data = await getPythMetadata(cluster);
77
const result: Record<string, string[]> = {};
88
for (const key of data.productPrice.keys()) {
99
const price = data.productPrice.get(key);

0 commit comments

Comments
 (0)