node.js redis.get(key,callback) 即使数据和密钥都存在于redis db中,也会随机返回null

问题描述

我在 node.js 中陷入了一个非常不正常的情况。我正在使用 ioredis npm 模块连接 redis 数据库并将密钥保存在 redis db 1 中。 在我的应用程序中,多个 api 首先从一个键中获取数据,然后从第一个键返回的另一个键中获取数据。 在这种情况下,当节点实例上出现 2 个或更多 api 命中时。 node js在redis数据库中存在的key甚至key上随机给出null。 我还尝试使用替代方法添加 settimeout 以在一段时间后获取密钥,但仍然没有成功。请检查下面的代码

const userDataKey: any = await getUserDataFromredis(`${UserConfig.USER_LOGIN_REdis_PREFIX}${decodedData.id}_${decodedData.uuid}`)
            
const userData: any = await getUserDataFromredis(`${userDataKey}`)


export const getUserDataFromredis = async (key: string) => {
    return new Promise((resolve,reject) => {
        redisConnection.select(UserConfig.REdis_DB,(err) => {
            if (err) reject(err)
            console.log("this is the key : ",key);
            redisConnection.get(key,(error,data: any) => {
                if (err) reject(error)
                console.log("this is data : ",data);
                if (_.isNull(data)) {
                    console.log("NULL CONDITION MATCHED:",data);
                    setTimeout(() => {
                        redisConnection.get(key,data: any) => {
                            if (err) reject(error)

                            console.log(`this is key and data if first time found NULL:${key},${data}`);

                            resolve(JSON.parse(data));
                        });
                    },1000);
                } else {
                    resolve(JSON.parse(data));
                }
            });
        })
    })
}

你能帮我看看应该是什么原因吗,我有时甚至在redis数据库中存在key也会得到null。

this is data :  "rm_user_92"
this is data :  "rm_user_92"
this is data :  {"id":92,"name":"VinayRM1","email":"vinayrm20@yopmail.com","password":"$2b$10$0h5i.TiB6AENQF1c7XB1DOrBh/yNcKW9m3H.CM77wz.59IVfOjrEe","phone":"943434343434","is_active":"1","image":null,"user_type":2,"created_at":"2021-01-11T11:56:44.000Z","updated_at":"2021-01-11T06:26:44.046Z","added_by":1,"company":null,"country_code":91,"password_updated_at":"2021-01-29T19:14:01.000Z","device_token":"asdfdsafsadfasfsaf"}
Executing (default): Select U.*,A."id" as rm_id,A."name" as rm_name,A."email" as rm_email,A."phone" as rm_phone
                              FROM "user" U
                              LEFT JOIN "admin" A ON U."rm_id" = A."id" where U."id" = 172
Executing (default): SELECT * FROM "augment_view_analytics" WHERE import_date='2021-11-08' and user_id=172 and file_type=1;
Executing (default): SELECT * FROM "augment_view_analytics" where user_id = 172 and file_type=1 and import_date <= '2021-11-08' ORDER BY import_date DESC LIMIT 1;
this is data :  null
NULL CONDITION MATCHED: null

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)