Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions apps/insights/src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,24 @@ const transformer = {
deserialize: parse,
};

// export const DEFAULT_CACHE_TTL = 3600; // 1 hour
// export const DEFAULT_CACHE_STALE = 86_400; // 24 hours


export const DEFAULT_CACHE_TTL = 60; // 1 minute
export const DEFAULT_CACHE_STALE = 60; // 1 minute
/**
* - API routes will be cached for 1 hour
* - Cached function will be cached for 10 minutes,
* If the function is called within 1 hour, it will
* still be served from the cache, but also fetch the latest data
*/
export const DEFAULT_NEXT_FETCH_TTL = 3600; // 1 hour
export const DEFAULT_REDIS_CACHE_TTL = 60 * 10; // 10 minutes
export const DEFAULT_REDIS_CACHE_STALE = 3600; // 1 hour

export const redisCache: ACDCache = createCache({
transformer,
stale: DEFAULT_CACHE_STALE,
ttl: DEFAULT_CACHE_TTL,
stale: DEFAULT_REDIS_CACHE_STALE,
ttl: DEFAULT_REDIS_CACHE_TTL,
storage: {
type: "redis",
options: {
client: getRedis(),
},
},
});

export const memoryOnlyCache: ACDCache = createCache({
ttl: DEFAULT_CACHE_TTL,
stale: DEFAULT_CACHE_STALE,
});
10 changes: 5 additions & 5 deletions apps/insights/src/server/pyth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { parse } from "superjson";
import { z } from "zod";

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

const data = await fetch(url, {
next: {
revalidate: DEFAULT_CACHE_TTL,
revalidate: DEFAULT_NEXT_FETCH_TTL,
},
headers: VERCEL_REQUEST_HEADERS,
});
Expand All @@ -38,7 +38,7 @@ export async function getFeedsForPublisherRequest(

const data = await fetch(url, {
next: {
revalidate: DEFAULT_CACHE_TTL,
revalidate: DEFAULT_NEXT_FETCH_TTL,
},
headers: VERCEL_REQUEST_HEADERS,
});
Expand All @@ -54,7 +54,7 @@ export const getFeedsRequest = async (cluster: Cluster) => {

const data = await fetch(url, {
next: {
revalidate: DEFAULT_CACHE_TTL,
revalidate: DEFAULT_NEXT_FETCH_TTL,
},
headers: VERCEL_REQUEST_HEADERS,
});
Expand Down Expand Up @@ -82,7 +82,7 @@ export const getFeedForSymbolRequest = async ({

const data = await fetch(url, {
next: {
revalidate: DEFAULT_CACHE_TTL,
revalidate: DEFAULT_NEXT_FETCH_TTL,
},
headers: VERCEL_REQUEST_HEADERS,
});
Expand Down
4 changes: 2 additions & 2 deletions apps/insights/src/services/pyth/get-feeds.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Cluster, priceFeedsSchema } from ".";
import { getPythMetadataCached } from "./get-metadata";
import { getPythMetadata } from "./get-metadata";
import { redisCache } from "../../cache";

const _getFeeds = async (cluster: Cluster) => {
const unfilteredData = await getPythMetadataCached(cluster);
const unfilteredData = await getPythMetadata(cluster);
const filtered = unfilteredData.symbols
.filter(
(symbol) =>
Expand Down
8 changes: 1 addition & 7 deletions apps/insights/src/services/pyth/get-metadata.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { clients, Cluster } from ".";
// import { memoryOnlyCache } from "../../cache";

export const getPythMetadataCached = async (cluster: Cluster) => {
export const getPythMetadata = async (cluster: Cluster) => {
return clients[cluster].getData();
};

// export const getPythMetadataCached = memoryOnlyCache.define(
// "getPythMetadata",
// getPythMetadata,
// ).getPythMetadata;
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Cluster } from ".";
import { getPythMetadataCached } from "./get-metadata";
import { getPythMetadata } from "./get-metadata";
import { redisCache } from "../../cache";

const _getPublishersForCluster = async (cluster: Cluster) => {
const data = await getPythMetadataCached(cluster);
const data = await getPythMetadata(cluster);
const result: Record<string, string[]> = {};
for (const key of data.productPrice.keys()) {
const price = data.productPrice.get(key);
Expand Down
Loading