目录
DEL 删除KEY
EXPIRE 设置过期时间
PERSIST 移除给定KEY的生存时间
EXPIREAT 设置过期时间-时间戳
TTL 获取剩余过期时间
PTTL 获取剩余过期时间
RENAME 重命名KEY
RENAMENX 重命名KEY
EXISTS 判断KEY是否存在
KEYS 按模式匹配KEY
SORT 排序
SORT key asc|desc 数字排序
SORT key alpha 字符串排序
sort key limit start size 限制返回数量
DUMP 序列化给定 KEY
RESTORE 反序列化给定 KEY
OBJECT 查看KEY内部存储信息
TYPE 查值的类型
DEL key [key ...]:删除给定的一个或多个 key
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> set name lisi # 初始化key
OK
127.0.0.1:6379> set age 22
OK
(integer) 1
127.0.0.1:6379> lpush mylist apple banana orange pear
(integer) 4
127.0.0.1:6379> del name age mylist address # 删除key
(integer) 3 # 删除成功的key数量(不存在的key忽略)
时间复杂度:
返回:
EXPIRE key seconds:为给定 key 设置生存时间,当 key 过期时(生存时间为 0 ),它会被自动删除
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> set age 20
OK
127.0.0.1:6379> expire age 20
(integer) 1
127.0.0.1:6379> get age # 查看age值
"21"
127.0.0.1:6379> get age # 过20秒再查看
(nil)
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> set name lisi
OK
127.0.0.1:6379> expire name 20 # 设置过期时间为20秒
(integer) 1
127.0.0.1:6379> rename name name2 # 重命名key为name2
OK
127.0.0.1:6379> keys * # 查看库中有多少key
1) "name2"
127.0.0.1:6379> get name2 # 查看name2信息
"lisi"
127.0.0.1:6379> get name2 # 过20秒再获取name2信息
(nil)
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> set name lisi
OK
127.0.0.1:6379> expire name 20 # 设置过期时期
(integer) 1
127.0.0.1:6379> persist name # 对key为name执行persist命令
(integer) 1
127.0.0.1:6379> get name # 获取name信息
"lisi"
127.0.0.1:6379> get name # 过20秒再获取name信息
"lisi"
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> set name lisi
OK
127.0.0.1:6379> expire name 50
(integer) 1
127.0.0.1:6379> expire name 20
(integer) 1
127.0.0.1:6379> expire name 5
(integer) 1
127.0.0.1:6379> get name
"lisi"
127.0.0.1:6379> get name # 过5秒后查name信息
(nil)
时间复杂订O(1)。
返回:
PERSIST key:将这个 key 从(带生存时间 key )的过期时间删除
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> set name lisi
OK
127.0.0.1:6379> expire name 10
(integer) 1
127.0.0.1:6379> persist name
(integer) 1
127.0.0.1:6379> ttl name
(integer) -1 # name的过期时间已清除,所以返回-1
127.0.0.1:6379> persist name
(integer) 0
127.0.0.1:6379> persist name1
(integer) 0
时间复杂度O(1)。
返回:
EXPIREAT key timestamp:操作与EXPIRE相似
TTL key:以秒为单位,返回给定 key 的剩余生存时间(TTL, time to live)
127.0.0.1:6379> set name lisi ex 20
OK
127.0.0.1:6379> ttl name
(integer) 18
127.0.0.1:6379> ttl name
(integer) 6
127.0.0.1:6379> ttl name
(integer) -2
127.0.0.1:6379> get name
(nil)
时间复杂度O(1)
返回值:
PTTL key:操作类似于TTL
RENAME key newkey:将 key 改名为 newkey
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> rename name address # key不存在
(error) ERR no such key
127.0.0.1:6379> set name lisi
OK
127.0.0.1:6379> rename name name2 # 执行rename命令
OK
127.0.0.1:6379> set address guiyang
OK
127.0.0.1:6379> keys *
1) "address"
2) "name2"
127.0.0.1:6379> rename name2 address # 执行rename命令
OK
127.0.0.1:6379> keys * # newkey已存在,直接覆盖,只剩下address一个key
1) "address"
127.0.0.1:6379> get address
"lisi"
时间复杂度O(1)。
返回值:
RENAMENX key newkey:操作与RENAME相似
EXISTS key:检查给定 key 是否存在
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> exists name
(integer) 0
127.0.0.1:6379> set name lisi
OK
127.0.0.1:6379> exists name
(integer) 1
时间复杂度O(1)。
返回:
KEYS pattern:查找所有符合给定模式 pattern 的 key
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> set hello 1
OK
127.0.0.1:6379> set hallo 2
OK
127.0.0.1:6379> set hxllo 3
OK
127.0.0.1:6379> set hillo 4
OK
127.0.0.1:6379> set habcdello 5
OK
127.0.0.1:6379> keys *
1) "habcdello"
2) "hallo"
3) "hello"
4) "hillo"
5) "hxllo"
127.0.0.1:6379> keys h?llo
1) "hallo"
2) "hello"
3) "hillo"
4) "hxllo"
127.0.0.1:6379> keys h*llo
1) "habcdello"
2) "hallo"
3) "hello"
4) "hillo"
5) "hxllo"
127.0.0.1:6379> keys h[ae]llo
1) "hallo"
2) "hello"
时间复杂度为O(N), N 为数据库中 key 的数量
返回:
SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC | DESC] [ALPHA] [STORE destination]
127.0.0.1:6379> lpush mylist 2 4 3 1
(integer) 4
127.0.0.1:6379> sort mylist # 默认为 asc
1) "1"
2) "2"
3) "3"
4) "4"
127.0.0.1:6379> sort mylist desc
1) "4"
2) "3"
3) "2"
4) "1"
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> lpush mylist a g b e j
(integer) 5
127.0.0.1:6379> sort mylist
(error) ERR One or more scores can't be converted into double # 字符串排序报错
127.0.0.1:6379> sort mylist alpha # 添加alpha进行对字符串排序
1) "a"
2) "b"
3) "e"
4) "g"
5) "j"
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> lpush mylist a g b e j
(integer) 5
127.0.0.1:6379> sort mylist asc alpha limit 0 2
1) "a"
2) "b"
DUMP key:序列化给定 key ,并返回被序列化的值,使用 RESTORE 命令可以将这个值反序列化为 Redis 键
127.0.0.1:6379> flushdb # 清空库
OK
127.0.0.1:6379> set name lisi
OK
127.0.0.1:6379> dump name # dump存在的key
"\x00\x04lisi\t\x00\xd2p\xcc\xfe6\x8d\xd9\x15"
127.0.0.1:6379> dump age # dump不存在的key
(nil)
时间复杂度:
返回值:
RESTORE key ttl serialized-value:反序列化给定的序列化值,并将它和给定的 key 关联
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> set name lisi
OK
127.0.0.1:6379> dump name # 序列化
"\x00\x04lisi\t\x00\xd2p\xcc\xfe6\x8d\xd9\x15"
127.0.0.1:6379> restore name2 0 "\x00\x04lisi\t\x00\xd2p\xcc\xfe6\x8d\xd9\x15" # 反序列化
OK
127.0.0.1:6379> keys *
1) "name2"
2) "name"
127.0.0.1:6379> get name
"lisi"
127.0.0.1:6379> get name2
"lisi"
127.0.0.1:6379> restore name2 0 "\x00\x04lisi\t\x00\xd2p\xcc\xfe6"
(error) BUSYKEY Target key name already exists. # 序列化值不完整,报错
时间复杂度
返回值:
OBJECT subcommand [arguments [arguments]]:允许从内部察看给定 key 的 Redis 对象
子命令:
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> set name lisi
OK
127.0.0.1:6379> set age 20
OK
127.0.0.1:6379> set code 123456789901234567890
OK
127.0.0.1:6379> lpush mylist 1 2 3 4 5 b c d e f g h j
(integer) 13
127.0.0.1:6379> hset myhash name lisi age 20 address 'guiyang'
(integer) 3
127.0.0.1:6379> sadd myset 1 2 3 a b c d e f hello world
(integer) 11
127.0.0.1:6379> zadd mysset 1 a 3 c 2 d 5 5 6 1a
(integer) 5
127.0.0.1:6379> object encoding name
"embstr" # 字符串
127.0.0.1:6379> object encoding age
"int" # 数字
127.0.0.1:6379> object encoding code
"embstr" # 大的数字被编码为字符串
127.0.0.1:6379> object encoding mylist
"quicklist" # 列表
127.0.0.1:6379> object encoding myhash
"ziplist" # 集合
127.0.0.1:6379> object encoding myset
"hashtable" # hash
127.0.0.1:6379> object encoding mysset
"ziplist" # 有序集合
时间复杂度为O(1)。
返回值:
TYPE key:返回 key 所储存的值的类型。
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> set name lisi
OK
127.0.0.1:6379> set age 20
OK
127.0.0.1:6379> set code 123456789901234567890
OK
127.0.0.1:6379> lpush mylist 1 2 3 4 5 b c d e f g h j
(integer) 13
127.0.0.1:6379> hset myhash name lisi age 20 address 'guiyang'
(integer) 3
127.0.0.1:6379> sadd myset 1 2 3 a b c d e f hello world
(integer) 11
127.0.0.1:6379> zadd mysset 1 a 3 c 2 d 5 5 6 1a
(integer) 5
127.0.0.1:6379> type address
none
127.0.0.1:6379> type name
string # 字符串
127.0.0.1:6379> type age
string # 字符串
127.0.0.1:6379> type code
string # 字符串
127.0.0.1:6379> type mylist
list # 列表
127.0.0.1:6379> type myhash
hash # 哈希
127.0.0.1:6379> type myset
set # 集合
127.0.0.1:6379> type mysset
zset # 有序集合
时间复杂度O(1)。
返回:
PS:还有 SCAN迭代键、RANDOMKEY随机返回键、MIGRATE原子键跨实例移动、MOVE库之间移动等命令几乎用不上,有需要的同学请移步到官网查阅https://redis.io。