redis
G ># path: courses/express-node/redis.md
- **fileName**: redis
- **Created on**: 2024-06-29 04:26:01
installation:
npm i redis
sudo pacman -S redis # arch
sudo apt-install redis # debain , ubuntu
sude dnf install redis # fedora
example for using redis with node :
// adding some imports for packages
import express from "express"; // express packages
import cors from "cors"; // cors for handle multple request
import axios from "axios"; // for get request for server
import redis from "redis"; // for cache
const app = express(); // statting for express app
const DEFAULT_TIME = 60; // the time for cache before exprlorer
app.use(cors()); // use middleware for cors for handle multiple request
app.use(express.json()); // for handle response in json
const RedisClient = redis.createClient(); // start creating the client for redis
RedisClient.connect() // connect to redis and handle error
.then(async () => {
console.log("connected to redis");
RedisClient.setEx("name", 60, "yossef");
})
.catch((err) => {
console.log("err happened" + err);
client.quit();
});
app.get("/", async(_, res) => { // setting some end points
console.log("welcome form yossef");
const data = await axios.get("https://jsonplaceholder.typicode.com/photos")
res.json({data: data.data});
});
app.get("/photos", async (_, res) => { // adding the redis end point
/* handleRedisSetGetData => is function that handle the check for redis set and get the value from redis cache take too param on is the cache name and second the callback function if there is not cache in redis then do the call back function and get data and cache it in redis */
const photos = await handleRedisSetGetData("photos", async () => {
const response = await axios.get("https://jsonplaceholder.typicode.com/photos")
return response.data;
});
console.log("photos: " , photos);
return res.json(photos);
// the main function take two params one for the key and and the name for the cache in redis and second is callback function for execute it if there is no cache
const handleRedisSetGetData = (key, cb) => {
return new Promise((resolve, reject) => {
// problem in RediscClient.get()
RedisClient.get(key, async (error, result) => {
// redisClient.get if return error that mean there is no key
if (error) return reject(error);
if (result != null) {
// found the key and return the value from string to object
return resolve(JSON.parse(result));
} else {
try {
const freshData = await cb(); // call the callback function
// save the new data to redis ( name, time, data=> string)
RedisClient.setEx(key, DEFAULT_TIME, JSON.stringify(freshData),
async(err) => {
if (err) return reject(err); // check if there any error
resolve(freshData); // resovle the new data
});
} catch (cbError) {
reject(cbError);
}
}
});
});
};
app.listen(3000, () => {
console.log("Server is running on port 3000");
});
for starting using redis must run the server for redis first by redis-server
and for get in cli and using command redis-cli
for more info about the command check this page:
geeks for geeks
some info about the command for redis-cli
continue:dotfiles.md