-1
function getCache(key) {
cache.get(key, (err, value) => {
if(err){
return ("err");
}else{
return ("" + value);
}
});
};
console.log(getCache('fatorial:7'));
I’m trying to get key value saved in redis, but it’s returning Undefined...
I don’t know what else to do... sorry.
The complete code is here:
const express = require("express");
const bodyparser = require("body-parser");
const redis = require("redis");
const fatorialFuncao = require("./fatorial");
const PORT = process.env.PORT || 8080;
const REDIS_PORT = process.env.PORT || 6379;
const cache = redis.createClient(REDIS_PORT);
const app = express();
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(bodyparser.urlencoded({extended: false}));
app.use(bodyparser.json());
cache.on("connect", () => {
console.log("Redis is ready");
});
cache.on("error", (e) => {
console.log("Redis error", e);
});
function getCache(key) {
cache.get(key, (err, value) => {
if(err){
return ("err");
}else{
return ("" + value);
}
});
};
console.log(getCache('fatorial:7'));
function setCache(key, value) {
cache.set(key, value, 'EX', 10000, (err, value) => {
if(err){
console.log(err);
}else{
console.log("Os parâmetros foram cadastrados no cash com sucesso");
}
});
};
app.get("/", (req, res) => {
res.render('index');
});
app.post("/calculofatorial", (req, res) => {
let id = req.body.s1;
let valueId = getCache(`fatorial:${id}`);
if(valueId){
res.send(`Obs.: Essa cálculo foi recuperado do cache`);
}else{
if(parseInt(parseInt(req.body.s1)) > 0) {
let calculation = fatorialFuncao(parseInt(req.body.s1))
setCache(`fatorial:${id}`, calculation);
res.send(`O fatorial de ${id} é ${calculation}. Obs.: Essa operação não estava armazenada em cache`);
} else {
let msgErro = "digite um valor válido"
res.send(msgErro);
}
}
});
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
});
fucking... I’m gonna cry.... fucking... friend... I’m really happy... I spent 12 hours trying to understand this... dude... we got it!!!!! thank you... my God... I’m crying from happiness.... Putz. z...
– Glarcan
god bless you.
– Glarcan
@Glarcan if solved your problem please mark the answer as correct. Thank you!
– Guilherme Nascimento