    const redis = require("redis");
    const redis_help ={}
    const ID_KEY = "ID_KEY"
    const redis_config = {
        host: '127.0.0.1', // 替换为你的 Redis 服务器 IP 地址
        port: 6379  // 替换为你的 Redis 服务器端口号，例如 6379（如果未更改默认设置）
    }
    const client = redis.createClient();

    client.on('error', (err) => {
        console.error('Redis Client Error', err);
    });
  
    redis_help.isInit = false
    redis_help.getNewId = async function(){
        let id = await redis_help.getKeyValue(ID_KEY)
        if(id==null){
            id = "0"
        }else{
            id = `${parseInt(id)+1}`
        }
        await redis_help.setKeyValue(ID_KEY,id+"")
        return id
    }

    redis_help.setKeyValue = function(key, value) {
        return client.set(key, value+"").then((status)=>{
            if(status=="OK"){
                // console.log("写入成功！",status,key,value)
            }else{
                console.error("redis_help.setKeyValue 失败",status,key,value)
            }
        }).catch((e)=>{
            console.error("redis_help.setKeyValue 失败",e,key,value)
        });
    }


    redis_help.getKeyValue = function(key) {
        return client.get(key).then((value)=>{
            if(value!=undefined&&value!=null){
                // console.log("获取成功",value)
            }else{
                console.error("redis_help.getKeyValue 失败",value,key)
                return null
            }
            return value
        }).catch((e)=>{
            console.error("redis_help.getKeyValue error 失败",e,key)
            return null
        });
    }

    redis_help.setKeyWithExpiry = function(key, value, expiryInSeconds) {
        return new Promise((resolve, reject) => {
            client.setex(key, expiryInSeconds, value, (err, reply) => {
                if (err) reject(err);
                resolve(reply);
            });
        });
    }


    redis_help.deleteKey = function(key) {
        return client.del(key).then(()=>{}).catch(()=>{});
    }

    redis_help.connect = function(call_back){
        if(redis_help.isInit){
            call_back(true)
        }else{
            client.connect(redis_config).then(async () => {
                console.log('Connected to Redis');
                // client.set('name', 'zhangsan',redis.print)
                redis_help.isInit = true
                // client.get("name").then((res)=>{
                //     console.log("res:",res)
                // }).catch((e)=>{
                //     console.log("res e:",e)
                // })
                
                // 示例操作
                // await setKeyValue('myKey', 'myValue');
                // const value = await getKeyValue('myKey');
                // console.log('Value:', value);
                // await deleteKey('myKey');
                // const deletedValue = await getKeyValue('myKey');
                // console.log('Deleted Value:', deletedValue); // 应该输出 null 或 undefined
        
                // 关闭连接
                // await client.quit();
                call_back(true)
            }).catch((err) => {
                call_back(false)
                console.error('Failed to connect to Redis', err);
            }
        );
        }
   
    }

    // (async () => {
    //     try {
    //       await setKeyValue('user:1000:name', 'John Doe');
    //       const userName = await getKeyValue('user:1000:name');
    //       console.log('User Name:', userName);
      
    //       await setKeyWithExpiry('tempKey', 'tempValue', 5); // 5 秒后过期
    //       setTimeout(async () => {
    //         const expiredValue = await getKeyValue('tempKey');
    //         console.log('Expired Value:', expiredValue); // 应该输出 null 或 undefined
    //       }, 6000); // 6 秒后检查
      
    //       await deleteKey('user:1000:name');
    //       const deletedUserName = await getKeyValue('user:1000:name');
    //       console.log('Deleted User Name:', deletedUserName); // 应该输出 null 或 undefined
    //     } catch (err) {
    //       console.error('Error:', err);
    //     }
    //   })();

    module.exports = redis_help;