127.0.0.1:6379> SET name joker
OK
127.0.0.1:6379> GET name
joker
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> DEL name
1
127.0.0.1:6379> GET name
127.0.0.1:6379>
DUMP
EXISTS
判断一个key是否存在
时间复杂度:O(1)
返回值:
若 key 存在,返回 1 ,否则返回 0 。
1
2
3
4
5
6
7
8
9
10
11
12
127.0.0.1:6379> set name haha
OK
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> exists name
1
127.0.0.1:6379>
127.0.0.1:6379> del name
1
127.0.0.1:6379> exists name
0
127.0.0.1:6379>
EXPIRE
EXPIRE key seconds
设置key的过期时间
以下几个操作不会改变key的过期时间
对一个key进行INCR,LPUSH,HSET等
RENAME,重命名某个key
Note
SET 同一个 key时会删除过期时间
1
2
3
4
5
6
7
8
9
10
11
127.0.0.1:6379> set name joker
OK
127.0.0.1:6379> expire name 10000
1
127.0.0.1:6379> ttl name
9997
127.0.0.1:6379> set name top
OK
127.0.0.1:6379> ttl name
-1
127.0.0.1:6379>
另外如果想移除某个key的过期时间,可以用 PERSIST命令
时间复杂度
O(1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
127.0.0.1:6379> get name
haha
127.0.0.1:6379> expire name 1000
1
127.0.0.1:6379> ttl name
993
127.0.0.1:6379> expire name 1000
1
127.0.0.1:6379> ttl name
998
127.0.0.1:6379> ttl name
976
127.0.0.1:6379> PERSIST name
1
127.0.0.1:6379> ttl name
-1
127.0.0.1:6379>
EXPIREAT
EXPIREAT key timestamp
从字面意思可知,设置过期日期
时间复杂度:
O(1)
1
2
3
4
5
6
// 1658997850 -->2022-07-28 16:44:10
127.0.0.1:6379> expireat name 1658997850
1
127.0.0.1:6379> ttl name
7183
127.0.0.1:6379>
命令的内部实现是这样的:在传输目标实例之前,会执行DUMP操作将本地指定的key序列化,然后在传送给目标实例,目标实例接收后执行RESTORE将数据反序列化,并添加到数据库中。如果可选参数是REPLACE会删除本地实例的key,如果是COPY则不会删除,但是如果目标实例有相同的key会发生错误ERR Target instance replied with error: BUSYKEY Target key name already exists.
1
2
3
4
5
6
7
8
9
10
11
12
13
127.0.0.1:6379> MIGRATE 111.36.72.254 6375 joker 0 1000 COPY AUTH 1234
OK
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> get joker
hahahhah
111.36.72.254:6375> get joker
"hahahhah"
127.0.0.1:6379> MIGRATE 111.36.72.254 6375 joker 0 1000 COPY AUTH 1234
ERR Target instance replied with error: BUSYKEY Target key name already exists.
Note
MIGRATE 命令是原子操作,即使失败也不会丢key
MOVE
MOVE key db
将指定key的移动到指定的数据库中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
127.0.0.1:6379> select 0
OK
127.0.0.1:6379> get joker
hahahhah
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> move joker 1
1
127.0.0.1:6379> exists joker
0
127.0.0.1:6379>
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get joker
hahahhah
127.0.0.1:6379[1]>
但是试图移动的key和目标数据库存在key相同,目标数据库的key对应的value并不会被覆盖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379>
127.0.0.1:6379> get joker
127.0.0.1:6379> set joker jjjjj
OK
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> move joker 1
0
127.0.0.1:6379>
127.0.0.1:6379> get joker
jjjjj
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get joker
hahahhah
127.0.0.1:6379[1]>
OBJECT
OBJECT subcommand [arguments [arguments]]
OBJECT REFCOUNT key 返回给定 key 引用所储存的值的次数
OBJECT ENCODING key 储存的值所使用的内部表示
OBJECT IDLETIME key 自储存以来的空转时间
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
127.0.0.1:6379> set name haha
OK
127.0.0.1:6379> OBJECT REFCOUNT name
1
127.0.0.1:6379>
127.0.0.1:6379> OBJECT ENCODING name
embstr
127.0.0.1:6379>
127.0.0.1:6379> OBJECT IDLETIME name
36
127.0.0.1:6379> GET name
haha
127.0.0.1:6379> OBJECT IDLETIME name
2
127.0.0.1:6379>
PERSIST
PERSIST key
移除给定 key 的生存时间
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
127.0.0.1:6379> set name haha
OK
127.0.0.1:6379>
127.0.0.1:6379> expire name 10000
1
127.0.0.1:6379> ttl name
9997
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> PERSIST name
1
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> ttl name
-1
127.0.0.1:6379>
PEXPIREAT
PEXPIREAT key milliseconds-timestamp
以毫秒为单位设置过期时间
1
2
3
4
5
6
7
8
9
127.0.0.1:6379> set name 123
OK
127.0.0.1:6379>
//1659166810000 -->2022-07-30 15:40:10
127.0.0.1:6379> PEXPIREAT name 1659166810000
1
127.0.0.1:6379> ttl name
3547
127.0.0.1:6379>
PTTL
PTTL key
以毫秒为单位返回 key 的剩余生存时间
1
2
3
4
5
6
7
8
9
127.0.0.1:6379> set name haha
OK
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> expire name 10
1
127.0.0.1:6379> pttl name
4075
127.0.0.1:6379>
127.0.0.1:6379> set name 123
OK
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> expire name 100
1
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> ttl name
92
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> rename name rename
OK
127.0.0.1:6379> ttl rename
77
如果已经存在新的key,则会覆盖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
127.0.0.1:6379> set name haha
OK
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> set name1 joker
OK
127.0.0.1:6379>
127.0.0.1:6379> rename name name1
OK
127.0.0.1:6379>
127.0.0.1:6379> get name1
haha
127.0.0.1:6379> get name
127.0.0.1:6379>
RENAMENX
RENAMENX key newkey
当且仅当 newkey 不存在时,将 key 改名为 newkey
1
2
3
4
5
6
7
8
9
10
127.0.0.1:6379> set name haha
OK
127.0.0.1:6379> set name1 joker
OK
127.0.0.1:6379> RENAMENX name name1
0
127.0.0.1:6379> MGET name name1
haha
joker
127.0.0.1:6379>
127.0.0.1:6379> set name haha
OK
127.0.0.1:6379> ttl name
-1
127.0.0.1:6379> ttl nam
-2
127.0.0.1:6379> expire name 100
1
127.0.0.1:6379> ttl name
96
127.0.0.1:6379>
127.0.0.1:6379> EXISTS name
(integer) 0
127.0.0.1:6379> APPEND name haha
(integer) 4
127.0.0.1:6379> get name
"haha"
127.0.0.1:6379> APPEND name 'love you'
(integer) 12
127.0.0.1:6379> get nmae
(nil)
127.0.0.1:6379> get name
"hahalove you"