【问题标题】:How to get redis value for a given redis key using Nodejs + Redis如何使用 Nodejs + Redis 获取给定 redis 键的 redis 值
【发布时间】:2020-02-23 06:47:01
【问题描述】:

我正在使用 Nodejs + redis 来设置和获取键:值对。我编写了一个示例代码来设置一个键:值对,然后使用 npm redis 插件的默认自述文件获取它。

我的目标是使用任何给定的键从 redis 服务器获取价值。我已按照 npm redis 插件给出的步骤进行操作。我可以记录它,但是由于插件是异步的,所以无法找到获取同步 client.get 请求的方法。

我的代码是

var redis = require("redis"),
    client = redis.createClient();
const bluebird = require("bluebird");

bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);

const redisKey = "redis-set-key";
const redisValue = "hello-world";

client.set(redisKey, redisValue);

function getData(key) {
    return client.get(key, function(err, result) {
        console.log("1. Get key from redis - ", result.toString());
        return result.toString();
    });
}

const getRedisdata = getData(redisKey);
console.log("2. getRedisdata", getRedisdata);

结果

2. getRedisdata false
1. Get key from redis -  hello-world

我的目标是得到这样的结果

1. Get key from redis -  hello-world
2. getRedisdata hello-world

请帮我解决这个问题。

找到了解决办法,这是我解决的代码

const redis = require("redis");
const client = redis.createClient();
const bluebird = require("bluebird");

bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);

const redisKey = "redis-set-key";
const redisValue = "hello-world";
client.set(redisKey, redisValue);

async function setKey(key, value, expire = "EX", time = 300) {
    return new Promise((resolve, reject) => {
        return client.set(key, value, function(err, result) {
            if (result === null) {
                reject("set key fail promise");
            } else {
                resolve(result);
            }
        });
    });
}

async function getKey(key) {
    return new Promise((resolve, reject) => {
        return client.getAsync(key).then(function(res) {
            if (res == null) {
                reject("fail promise");
            } else {
                resolve(res);
            }
        });
    });
}

async function hashGetKey(hashKey, hashvalue) {
    return new Promise((resolve, reject) => {
        return client.hget(hashKey, hashvalue, function(err, res) {
            if (res == null) {
                reject("hash key fail promise");
            } else {
                resolve(res.toString());
            }
        });
    });
}

async function hashGetAllKey(hashKey) {
    return new Promise((resolve, reject) => {
        return client.hgetall(hashKey, function(err, res) {
            if (res == null) {
                reject("hash key all fail promise");
            } else {
                resolve(res);
            }
        });
    });
}

async function delKey(key) {
    return new Promise((resolve, reject) => {
        return client.del(key, function(err, result) {
            if (result === null) {
                reject("delete fail promise");
            } else {
                resolve(result);
            }
        });
    });
}

(async () => {
    // get single key value
    try {
        const keyData = await getKey("string key");
        console.log("Single key data:-", keyData);
    } catch (error) {
        console.log("Single key data error:-", error);
    }

    // get single hash key value
    try {
        const hashKeyData = await hashGetKey("hashkey", "hashtest 1");
        console.log("Single hash key data:-", hashKeyData);
    } catch (error) {
        console.log("Single hash key data error:-", error);
    }

    // get all hash key values
    try {
        const allHashKeyData = await hashGetAllKey("hashkey");
        console.log("All hash key data:-", allHashKeyData);
    } catch (error) {
        console.log("All hash key data error:-", error);
    }

    // delte single key
    try {
        const checkDel = await delKey("XXYYZZ!!!!");
        console.log("Check key delete:-", checkDel);
    } catch (error) {
        console.log("Check key delete error:-", error);
    }

    // set single key
    try {
        const checkSet = await setKey("XXYYZZ", "AABBCC");
        console.log("Check data setkey", checkSet);
    } catch (error) {
        console.log("Check data setkey error", error);
    }
})();

//  hget hashkey "hashtest 1"
client.hset("hashkey", "hashtest 1", "some value", redis.print);
client.hset(["hashkey", "hashtest 2", "some other value"], redis.print);

【问题讨论】:

    标签: node.js redis bluebird


    【解决方案1】:

    你没看过Redis模块的自述文件吗,它提供了另一种使用async/await方式使异步redis get进程同步的方法。

    const { promisify } = require("util");
    const getAsync = promisify(client.get).bind(client);
     
    getAsync.then(console.log).catch(console.error);
    

    【讨论】:

      猜你喜欢
      • 2014-03-10
      • 1970-01-01
      • 2012-02-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多