Redis 哈希hash 类型
Redis hash 是一个 string 类型的 field 和 value 的映射表,hash 特别适合用于存储对象
基本语法:
设置值 hmset: hmset zhangsan name "张三" age 20 sex “男”
设置值 hset: hset zhangsan name "张三"
获取数据: hgetall key
删除指定数据: del key
删除全部数据: flushall
代码演示:
1.hmset
/*
https://www.npmjs.com/package/redis
*/
var redis = require("redis"),
client = redis.createClient(6379,'127.0.0.1');
//增加一条数据
client.hmset('userinfo','name','zhangsna','age','20','sex','女');
client.hgetall('userinfo',(err,result)=>{
console.log(result);
})
run code
2.hset
/*
https://www.npmjs.com/package/redis
*/
var redis = require("redis"),
client = redis.createClient(6379,'127.0.0.1');
//增加一条数据
client.hset('userinfo','name',"zhangsan");
client.hset('userinfo','age',"20");
client.hgetall('userinfo',(err,result)=>{
console.log(result);
})
run code