5. Redis key相关命令

2015-11-20  本文已影响451人  大明白

KEYS pattern

返回所有给定模式的key(pattern类似于正则)

EXISTS key

检测指定key是否存在,存在返回1,否则返回0

127.0.0.1:6379> EXISTS userinfo
(integer) 1

TYPE key

返回key存储的类型,如果不存在则返回none

127.0.0.1:6379> TYPE userinfo
hash
127.0.0.1:6379> TYPE redis
string
127.0.0.1:6379> TYPE notExists
none

EXPIRE key seconds

设置key的过期时间,如果key不存在则返回0,否则返回1.如果key已经存在过期时间则再设置会覆盖之前的过期时间

127.0.0.1:6379> EXPIRE tedis 5
(integer) 1
127.0.0.1:6379> TTL tedis
(integer) 2
127.0.0.1:6379> TTL tedis
(integer) -2
127.0.0.1:6379> EXISTS tedis
(integer) 0
127.0.0.1:6379> EXPIRE tedis 5
(integer) 0   // tedis不存在

EXPIREAT key timestamp

设置在指定timestamp过期

127.0.0.1:6379> EXPIREAT redis 1448018396
(integer) 1
127.0.0.1:6379> TTL redis
(integer) 6960

PEXPIRE key milliseconds

设定过期时间为毫秒,其他与EXPIRE相同

127.0.0.1:6379> PEXPIRE redis 60000
(integer) 1
127.0.0.1:6379> TTL redis
(integer) 58

PEXPIREAT key timestamp

设定指定timestamp过期,单位是毫秒,其他与EXPIREBY相同

127.0.0.1:6379> PEXPIREAT redis 1448018396000
(integer) 1
127.0.0.1:6379> TTL redis
(integer) 6662

TTL key

以秒为单位返回key的剩余时间,如果没有设置过期时间,则返回-1,如果key不存在,则返回-2

127.0.0.1:6379> TTL redis
(integer) 6523
127.0.0.1:6379> TTL stronger
(integer) -2
127.0.0.1:6379> TTL str1
(integer) -1

PTTL key

以毫秒为单位,返回key的剩余时间,如果没有设置过期时间,则返回-1,如果key不存在,则返回-2

127.0.0.1:6379> PTTL redis
(integer) 6505944
127.0.0.1:6379> PTTL str1
(integer) -1
127.0.0.1:6379> PTTL stronger
(integer) -2

PERSIST key

将带有过期时间的key变为永久的key,成功返回1,key不存在或者已经是永久的key则返回0

127.0.0.1:6379> TTL redis
(integer) 6377
127.0.0.1:6379> PERSIST redis
(integer) 1
127.0.0.1:6379> TTL redis
(integer) -1

DEL key [key]##

删除指定的key 一个或多个,返回值是删除的key的个数

127.0.0.1:6379> DEL str5
(integer) 1
127.0.0.1:6379> DEL str5
(integer) 0
127.0.0.1:6379> DEL str3 str4
(integer) 2

RANDOMKEY

随机的从当前数据库中返回一个KEY

RENAME key newkey##

重命名key为newkey,如果newkey是已经存在的key则覆盖它,如果key和newkey相同则报错,如果rename一个不存在的key会保存

127.0.0.1:6379> KEYS *
1) "str1"
2) "str6"
3) "yiibai"
4) "str2"
5) "userinfo"
6) "redis"
7) "page2"
8) "page"
127.0.0.1:6379> RENAME str6 str3
OK
127.0.0.1:6379> RENAME str3 str2
OK
127.0.0.1:6379> KEYS *
1) "str1"
2) "str2"
3) "yiibai"
4) "userinfo"
5) "redis"
6) "page2"
7) "page"
127.0.0.1:6379> GET str2
"text6"
127.0.0.1:6379> GET str1
"text1"
127.0.0.1:6379> RENAME str2 str2
(error) ERR source and destination objects are the same

RENAMENX key newkey

必须重命名这个新名称不存在才能成功,失败返回0

127.0.0.1:6379> EXISTS str1
(integer) 1
127.0.0.1:6379> RENAMENX str2 str1
(integer) 0

DUMP key

序列化个定的key,返回序列化之后的值

127.0.0.1:6379> DUMP redis
"\x00\x0bhello world\x06\x00\x9d\x11!9S\x92\x9c\xa7"

RESTORE key ttl value

反序列化字符串到key上,这个key不能是已存在的,否则出粗。ttl设定有效时间的毫秒数,设置0表示永久

127.0.0.1:6379> RESTORE str5 0 "\x00\x0bhello world\x06\x00\x9d\x11!9S\x92\x9c\xa7"
OK
127.0.0.1:6379> RESTORE str2 0 "\x00\x0bhello world\x06\x00\x9d\x11!9S\x92\x9c\xa7"
(error) BUSYKEY Target key name already exists.

MOVE key DbId

移动key从一个数据库到另外一个数据库中,如果移动的key不存在则操作失败;如果目的数据库已经存在key则操作失败返回0,成功返回1

127.0.0.1:6379> MOVE str5 2
(integer) 1  // 成功
127.0.0.1:6379> SELECT 2
OK
127.0.0.1:6379[2]> KEYs *
1) "str5"
127.0.0.1:6379[2]> SELECT 0
OK
127.0.0.1:6379> KEYS str*
1) "str1"
2) "str2"
127.0.0.1:6379> set str5 "from 0"
OK
127.0.0.1:6379> KEYS str*
1) "str1"
2) "str2"
3) "str5"
127.0.0.1:6379> MOVE str5 2
(integer) 0  // 失败,2中已存在key

OBJECT MIGRATE SCAN SORT 后续讲解

上一篇下一篇

猜你喜欢

热点阅读