diff --git a/server/api/getchaininfo.ts b/server/api/getchaininfo.ts index d6dd432..ff09082 100644 --- a/server/api/getchaininfo.ts +++ b/server/api/getchaininfo.ts @@ -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) => { @@ -12,31 +13,37 @@ export default defineEventHandler(async (event) => { const runtimeConfig = useRuntimeConfig(); const { value, addToCache } = await useDataCache("chaininfo", event); + const cache = await useDataCache("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(`${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(`${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; }); \ No newline at end of file diff --git a/server/api/getmirrors.ts b/server/api/getmirrors.ts index 8e06f69..02a0d6d 100644 --- a/server/api/getmirrors.ts +++ b/server/api/getmirrors.ts @@ -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) => { @@ -12,86 +13,91 @@ export default defineEventHandler(async (event) => { const runtimeConfig = useRuntimeConfig(); const { value, addToCache } = await useDataCache("mirrors", event); + const cache = await useDataCache("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; - const result: Networks = { - timestamp: 0, - networks: {}, - }; - - for (const network of mirrors) { - const networkName = network.name as string; - const networkMirrors = network.mirrors as Array; - const networkIcon = network.icon as string; - - const resultMirrors: Array = []; - - 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(), - }; - const snapshotResult: Array = []; - - 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; + const networkIcon = network.icon as string; + + const resultMirrors: Array = []; + + 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(), + }; + + const snapshotResult: Array = []; + + 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; }); \ No newline at end of file diff --git a/server/api/getprice.ts b/server/api/getprice.ts index 4cef910..2654634 100644 --- a/server/api/getprice.ts +++ b/server/api/getprice.ts @@ -3,6 +3,7 @@ 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) => { @@ -10,29 +11,35 @@ export default defineEventHandler(async (event) => { setResponseHeader(event, "content-type", "application/json"); const { value, addToCache } = await useDataCache("price", event); + const cache = await useDataCache("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("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("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; }); \ No newline at end of file