gpt4 book ai didi

node.js - 向REDIS中插入数据(node.js + redis)

转载 作者:可可西里 更新时间:2023-11-01 11:22:27 26 4
gpt4 key购买 nike

如何,我可以像这样插入(存储)数据(node.js + redis):

var timestamp = new Date().getTime();

client.hmset('room:'+room, {
'enabled' : true,
timestamp : {
'g1' : 0,
'g2' : 0

}
});

我可以为 g1 或 g2 增加多少?

附言当以这种方式插入时间戳时,redis-cli 显示时间戳而不是 UNIX 时间

最佳答案

您正在寻找 HMGET 的组合和 HMSET .根据the docs :

HMGET key field [field ...]

Returns the values associated with the specified fields in the hash stored at key.

For every field that does not exist in the hash, a nil value is returned. Because a non-existing keys are treated as empty hashes, running HMGET against a non-existing key will return a list of nil values.

HMSET key field value [field value ...]

Sets the specified fields to their respective values in the hash stored at key. This command overwrites any existing fields in the hash. If key does not exist, a new key holding a hash is created.

然后,您要做的是从 has 中检索您的值,对其执行任何看起来合适的操作,然后保存以前的值。

另一个可能更好的解决方案是使用 HINCRBY .如果您坚持使用时间戳,则可以在不执行获取操作的情况下增加字段:

HINCRBY key field increment

Increments the number stored at field in the hash stored at key by increment. If key does not exist, a new key holding a hash is created. If field does not exist the value is set to 0 before the operation is performed.

The range of values supported by HINCRBY is limited to 64 bit signed integers.

不过,您可能需要重组哈希才能使其正常工作,除非有一种方法可以深入到您的 g1/g2 字段(stackoverflow 社区,如果您知道方法,请随时编辑此答案或对其进行评论) .像这样的结构应该可以工作:

{
enabled : true,
timestamp_g1 : 0,
timestamp_g2 : 0
}

关于node.js - 向REDIS中插入数据(node.js + redis),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18413223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com