想通过Node.js存储在Redis中

XMe*_*Men 8 json redis key-value-store node.js

我想在Redis中存储用户的哈希/ JSON数据,并希望在用户中添加用户哈希这样的用户数据.

例如,users = {};

当用户rahul登录时users将成为.

users = {
    rahul: {
        username: 'rahul',          
    }
}
Run Code Online (Sandbox Code Playgroud)

当用户namita登录时

users = {
    rahul: {
        username: 'rahul',

    },
    namita: {
        username: 'namita',

    }
}
Run Code Online (Sandbox Code Playgroud)

在Redis中代码是做什么的?

我如何初始化密钥users并添加rahul

在这个集合,hset等中,我需要使用哪个功能?

我将如何检索users['rahul']通过Redis 的数据?

yoj*_*o87 22

存储单个hash/json的最佳解决方案可能是使用哈希命令.我也有这种" 困境 ",并且有几个 关于数据结构的问题,其中包含Redis中具有类似JSON的对象的用户.

编辑

使用node_redis模块.它由专业人员主动维护,可能是Redis最常用的node.js驱动程序.首先,您应该使用SADD命令将新的JSON对象名称添加到集合中,以便跟踪"用户"包含的项目.然后使用HMSET存储"user:rahul"对象键值哈希值.例:

// add first user
client.sadd("users", "user:rahul");
client.hmset("user:rahul", "username", "rahul", "foo", "bar");

// add second user
client.sadd("users", "user:namita");
client.hmset("user:namita", "username", "namita", "foo", "baz");
Run Code Online (Sandbox Code Playgroud)

您现在可以通过各种类型的redis hash命令访问您的用户,具体取决于您是只想检索某些哈希值还是整个单个用户对象.要获取所有用户,您可以使用SMEMBERS命令.尝试查看node_redis 自述文件示例,您应该在其中找到有关其API的更多信息.