Skip to content

Commit

Permalink
cache hourly
Browse files Browse the repository at this point in the history
  • Loading branch information
vrtnd committed Dec 9, 2024
1 parent f141750 commit 8100107
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/utils/bridgeVolume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getConfigsWithDestChain,
} from "./wrappa/postgres/query";
import { importBridgeNetwork } from "../data/importBridgeNetwork";
import { cache } from "./cache";

const startTimestampToRestrictTo = 1661990400; // Sept. 01, 2022: timestamp data is backfilled to

Expand Down Expand Up @@ -124,6 +125,11 @@ export const getHourlyBridgeVolume = async (
bridgeNetworkId?: number
) => {
let bridgeDbName = undefined as any;
const cacheKey = `hourly_bridge_volume_${bridgeNetworkId}_${chain}_${startTimestamp}_${endTimestamp}`;
const cachedData = await cache.get(cacheKey);
if (cachedData) {
return cachedData as any[];
}
if (bridgeNetworkId) {
const bridgeNetwork = importBridgeNetwork(undefined, bridgeNetworkId);
if (!bridgeNetwork) {
Expand Down Expand Up @@ -219,6 +225,7 @@ export const getHourlyBridgeVolume = async (
}
}
*/
await cache.set(cacheKey, hourlyBridgeVolume);

return hourlyBridgeVolume;
};
9 changes: 2 additions & 7 deletions src/utils/cache.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { LRUCache } from "lru-cache";
import hash from "object-hash";

const MAX_SIZE_BYTES = 50 * 1024 * 1024;

export const cache = new LRUCache({
maxSize: MAX_SIZE_BYTES,
sizeCalculation: (value: any) => {
return Buffer.byteLength(JSON.stringify(value), "utf8");
},
ttl: 100 * 60 * 12,
max: 1000,
ttl: 1000 * 60 * 60,
});

interface APIEvent {
Expand Down

0 comments on commit 8100107

Please sign in to comment.