Redis命令-Key

keys [pattern]

查找所有符合给定筛选项的key

127.0.0.1:6379> set test1 a
OK
127.0.0.1:6379> set test2 b
OK
127.0.0.1:6379> set test3 c
OK
127.0.0.1:6379> set test4 d
OK
127.0.0.1:6379> set prefix-test1 e
OK
127.0.0.1:6379> set prefix-test2 f
OK
127.0.0.1:6379> keys * 
1) "prefix-test2"
2) "prefix-test1"
3) "test2"
4) "test3"
5) "test1"
6) "test4"
127.0.0.1:6379> keys test*
1) "test2"
2) "test3"
3) "test1"
4) "test4"
127.0.0.1:6379> keys prefix*
1) "prefix-test2"
2) "prefix-test1"
127.0.0.1:6379> 

del [key]

删除已经存在的key,不存在的key会忽略,返回值是删除key的数量

127.0.0.1:6379> del test1
(integer) 1
127.0.0.1:6379> del no-exists-key
(integer) 0
127.0.0.1:6379> keys test*
1) "test2"
2) "test3"
3) "test4"

exists [key]

检查给定key是否存在,存在返回1,不存在返回0

127.0.0.1:6379> exists test1
(integer) 0
127.0.0.1:6379> exists test2
(integer) 1
127.0.0.1:6379> keys *
1) "prefix-test2"
2) "prefix-test1"
3) "test2"
4) "test3"
5) "test4"

dump [key]

序列化给定的key,key不存在返回nil,否则返回序列化之后的值

127.0.0.1:6379> dump test1
(nil)
127.0.0.1:6379> dump test2
"\x00\x01b\b\x00\xaf\tT.\x0f\xfa\x98\xbb"
127.0.0.1:6379> keys *
1) "prefix-test2"
2) "prefix-test1"
3) "test2"
4) "test3"
5) "test4"

expire [key]

为给定key设置过期时间(单位:秒),过期后将不再可用,设置成功时返回1,当key不存在或者低版本不能设置过期时间时返回0

127.0.0.1:6379> set expire-key 1
OK
127.0.0.1:6379> get expire-key
"1"
127.0.0.1:6379> expire expire-key 5
(integer) 1
127.0.0.1:6379> get expire-key
(nil)

expireat [key]

作用和expire类似,区别在于该命令是以unix时间戳格式设置过期时间

127.0.0.1:6379> set expireat-key 1
OK
127.0.0.1:6379> get expireat-key
"1"
127.0.0.1:6379> expireat expireat-key 1537302959
(integer) 1
127.0.0.1:6379> get expireat-key
(nil)

pexpire [key]

作用和expire类似,区别在于该命令是以毫秒为单位设置过期时间

127.0.0.1:6379> set pexpire-key 1
OK
127.0.0.1:6379> get pexpire-key
"1"
127.0.0.1:6379> pexpire pexpire-key 5000
(integer) 1
127.0.0.1:6379> get pexpire-key
(nil)

pexpireat [key]

作用和expireat类似,区别在于该命令的unix时间戳以毫秒计

127.0.0.1:6379> set pexpireat-key 1
OK
127.0.0.1:6379> get pexpireat-key
"1"
127.0.0.1:6379> pexpireat pexpireat-key 1537303302055
(integer) 1
127.0.0.1:6379> get pexpireat-key
(nil)

move [key] [dbid]

将给定的key移动到给定的数据库中,成功时返回1,失败时返回0,当目标数据库有相同的key时,返回0

# 移动存在的key
127.0.0.1:6379> select 0
OK
127.0.0.1:6379> set move-key 1
OK
127.0.0.1:6379> move move-key 1
(integer) 1
127.0.0.1:6379> exists move-key
(integer) 0
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> exists move-key
(integer) 1

# 移动不存在的key
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> exists move-key2
(integer) 0
127.0.0.1:6379> move move-key2 1
(integer) 0
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> exists move-key2
(integer) 0

# 当目标数据库有相同的key时,返回0
127.0.0.1:6379[1]> select 1
OK
127.0.0.1:6379[1]> set move-key3 1
OK
127.0.0.1:6379[1]> exists move-key3
(integer) 1
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> set move-key3 2
OK
127.0.0.1:6379> move move-key3 1
(integer) 0
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get move-key3
"1"

persist [key]

移除key的过期时间,成功返回1,失败返回0,当key不存在或者key没有设置过期时间时返回0

