目录
3)expire设置key的过期时间、ttl查询key的过期时间
一、通配符匹配key
redis作为键值对类型的数据库,键也就是key,那么查询key有哪些方式呢??
首先需要明确的一点就是key是不会重复,而且redis的key默认是字符串类型
场景 | 命令(通配符) | 说明 |
---|---|---|
匹配所有的键 | * 可以匹配任意个字符 keys *会查询所有的key eg:h*llo 可以匹配 hllo、hello、heabsdllo | 生产环境慎用 |
匹配前缀 | user:* 可以匹配以user:开头的所有key | 查找以什么开头 |
匹配任意单个字符 | ? he?llo 可以匹配任意单个字符 eg:keys order:2023-??-?? | 匹配日期格式键 |
匹配固定的字符 | h[ac]llo 可以匹配hallo、hcllo | 匹配固定字符 |
匹配除了 | h[^ac]llo 除了hallo、hcllo | 排除的情况 |
匹配某个范围 | h[a-c]llo 可以匹配hallo、hbllo、hcllo | 匹配某个范围 |
二、key的常用命令
1)exists 判断某个key是否存在
返回值是存在的key的个数,exists 后面可以跟着多个key
127.0.0.1:6379> exists hallo
(integer) 1
127.0.0.1:6379> exists key
(integer) 0
2)删除key
127.0.0.1:6379> del hallo
(integer) 1
127.0.0.1:6379> exists hallo
(integer) 0可以批量删除多个key
del key key1 key2
返回值是成功删除key的个数
3)expire设置key的过期时间、ttl查询key的过期时间
127.0.0.1:6379> expire hcllo 10
(integer) 1设置成功返回值是1,设置失败的返回值是0
127.0.0.1:6379> ttl hcllo
(integer) 5
然后可以使用ttl来观察key的过期时间
4)type可以获取key对应的value的类型
127.0.0.1:6379> set key1 hello
OK
127.0.0.1:6379> lpush key2 e1 e2 e3
(integer) 3
127.0.0.1:6379> sadd key3 n1 n2 n3 n4
(integer) 4
127.0.0.1:6379> type key1
string
127.0.0.1:6379> type key2
list
127.0.0.1:6379> type key3
set