Client.hsetnx(key, field, value)
仅当 field
不存在时,才将存储在 key
的哈希中的指定 field
设置为 value
。如果 key
不存在,则会创建一个存储哈希的新键。如果 field
已存在,此操作无效。
参数
参数 | 类型 | 描述 |
---|---|---|
key | string | 存储要设置字段的哈希的键。 |
field | string | 要在哈希中设置的字段。 |
value | string | 用于设置字段的值。 |
返回值
类型 | 解析为 | 拒绝时 |
---|---|---|
Promise<boolean> | 成功时,如果 field 是哈希中的新字段且值已设置,则 promise 解析为 1 ;如果 field 已存在于哈希中且未执行任何操作,则解析为 0 。 |
示例
import redis from 'k6/experimental/redis';
// Instantiate a new redis client
const redisClient = new redis.Client('redis://:6379');
export default async function () {
await redisClient.hsetnx('myhash', 'myfield', 'myvalue');
await redisClient.hsetnx('myhash', 'myotherfield', 'myothervalue');
const set = await redisClient.hsetnx('myhash', 'myfield', 'mynewvalue');
if (set === true) {
throw new Error('hsetnx should have failed on existing field');
}
}