-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: base caching #55
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { NextFunction, Request, Response } from "express"; | ||
import NodeCache from "node-cache"; | ||
|
||
const DEFAULT_TTL = 60; // 1 minute | ||
const cache = new NodeCache(); | ||
|
||
//FIXME: This is a temporary cache implementation. It is not recommended for production use. | ||
// might be replaced with a more robust solution in the future. | ||
/** | ||
* A middleware that caches responses for a given time to live (TTL). | ||
* @param args - The time to live (TTL) in seconds for the cached response. | ||
* @returns A middleware function that caches responses for a given time to live (TTL). | ||
*/ | ||
export function cacheMiddleware(args: { ttl: number } = { ttl: DEFAULT_TTL }) { | ||
return async (req: Request, res: Response, next: NextFunction): Promise<void> => { | ||
const key = req.originalUrl || req.url; | ||
const cachedResponse = await cache.get(key); | ||
if (cachedResponse) { | ||
// Check if the cached response is a JSON object or plain text | ||
res.json(cachedResponse); | ||
} else { | ||
// Store the original send and json functions | ||
const originalJson = res.json.bind(res); | ||
// Override the json function | ||
|
||
res.json = (body): Response => { | ||
// Cache the response body | ||
cache.set(key, body, args.ttl); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is just a temporary solution, no idea how many items can NodeCache handle. In this case we are just gona have 2 items. |
||
// Call the original json function with the response body | ||
return originalJson(body); | ||
}; | ||
|
||
next(); | ||
} | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,11 +1,11 @@ | ||||||
import { inspect } from "util"; | ||||||
import { caching } from "cache-manager"; | ||||||
|
||||||
import { EvmProvider } from "@zkchainhub/chain-providers/dist/src/index.js"; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
import { L1MetricsService } from "@zkchainhub/metrics"; | ||||||
import { CoingeckoProvider } from "@zkchainhub/pricing"; | ||||||
import { Logger } from "@zkchainhub/shared"; | ||||||
|
||||||
import { EvmProvider } from "../../../packages/chain-providers/dist/src/index.js"; | ||||||
import { App } from "./app.js"; | ||||||
import { config } from "./common/config/index.js"; | ||||||
import { MetricsController, MetricsRouter } from "./metrics/index.js"; | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,6 +1,7 @@ | ||||||
import { Address, encodeFunctionData, erc20Abi, parseEther, zeroAddress } from "viem"; | ||||||
import { afterEach, describe, expect, it, Mocked, vi } from "vitest"; | ||||||
|
||||||
import { EvmProvider, MulticallNotFound } from "@zkchainhub/chain-providers/dist/src/index.js"; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
import { IPricingProvider } from "@zkchainhub/pricing"; | ||||||
import { | ||||||
BatchesInfo, | ||||||
|
@@ -15,7 +16,6 @@ import { | |||||
WETH, | ||||||
} from "@zkchainhub/shared"; | ||||||
|
||||||
import { EvmProvider, MulticallNotFound } from "../../../../chain-providers/dist/src/index.js"; | ||||||
import { | ||||||
bridgeHubAbi, | ||||||
diamondProxyAbi, | ||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we add this as env config too? maybe add it to tech debt to not forget