Skip to content

Commit

Permalink
Cache all packets
Browse files Browse the repository at this point in the history
  • Loading branch information
Inkvi committed Mar 9, 2024
1 parent e99621c commit 52f6131
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
10 changes: 9 additions & 1 deletion app/api/packets/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NextRequest, NextResponse } from 'next/server';
import { Packet, PacketStates } from 'utils/types/packet';
import { CHAIN, CHAIN_CONFIGS } from 'utils/chains/configs';
import { CachingJsonRpcProvider } from 'api/utils/provider-cache';
import { GetTmClient } from 'api/utils/cosmos';
import { getCacheTTL, GetTmClient, SimpleCache } from 'api/utils/cosmos';
import { IdentifiedChannel } from 'cosmjs-types/ibc/core/channel/v1/channel';
import { QueryChannelsResponse } from 'cosmjs-types/ibc/core/channel/v1/query';
import Abi from 'utils/dispatcher.json';
Expand All @@ -25,6 +25,12 @@ export async function GET(request: NextRequest) {
return NextResponse.error();
}

const cache = SimpleCache.getInstance()
const allPackets = cache.get("allPackets");
if (allPackets) {
return NextResponse.json(allPackets);
}

const channelsResponse = await getChannels();
if (!channelsResponse) {
return NextResponse.error();
Expand Down Expand Up @@ -323,5 +329,7 @@ export async function GET(request: NextRequest) {
Object.keys(packets).forEach((key) => {
response.push(packets[key]);
});

cache.set("allPackets", response, getCacheTTL());
return NextResponse.json(response);
}
10 changes: 7 additions & 3 deletions app/api/utils/cosmos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ interface ICache {
set<T>(key: string, value: T, ttl: number): void;
}

class SimpleCache implements ICache {
export class SimpleCache implements ICache {
private static instance: SimpleCache;
private cache: NodeCache;

private constructor(defaultTTL: number) {
this.cache = new NodeCache({ stdTTL: defaultTTL });
}

public static getInstance(defaultTTL: number = 60): SimpleCache {
public static getInstance(defaultTTL: number = getCacheTTL()): SimpleCache {
if (!SimpleCache.instance) {
SimpleCache.instance = new SimpleCache(defaultTTL);
}
Expand Down Expand Up @@ -131,9 +131,13 @@ class CachingIbcExtension {
}


export function getCacheTTL() {
return process.env.CACHE_TTL ? parseInt(process.env.CACHE_TTL) : 60;
}

function setupCachingIbcExtension(base: QueryClient) {
const ibcExtension = setupIbcExtension(base);
const cacheTTL = process.env.CACHE_TTL ? parseInt(process.env.CACHE_TTL) : 60;
const cacheTTL = getCacheTTL();
return new CachingIbcExtension(ibcExtension, cacheTTL).ibc;
}

Expand Down
2 changes: 1 addition & 1 deletion app/api/utils/provider-cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethers } from 'ethers';
import { ethers } from 'ethers';
import * as flatCache from 'flat-cache';

export class CachingJsonRpcProvider extends ethers.JsonRpcProvider {
Expand Down

0 comments on commit 52f6131

Please sign in to comment.