如何检查redis中是否已存在会话ID

时间:2022-12-01 22:53:08

I have the following code and I am not sure how to check if the session key exists already, because I do not want to create another redis session if it already exists.

我有以下代码,我不知道如何检查会话密钥是否已经存在,因为我不想创建另一个redis会话(如果它已经存在)。

The request object is new with every call but I know that the event.sender.id is the same on each request.

每次调用时请求对象都是新的,但我知道每个请求的event.sender.id都是相同的。

     // If not set then create the session object
     if (!req.session.key) {
       console.log('Set session variable');
       req.session.key = event.sender.id;
       console.log('*** SESSION CREATED WITH ' + event.sender.id);
     }

1 个解决方案

#1


1  

Server.js

const redis = require("redis");
const client = redis.createClient(); 
const hostname = '127.0.0.1'; 
const port = 6379;


client.on("error", function (err) {
    console.log("Error " + err); });

var session_id_key = event.sender.id ; //your id

client.exists("key",session_id_key, function (err, replies) {

    if(!err && replies===1){

        console.log("THE SESSION ID ALREADY PRESENT ");
    }else{
        console.log("THE SESSION ID DOES NOT PRESENT");
    } });

The Answer is : THE SESSION ID ALREADY PRESENT

答案是:会议ID已经存在

#1


1  

Server.js

const redis = require("redis");
const client = redis.createClient(); 
const hostname = '127.0.0.1'; 
const port = 6379;


client.on("error", function (err) {
    console.log("Error " + err); });

var session_id_key = event.sender.id ; //your id

client.exists("key",session_id_key, function (err, replies) {

    if(!err && replies===1){

        console.log("THE SESSION ID ALREADY PRESENT ");
    }else{
        console.log("THE SESSION ID DOES NOT PRESENT");
    } });

The Answer is : THE SESSION ID ALREADY PRESENT

答案是:会议ID已经存在