Redis系列-key相关主要操作函数 . 这篇blog主要总结下,redis中跟key相关的常用函数
1)keys
语法:keys pattern
解释:查找所有匹配指定模式pattern的key
[plain] view plaincopyprint? 01.[root@xsf001 ~]# redis-cli 02.redis 127.0.0.1:6379> keys * #所有key 03. 1) "score" 04. 2) "stu" 05. 3) "score1" 06. 4) "dest" 07. 5) "lst.user" 08. 6) "lst.tect" 09.redis 127.0.0.1:6379> keys scor? 10.1) "score" 11.redis 127.0.0.1:6379> keys scor[ee1] 12.1) "score" [root@xsf001 ~]# redis-cli redis 127.0.0.1:6379> keys * #所有key 1) "score" 2) "stu" 3) "score1" 4) "dest" 5) "lst.user" 6) "lst.tect" redis 127.0.0.1:6379> keys scor? 1) "score" redis 127.0.0.1:6379> keys scor[ee1]毕业论文 1) "score" 2)randomkey
语法:randomkey
解释:返回一个随机key
[plain] view plaincopyprint? 01.redis 127.0.0.1:6379> randomkey 02."score" 03.redis 127.0.0.1:6379> randomkey 04."list.user" redis 127.0.0.1:6379> randomkey "score" redis 127.0.0.1:6379> randomkey "list.user" 3)exists
语法:exists key
解释:判断一个key是否存在
[plain] view plaincopyprint? 01.redis 127.0.0.1:6379> exists score #key存在 返回1 02.(integer) 1 03.redis 127.0.0.1:6379> exists scorefda #key不存在 返回0 04.(integer) 0 redis 127.0.0.1:6379> exists score #key存在 返回1 (integer) 1 redis 127.0.0.1:6379> exists scorefda #key不存在 返回0 (integer) 0 4)type
语法:type key
解释:返回key所存储的值类型,返回值:none【key不存在】,string,list ,set, zset和hash
[plain] view plaincopyprint? 01.redis 127.0.0.1:6379> type score 02.zset 03.redis 127.0.0.1:6379> type lst.user 04.list redis 127.0.0.1:6379> type score zset redis 127.0.0.1:6379> type lst.user list 5)expire
语法:expire key seconds
解释:设置key的生存时间,单位是秒,当key过期时,会被自动删除
[plain] view plaincopyprint? 01.redis 127.0.0.1:6379> expire dest 30 02.(integer) 1 03.redis 127.0.0.1:6379> expire dest1 30 # key不存在 04.(integer) 0 redis 127.0.0.1:6379> expire dest 30 (integer) 1 redis 127.0.0.1:6379> expire dest1 30 # key不存在 (integer) 0 6)ttl
语法: ttl key
解释:得到key能存活时间,如果key不存在或没有设置生存时间时,返回-1
[plain] view plaincopyprint? 01.redis 127.0.0.1:6379> expire diff 100 02.(integer) 1 03.redis 127.0.0.1:6379> ttl diff 04.(integer) 94 05.redis 127.0.0.1:6379> ttl diff 06.(integer) 92 redis 127.0.0.1:6379> expire diff 100 (integer) 1 redis 127.0.0.1:6379> ttl diff (integer) 94 redis 127.0.0.1:6379> ttl diff (integer) 92 7)persist
语法:persist key
解释:移除给定key的生存时间
[plain] view plaincopyprint? 01.redis 127.0.0.1:6379> ttl diff 02.(integer) 28 03.redis 127.0.0.1:6379> persist diff 04.(integer) 1 05.redis 127.0.0.1:6379> ttl diff 06.(integer) -1 redis 127.0.0.1:6379> ttl diff (integer) 28 redis 127.0.0.1:6379> persist diff (integer) 1 redis 127.0.0.1:6379> ttl diff (integer) -1 8)rename
语法:rename key newkey [1] [2] 下一页
|