Node.js - clent.hgetall 和 client.hmset 在 Redis 中


Redis 命令通常將輸入作為單個字串或字串陣列作為引數,而回復則以單個字串或字串陣列的形式傳送回。然而,在處理雜湊值時,有幾個例外。

  • Node.js 中的 client.hgetall() 函式 - Redis 返回一個以雜湊鍵為鍵的物件。字串將以字串或緩衝區形式返回,具體取決於設定。

  • client.hmset() 函式向 Redis 提供雜湊引數。

語法

client.hmset()

client.hmset(hash, key1, val1, ...keyN, valN, [callback])

client.hgetall()

client.hgetall(hash, callback)

示例 1

建立一個名為 "hmset.js" 的檔案,並複製以下程式碼段。建立檔案後,使用命令 "node hmset" 執行此程式碼,如下面的示例所示 −

// client.hmset Demo Example

// Importing the redis & assert module
const redis = require("redis");
const client = redis.createClient();

client.hmset("hi", "foo", "bar", "hello", "world");

輸出

There will be no response because the values are stored in Redis DB.

示例 2

// client.hgetall() Demo Example

// Importing the redis & assert module
const redis = require("redis");
const client = redis.createClient();

client.hmset("hi", "foo", "bar", "hello", "world");

client.hgetall("hi", function(err, value) {
   console.log(value.foo);
   console.log(value.hello);
});

輸出

C:\home
ode>> node hgetAll.js bar world

更新於: 17-1-2022

2K+ 瀏覽量

開啟你的 職業

完成課程,獲得認證

開始
廣告
© . All rights reserved.