8 Tweets Jan 31, 2023
How to cache API requests in Express
Thread ๐Ÿงต๐Ÿ‘‡
Caching is a technique that stores frequently accessed data in a temporary storage area to retrieve it when needed quickly.
It speeds up access to the data and reduces the strain on the system that would otherwise be caused by constantly retrieving the same data from its original location.
Let's show how we can implement caching in the NodeJs Express API app.
We will use a package called "apicache" for this.
First, install the package by running the following command in your terminal.
`npm i apicache`
The code below explains how to use the package.
First, we require the package.
In the next step, we create a variable called cache, which holds the configured apicache middleware.
We use the express function app.use to use this middleware.
This will apply the cache for every route.
You can also use different per route cache.
We pass the actual number of minutes we want to cache the results.
In our case, it's 2 minutes.
Duration is in the following format "[length][unit]", as in "10 minutes" or "1 day"
Hope you enjoyed this thread.
If you found this thread useful, follow @Rapid_API ๐Ÿ™๐Ÿ’™

Loading suggestions...