Skip to content

Commit

Permalink
refactor: DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Oct 16, 2023
1 parent 57c4d71 commit 1e85709
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/middlewares/useProxyCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ export default async function useProxyCache(req, res, next) {
try {
const cache = await get(cid);

ipfsGatewaysCacheHitCount.inc({ status: 'HIT' });
ipfsGatewaysCacheSize.inc({ status: 'HIT' }, cache.length);

updateCacheMetrics('HIT', cache.length);
res.set('Content-Type', await getContentType(cache));
return res.send(cache);
} catch (e) {
Expand All @@ -32,8 +30,7 @@ export default async function useProxyCache(req, res, next) {
const size = buffer.length;

if (size <= getMaxFileSize(contentType)) {
ipfsGatewaysCacheHitCount.inc({ status: 'MISS' });
ipfsGatewaysCacheSize.inc({ status: 'MISS' }, size);
updateCacheMetrics('HIT', size);
await set(cid, buffer);
}
} catch (e) {
Expand All @@ -46,3 +43,8 @@ export default async function useProxyCache(req, res, next) {

next();
}

function updateCacheMetrics(status: 'HIT' | 'MISS', size: number) {
ipfsGatewaysCacheHitCount.inc({ status });
ipfsGatewaysCacheSize.inc({ status }, size);
}

0 comments on commit 1e85709

Please sign in to comment.