这是我参与11月更文挑战的第10天,活动详情查看:2021最后一次更文挑战
string 字符串
| 命令 | 行为 | 返回值 | 使用示例(略去回调函数) | |
|---|---|---|---|---|
| set | 设置存储在给定键中的值 | OK | set('key', 'value') | |
| get | 获取存储在给定键中的值 | value/null | get('key') | |
| del | 删除存储在给定键中的值(任意类型) | 1/0 | del('key') | |
| incrby | 将键存储的值加上整数increment | incrby('key', increment) | ||
| decrby | 将键存储的值减去整数increment | decrby('key', increment) | ||
| incrbyfloat | 将键存储的值加上浮点数increment | incrbyfloat('key', increment) | ||
| append | 将值value追加到给定键当前存储值的末尾 | append('key', 'new-value') | ||
| getrange | 获取指定键的index范围内的所有字符组成的子串 | getrange('key', 'start-index', 'end-index') | ||
| setrange | 将指定键值从指定偏移量开始的子串设为指定值 | setrange('key', 'offset', 'new-string') |
String字符串案例
const redis = require('redis');
const client = redis.createClient( 6379, '127.0.0.1');
client.set('hello word', 5, function(err, data) {
client.incr('hello word', function(err,data) {
client.get('hello word', function(err,data) {
console.log(data);
})
})
})
Redis其他常用命令汇总
| 命令 keys命令组 | 行为 | 返回值 | 使用示例(略去回调函数) | |
|---|---|---|---|---|
| del | 删除一个(或多个)keys | 被删除的keys的数量 | del('key1'[, 'key2', ...]) | |
| exists | 查询一个key是否存在 | 1/0 | exists('key') | |
| expire | 设置一个key的过期的秒数 | 1/0 | expire('key', seconds) | |
| pexpire | 设置一个key的过期的毫秒数 | 1/0 | pexpire('key', milliseconds) | |
| expireat | 设置一个UNIX时间戳的过期时间 | 1/0 | expireat('key', timestamp) | |
| pexpireat | 设置一个UNIX时间戳的过期时间(毫秒) | 1/0 | pexpireat('key', milliseconds-timestamp) | |
| persist | 移除key的过期时间 | 1/0 | persist('key') | |
| sort | 对队列、集合、有序集合排序 排序完成的队列等 | sort('key'[, pattern, limit offset count]) | ||
| flushdb | 清空当前数据库 |
事务(multi命令):
- 批量执行所有的命令,并统一返回结果
const redis = require('redis');
const client = redis.createClient( 6379, '127.0.0.1');
client.multi()
.set('xiao','xing')
.get('xiao')
.exec(function(err,replies) {
console.log(replies); // [ 'OK', 'xing' ]
})