Skip to content

Commit

Permalink
refactor(server/api): improve cache
Browse files Browse the repository at this point in the history
  • Loading branch information
steel97 committed Dec 15, 2024
1 parent 4494934 commit 7579e1d
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 83 deletions.
33 changes: 20 additions & 13 deletions server/api/getchaininfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useDataCache } from "#nuxt-multi-cache/composables";
import { setResponseHeader } from "h3";

const cacheInvalidateTime = 60;
const cacheInvalidateTimeRetry = cacheInvalidateTime - 2;

// chain info (explorer)
export default defineEventHandler(async (event) => {
Expand All @@ -12,31 +13,37 @@ export default defineEventHandler(async (event) => {
const runtimeConfig = useRuntimeConfig();

const { value, addToCache } = await useDataCache<BlockchainInfo>("chaininfo", event);
const cache = await useDataCache<boolean>("chaininfoCache", event);
if (value) {
return value;
}

const cTime = Math.round(new Date().getTime() / 1000);
let ret: BlockchainInfo = {
const ret: BlockchainInfo = {
status: false,
timestamp: cTime,
height: 0,
sizeOnDisk: 0,
};

try {
const data = await $fetch<any>(`${runtimeConfig.public.explorerBackendEndpoint}/api/getblockchaininfo`);
ret = {
status: true,
timestamp: cTime,
height: data.blocks as number,
sizeOnDisk: data.size_on_disk as number,
};
if (!cache.value) {
cache.addToCache(true, undefined, cacheInvalidateTimeRetry);
setTimeout(async () => {
try {
const data = await $fetch<any>(`${runtimeConfig.public.explorerBackendEndpoint}/api/getblockchaininfo`);
const cacheData = {
status: true,
timestamp: cTime,
height: data.blocks as number,
sizeOnDisk: data.size_on_disk as number,
};
addToCache(cacheData, undefined, cacheInvalidateTime);
}
catch (e) {

}
}, 1);
}
catch (e) {

}

addToCache(ret, undefined, cacheInvalidateTime);
return ret;
});
122 changes: 64 additions & 58 deletions server/api/getmirrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useDataCache } from "#nuxt-multi-cache/composables";
import { setResponseHeader } from "h3";

const cacheInvalidateTime = 60;
const cacheInvalidateTimeRetry = cacheInvalidateTime - 2;

// mirrors, probably can remove async, leave for now for feature things
export default defineEventHandler(async (event) => {
Expand All @@ -12,86 +13,91 @@ export default defineEventHandler(async (event) => {
const runtimeConfig = useRuntimeConfig();

const { value, addToCache } = await useDataCache<Networks>("mirrors", event);
const cache = await useDataCache<boolean>("mirrorsCache", event);
if (value) {
return value;
}

const cTime = Math.round(new Date().getTime() / 1000);
let ret: Networks = {
const ret: Networks = {
timestamp: cTime,
networks: {},
};

const mirrors = runtimeConfig.public.snapshotMirrors as Array<Network>;
const result: Networks = {
timestamp: 0,
networks: {},
};

for (const network of mirrors) {
const networkName = network.name as string;
const networkMirrors = network.mirrors as Array<any>;
const networkIcon = network.icon as string;

const resultMirrors: Array<Mirror> = [];

for (const mirror of networkMirrors) {
try {
const mirrorName = mirror.name;
const mirrorPath = mirror.path;

// get available snapshots
// excessive stack depth comparing types
// @ts-ignore
const snapshotInfo = (await $fetch(`${mirrorPath}snapshot.json`)) as any;

// construct data of mirror
const resultMirror: Mirror = {
name: mirrorName,
path: mirrorPath,
snapshots: new Array<Snapshot>(),
};

const snapshotResult: Array<Snapshot> = [];

for (const snapshot of snapshotInfo.snapshots) {
if (!cache.value) {
cache.addToCache(true, undefined, cacheInvalidateTimeRetry);
setTimeout(async () => {
const result: Networks = {
timestamp: 0,
networks: {},
};
for (const network of mirrors) {
const networkName = network.name as string;
const networkMirrors = network.mirrors as Array<any>;
const networkIcon = network.icon as string;

const resultMirrors: Array<Mirror> = [];

for (const mirror of networkMirrors) {
try {
const url = mirrorPath + snapshot.sha256;
const mirrorName = mirror.name;
const mirrorPath = mirror.path;

// get available snapshots
// excessive stack depth comparing types
// @ts-ignore
const response: string = await $fetch(url);
const hash = response.split(" ")[0];
snapshotResult.unshift({
name: snapshot.name as string,
sha256: hash ?? "",
block: snapshot.block as string,
date: snapshot.date as string,
});
const snapshotInfo = (await $fetch(`${mirrorPath}snapshot.json`)) as any;

// construct data of mirror
const resultMirror: Mirror = {
name: mirrorName,
path: mirrorPath,
snapshots: new Array<Snapshot>(),
};

const snapshotResult: Array<Snapshot> = [];

for (const snapshot of snapshotInfo.snapshots) {
try {
const url = mirrorPath + snapshot.sha256;
// excessive stack depth comparing types
// @ts-ignore
const response: string = await $fetch(url);
const hash = response.split(" ")[0];
snapshotResult.unshift({
name: snapshot.name as string,
sha256: hash ?? "",
block: snapshot.block as string,
date: snapshot.date as string,
});
}
catch {
// thrown if one of snapshots not found
}
}

resultMirror.snapshots = snapshotResult;

resultMirrors.push(resultMirror);
}
catch {
// thrown if one of snapshots not found

}
}

resultMirror.snapshots = snapshotResult;

resultMirrors.push(resultMirror);
result.timestamp = cTime;
result.networks[networkName.toLowerCase()] = {
icon: networkIcon,
name: networkName,
mirrors: resultMirrors,
};
}
catch {

}
}

result.timestamp = cTime;
result.networks[networkName.toLowerCase()] = {
icon: networkIcon,
name: networkName,
mirrors: resultMirrors,
};
addToCache(result, undefined, cacheInvalidateTime);
}, 1);
}

ret = result;

addToCache(ret, undefined, cacheInvalidateTime);
return ret;
});
31 changes: 19 additions & 12 deletions server/api/getprice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,43 @@ import { useDataCache } from "#nuxt-multi-cache/composables";
import { setResponseHeader } from "h3";

const cacheInvalidateTime = 60;
const cacheInvalidateTimeRetry = cacheInvalidateTime - 2;

// price data (coingecko)
export default defineEventHandler(async (event) => {
setResponseStatus(event, 200);
setResponseHeader(event, "content-type", "application/json");

const { value, addToCache } = await useDataCache<PriceInfo>("price", event);
const cache = await useDataCache<boolean>("priceCache", event);

if (value) {
return value;
}

let res: PriceInfo = {
const res: PriceInfo = {
status: false,
timestamp: Math.round(new Date().getTime() / 1000),
price: 0,
};

try {
const data = await $fetch<any>("https://api.coingecko.com/api/v3/simple/price?ids=veil&vs_currencies=usd");
const veilPrice = data.veil.usd as number;
res = {
status: true,
timestamp: Math.round(new Date().getTime() / 1000),
price: veilPrice,
};
}
catch (e) {
if (!cache.value) {
cache.addToCache(true, undefined, cacheInvalidateTimeRetry);
setTimeout(async () => {
try {
const data = await $fetch<any>("https://api.coingecko.com/api/v3/simple/price?ids=veil&vs_currencies=usd");
const veilPrice = data.veil.usd as number;
const cacheData = {
status: true,
timestamp: Math.round(new Date().getTime() / 1000),
price: veilPrice,
};
await addToCache(cacheData, undefined, cacheInvalidateTime);
}
catch (e) {
}
}, 1);
}

await addToCache(res, undefined, cacheInvalidateTime);
return res;
});

0 comments on commit 7579e1d

Please sign in to comment.