键值相关命令——Redis学习笔记

1:KEYS pattern
返回满足给定pattern的所有key

1
2
3
4
5
6
7
127.0.0.1:6379> keys *
1) "name"
2) "name2"
3) "age"
127.0.0.1:6379> keys n*
1) "name"
2) "name2"

2:EXISTS key [key …]
确认一个key是否存在

1
2
3
4
127.0.0.1:6379> exists name
(integer) 1
127.0.0.1:6379> exists nokey
(integer) 0

3:DEL key [key …]
删除一个KEY

1
2
3
4
5
6
7
8
127.0.0.1:6379> keys *
1) "name"
2) "name2"
3) "age"
127.0.0.1:6379> del name
(integer) 1
127.0.0.1:6379> del name
(integer) 0

4:EXPIRE key seconds
设置一个key的过期时间

1
2
3
4
5
6
127.0.0.1:6379> expire name2 100
(integer) 1
127.0.0.1:6379> ttl name2
(integer) 96
127.0.0.1:6379> ttl name2
(integer) 94

5:TTL key
返回key的过期时间

1
2
3
4
5
6
127.0.0.1:6379> expire name2 100
(integer) 1
127.0.0.1:6379> ttl name2
(integer) 96
127.0.0.1:6379> ttl name2
(integer) 94

6:MOVE key db
移动key到另一个数据库

1
2
3
4
5
6
7
8
9
10
127.0.0.1:6379> keys *
1) "age"
127.0.0.1:6379> move age 1
(integer) 1
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> keys *
1) "age"

7:PERSIST key
移除key的过期时间

1
2
3
4
5
6
7
8
9
10
127.0.0.1:6379> set name shiwuhao ex 100
OK
127.0.0.1:6379> ttl name
(integer) 93
127.0.0.1:6379> persist name
(integer) 1
127.0.0.1:6379> ttl name
(integer) -1
127.0.0.1:6379> get name
"shiwuhao"

8:RANDOMKEY
随机返回当前数据库的一个key

1
2
3
4
5
6
7
8
127.0.0.1:6379> keys *
1) "user_name"
2) "name"
3) "age"
127.0.0.1:6379> randomkey
"name"
127.0.0.1:6379> randomkey
"age"

9:RENAME key newkey
重命名key

1
2
3
4
5
6
127.0.0.1:6379> rename name new_name
OK
127.0.0.1:6379> keys *
1) "user_name"
2) "age"
3) "new_name"

10:type
返回值的类型

1
2
3
4
5
6
127.0.0.1:6379> type age
string
127.0.0.1:6379> type set1
none
127.0.0.1:6379> type set
set

11:dbsize
返回当前数据库key的数目

1
2
3
4
5
6
7
127.0.0.1:6379> dbsize
(integer) 4
127.0.0.1:6379> keys *
1) "user_name"
2) "age"
3) "set"
4) "new_name"

12:config get *
返回当前配置参数

1
2
3
127.0.0.1:6379> config get timeout
1) "timeout"
2) "0"