这是我参与11月更文挑战的第9天,活动详情查看:2021最后一次更文挑战
hash 散列
| 命令 | 行为 | 返回值 | 使用示例(略去回调函数) | |
|---|---|---|---|---|
| hset | 在散列里面关联起给定的键值对 | 1(新增)/0(更新) | hset('hash-key', 'sub-key', 'value') (不支持数组、字符串) | |
| hget 获取指定散列键的值 | hget('hash-key', 'sub-key') | |||
| hgetall | 获取散列包含的键值对 | json | hgetall('hash-key') | |
| hdel | 如果给定键存在于散列里面,则移除这个键 | hdel('hash-key', 'sub-key') | ||
| hmset | 为散列里面的一个或多个键设置值 OK | hmset('hash-key', obj) | ||
| hmget | 从散列里面获取一个或多个键的值 array | hmget('hash-key', array) | ||
| hlen | 返回散列包含的键值对数量 | hlen('hash-key') | ||
| hexists | 检查给定键是否在散列中 | 1/0 | hexists('hash-key', 'sub-key') | |
| hkeys | 获取散列包含的所有键 | array | hkeys('hash-key') | |
| hvals | 获取散列包含的所有值 | array | hvals('hash-key') | |
| hincrby | 将存储的键值以指定增量增加 | 返回增长后的值 | hincrby('hash-key', 'sub-key', increment) (注:假如当前value不为为字符串,则会无输出,程序停止在此处) | |
| hincrbyfloat | 将存储的键值以指定浮点数增加 |
案例
// hash 散列
const redis = require('redis');
const client = redis.createClient( 6379, '127.0.0.1');
client.send_command('lset',['list',0,1], function(err,data) {
console.log(data); // OK
})
client.send_command('lpush',['list', 'one'], function(err, data) {
console.log(dada); //OK
})
// 使用命令行获取对应的索引下的结果
// lindex list 0 'one'
// lindex list 1 '1'
// lindex list 2 'xxx'
// lindex list 3 'xxx'
zset 有序集合
| 命令 | 行为 | 返回值 | 使用示例(略去回调函数) | |
|---|---|---|---|---|
| zadd | 将一个带有给定分支的成员添加到有序集合中 | zadd('zset-key', score, 'key') (score为int) | ||
| zrange | 根据元素在有序排列中的位置,从中取出元素 | |||
| zrangebyscore | 获取有序集合在给定分值范围内的所有元素 | |||
| zrem | 如果给定成员存在于有序集合,则移除 | |||
| zcard | 获取一个有序集合中的成员数量 | 有序集的元素个数 | zcard('key') |
案例
// zset 有序集合
const redis = require('redis');
const client = redis.createClient( 6379, '127.0.0.1');
client.zadd(['zdb',0,'mysql', 1,'mongo',2,'redis'], function(err, data) {
console.log(data); // 3
})