命令 | 含义 | 时间复杂度 |
---|---|---|
keys | 查找全部符合给定模式 pattern 的 key | O(N), N 为数据库中 key 的数量 |
dbsize | 计算key的总数 | O(1) |
exists | 检查key是否存在 | O(1) |
del | 删除指定的key-value | O(1) |
expire、ttl、persist | 设置、查看、去掉key的过时时间 | O(1) |
type | 查看key的类型 | O(1) |
当key较多时,命令执行时间较长,会形成阻塞,慎用该命令。
127.0.0.1:6379> mset hello world hehe haha php good phe his OK 127.0.0.1:6379> keys * 1) "hello" 2) "phe" 3) "php" 4) "hehe" 127.0.0.1:6379> keys he* 1) "hello" 2) "hehe" 127.0.0.1:6379> keys he[h-l]* 1) "hello" 2) "hehe" 127.0.0.1:6379> keys ph? 1) "phe" 2) "php" 127.0.0.1:6379> dbsize (integer) 4
127.0.0.1:6379> exists hello (integer) 1 127.0.0.1:6379> del hello php (integer) 2 127.0.0.1:6379> exists hello (integer) 0 127.0.0.1:6379> get hello (nil)
大于等于0时,表示剩余过时秒数
-1 表示key存在,而且没有过时时间
-2 表示key已经不存在了
127.0.0.1:6379> set hello world OK 127.0.0.1:6379> expire hello 20 (integer) 1 127.0.0.1:6379> ttl hello (integer) 12 127.0.0.1:6379> get hello "world" 127.0.0.1:6379> ttl hello (integer) -2 127.0.0.1:6379> get hello (nil) 127.0.0.1:6379> set hello world OK 127.0.0.1:6379> expire hello 20 (integer) 1 127.0.0.1:6379> ttl hello (integer) 14 127.0.0.1:6379> persist hello (integer) 1 127.0.0.1:6379> ttl hello (integer) -1 127.0.0.1:6379> get hello "world"
string
hash
list
set
zset
none
127.0.0.1:6379> set a 1 OK 127.0.0.1:6379> type a string 127.0.0.1:6379> sadd myset 1 2 3 (integer) 3 127.0.0.1:6379> type myset set
Redis学习笔记 - 数据类型与API(1)Key
Redis学习笔记 - 数据类型与API(2)String
Redis学习笔记 - 数据类型与API(3)List
Redis学习笔记 - 数据类型与API(4)Set
Redis学习笔记 - 数据类型与API(5)Sorted Set
Redis学习笔记 - 数据类型与API(6)Hashphp