127.0.0.1:6379> set persist-key 1
OK
127.0.0.1:6379> expire persist-key 15
(integer) 1
127.0.0.1:6379> persist persist-key
(integer) 1
127.0.0.1:6379> get persist-key
"1"

ttl [key]

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

Redis 2.8 以前,当key不存在或者没有设置过期时间时都返回的是-1

127.0.0.1:6379> ttl ttl-key
(integer) -2
127.0.0.1:6379> set ttl-key 1
OK
127.0.0.1:6379> ttl ttl-key
(integer) -1
127.0.0.1:6379> expire ttl-key 10
(integer) 1
127.0.0.1:6379> ttl ttl-key
(integer) 9

pttl [key]

作用和ttl类似,区别在于返回的剩余过期时间单位是毫秒

127.0.0.1:6379> pttl pttl-key
(integer) -2
127.0.0.1:6379> set pttl-key 1
OK
127.0.0.1:6379> pttl pttl-key
(integer) -1
127.0.0.1:6379> pexpire pttl-key 9000
(integer) 1
127.0.0.1:6379> pttl pttl-key
(integer) 7471

randomkey

从当前数据库中随机返回一个key,当数据库不为空时返回一个key,当数据库为空时返回nil

127.0.0.1:6379> keys *
1) "move-key3"
2) "prefix-test2"
3) "persist-key"
4) "prefix-test1"
5) "test2"
6) "test3"
7) "test4"
127.0.0.1:6379> randomkey
"test4"
127.0.0.1:6379> randomkey
"test4"
127.0.0.1:6379> randomkey
"test2"
127.0.0.1:6379> randomkey
"persist-key"
127.0.0.1:6379> flushall
OK
127.0.0.1:6379> randomkey
(nil)

rename [old-key] [new-key]

重命名key,成功时提示OK,失败时返回一个错误

  • 当旧Key和新Key相同时,返回OK
  • 当旧Key不存在时,返回一个错误
  • 当新Key已经存在时,会覆盖旧值
# 正常情况
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set rename-key 1
OK
127.0.0.1:6379> keys *
1) "rename-key"
127.0.0.1:6379> rename rename-key rename-new-key
OK
127.0.0.1:6379> keys *
1) "rename-new-key"

# 旧Key和新Key相同
127.0.0.1:6379> set rename-key3 1
OK
127.0.0.1:6379> rename rename-key3 rename-key3
OK

# 旧key不存在
127.0.0.1:6379> exists rename-key2
(integer) 0
127.0.0.1:6379> rename rename-key2 rename-new-key2
(error) ERR no such key

# 新key已经存在
127.0.0.1:6379> set rename-key4 1
OK
127.0.0.1:6379> set rename-exists-key 2
OK
127.0.0.1:6379> get rename-exists-key
"2"
127.0.0.1:6379> rename rename-key4 rename-exists-key
OK
127.0.0.1:6379> get rename-exists-key
"1"

renamenx [old-key] [new-key]

仅当新Key不存在时返回1,新Key存在时返回0

# 新key不存在
127.0.0.1:6379> set renamenx-key 1
OK
127.0.0.1:6379> exists renamenx-new-key
(integer) 0
127.0.0.1:6379> renamenx renamenx-key renamenx-new-key
(integer) 1
127.0.0.1:6379> get renamenx-new-key
"1"

# 新key存在时
127.0.0.1:6379> set renamenx-key2 1
OK
127.0.0.1:6379> set renamenx-new-key2 2
OK
127.0.0.1:6379> renamenx renamenx-key2 renamenx-new-key2
(integer) 0
127.0.0.1:6379> get renamenx-new-key2
"2"

type [key]

返回key的数据类型,数据类型有:

  • none(key不存在)
  • string(字符串)
  • list(列表)
  • set(集合)
  • zset(有序集合)
  • hash(哈希)
127.0.0.1:6379> type none-exists-key
none
127.0.0.1:6379> set string-key 1
OK
127.0.0.1:6379> type string-key
string
127.0.0.1:6379> lpush list-key 1
(integer) 1
127.0.0.1:6379> type list-key
list
127.0.0.1:6379> sadd set-key 1
(integer) 1
127.0.0.1:6379> type set-key
set
127.0.0.1:6379> zadd zset-key 1 redis
(integer) 1
127.0.0.1:6379> type zset-key
zset
127.0.0.1:6379> hmset hash-key filed1 "hello" filed2 "redis"
OK
127.0.0.1:6379> type hash-key
hash
Redis Redis

Laravel5使用Passport密码授权单用户登录的方法