https://github.com/syed007hassan/node-redis
API caching in NodeJS using Redis database
https://github.com/syed007hassan/node-redis
api cache express redis-client
Last synced: 5 months ago
JSON representation
API caching in NodeJS using Redis database
- Host: GitHub
- URL: https://github.com/syed007hassan/node-redis
- Owner: Syed007Hassan
- Created: 2023-09-01T20:06:39.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-09-02T13:17:19.000Z (about 2 years ago)
- Last Synced: 2025-02-17T21:46:39.425Z (8 months ago)
- Topics: api, cache, express, redis-client
- Language: JavaScript
- Homepage:
- Size: 15.6 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Node-Redis-Cache
It is a NodeJS app which is using Redis to demonstrate caching. It consumes [Harry Potter](https://hp-api.onrender.com/api/characters) API for the demo purpose.
A middleware function is used that stores the data in Redis using a key-value pair.```
// cache data in redis
const cacheData = async (req, res, next) => {
try {
const characterId = req.params.id;
let redisKey = "hogwarts-characters";
if (characterId) {
redisKey = `hogwarts-character-${req.params.id}`;
}
const cacheResults = await redisClient.get(redisKey);
if (cacheResults) {
res.send({
fromCache: true,
data: JSON.parse(cacheResults),
});
} else {
next();
}
} catch (error) {
console.error(error);
res.status(404);
}
}```
## Response without caching
**Response time: 566 ms**
## Response with caching
**Response time: 17 ms**