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

How do I check if a session id already exists in redis

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

请求对象在每次调用时都是新的,但我知道 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);
     }

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");
    } });

答案是: 会话 ID 已经存在