-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# 🤖 Linear Closes ZKS-208 Closes ZKS-199 ## Description - Added caching of 60s default to `ecosystem` and `zkchain` endpoints - Moved core contract addresses from hardcoded config to env variables We decided to move forward with `NodeCache` since `cacheable` by nature uses Promises so hit the cache, this causes some problems when trying to override `res.json` hook on express since `res.json` is considered sync. For next steps we will: - keep using NodeCache as Highlevel api caching - keep using `cacheable` for modules advanced caching - analyze with more time if we can effectively use `cacheable` for the cacheMiddleware
- Loading branch information
Showing
9 changed files
with
91 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
// Call the original json function with the response body | ||
return originalJson(body); | ||
}; | ||
|
||
next(); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.