Redis常用命令
Redis数据类型:
String 字符串;
Lists 列表;
Sets 集合;
Sorted sets 有序集合;
Hashs 哈希表;
常用命令
# 插入数据
set name php
# 读取
get name
# 删除键值
del name
# 验证键值是否存在
exists name
# setnx;,设置 key 对应的值为 string 类型的 value。如果 key 已经存在,返回 0,nx 是 not exist
setnx age 20
# setex 设置 key 对应的值为 string 类型的 value,并指定此键值对应的有效期
setex color 15 red
# setrange 设置指定 key 的 value 值的子字符串
setrange php 8 com
其中的 8 是指从下标为 8(包含 8)的字符开始替换
# mset 一次设置多个 key 的值,成功返回 ok 表示所有的值都设置了,失败返回 0 表示没有任何值
被设置。
127.0.0.1:6379> mset sz ShenZhen bj BeiJing sh ShangHai
OK
127.0.0.1:6379> mget sz bj sh
1) "ShenZhen"
2) "BeiJing"
3) "ShangHai"
# msetnx 一次设置多个 key 的值,成功返回 ok 表示所有的值都设置了,失败返回 0 表示没有任何值
被设置,但是不会覆盖已经存在的 key;
如果这条命令返回 0,那么里面操作都会回滚,都不会被执行。
127.0.0.1:6379> msetnx gz Guangzhou tj tianjin
(integer) 1
127.0.0.1:6379> mget gz tj
1) "Guangzhou"
2) "tianjin"
127.0.0.1:6379> msetnx bj beijing nj NanJing
(integer) 0
127.0.0.1:6379> mget bj nj
1) "BeiJing"
2) (nil)
# get 获取 key 对应的 string 值,如果 key 不存在返回 nil
127.0.0.1:6379> get phppp
(nil)
127.0.0.1:6379> get php
"phpcomt"
# getset 设置 key 的值,并返回 key 的旧值; 当 key 没有旧值时,即 key 不存在时,返回 nil
127.0.0.1:6379> get year
"2024"
127.0.0.1:6379> getset year 2016
"2024"
127.0.0.1:6379> get year
"2016"
127.0.0.1:6379> getset room 405
(nil)
127.0.0.1:6379> get room
"405"
# getrange 获取指定 key 的 value 值的子字符串;
字符串左面下标是从 0 开始的;
字符串右面下标是从-1 开始的;
当下标超出字符串长度时,将默认为是同方向的最大下标
127.0.0.1:6379> get room
"405"
127.0.0.1:6379> getrange room 2 2
"5"
127.0.0.1:6379> get php
"phpcomt"
127.0.0.1:6379> getrange php 1 3
"hpc"
127.0.0.1:6379> getrange php 5 -1
"mt"
127.0.0.1:6379> getrange php 5 2024
"mt"
# mget 一次获取多个 key 的值,如果对应 key 不存在,则对应返回 nil
127.0.0.1:6379> mget bj tj sz dj
1) "BeiJing"
2) "tianjin"
3) "ShenZhen"
4) (nil)
# incr 对 key 的值做加加操作,并返回新的值。注意 incr 一个不是 int 的 value 会返回错误,incr 一
个不存在的 key,则设置 key 为 1
127.0.0.1:6379> get year
"2016"
127.0.0.1:6379> incr year
(integer) 2017
127.0.0.1:6379> incr year
(integer) 2018
127.0.0.1:6379> get years
(nil)
127.0.0.1:6379> incr years
(integer) 1
127.0.0.1:6379> get years
"1"
# incrby 同 incr 类似,加指定值 ,key 不存在时候会设置 key,并认为原来的 value 是 0
127.0.0.1:6379> get year
"2018"
127.0.0.1:6379> incrby year 3
(integer) 2021
127.0.0.1:6379> incrby yy 100
(integer) 100
127.0.0.1:6379> get yy
"100"
# decr 对 key 的值做的是减减操作,decr 一个不存在 key,则设置 key 为-1
127.0.0.1:6379> get yy
"100"
127.0.0.1:6379> decr yy
(integer) 99
127.0.0.1:6379> get yy
"99"
127.0.0.1:6379> decrby yy 1
(integer) 98
127.0.0.1:6379> get yy
"98"
127.0.0.1:6379> decr key
(integer) -1
127.0.0.1:6379> get key
"-1"
# decrby 制定相减值
127.0.0.1:6379> decrby yy 1
(integer) 97
127.0.0.1:6379> get yy
"97"
127.0.0.1:6379> decrby yy 3
(integer) 94
# append 给指定 key 的字符串值追加 value,返回新字符串值的长度
127.0.0.1:6379> get php
"phpcomt"
127.0.0.1:6379> append php -com
(integer) 11
127.0.0.1:6379> get php
"phpcomt-com"
# strlen 取指定 key 的 value 值的长度。
127.0.0.1:6379> get php
"phpcomt-com"
127.0.0.1:6379> strlen php
(integer) 11
127.0.0.1:6379> get year
"2021"
127.0.0.1:6379> strlen year
(integer) 4
hashes 类型及操作,Redis hash 是一个 string 类型的 field 和 value 的映射表,hash 特别适合用于存储对象。相较于将对象的每个字段存成单个 string 类型。将一个对象存储在 hash 类型中会占用更少的内存,并且可以更方便的存取整个对象
# hset 设置 hash field 为指定值,如果 key 不存在,则先创建
127.0.0.1:6379> hset student name xiaohong
(integer) 1
127.0.0.1:6379> hset student age 13
(integer) 1
127.0.0.1:6379> hset student sex boy
(integer) 1
# hsetnx 设置 hash field 为指定值,如果 key 不存在,则先创建。如果 field 已经存在,返回 0,nx 是
not exist的意思
127.0.0.1:6379> hmget student name age sex
1) "xiaohong"
2) "13"
3) "boy"
127.0.0.1:6379> hsetnx student address shenzhen
(integer) 1
127.0.0.1:6379> hget student address
"shenzhen"
127.0.0.1:6379> hsetnx student age 11
(integer) 0
127.0.0.1:6379> hmget student name age
1) "xiaohong"
2) "13"
# hmset 同时设置 hash 的多个 field
127.0.0.1:6379> hmset student email xiaohong@qq.com tel 15888888888 qq 123456
OK
# hget 获取指定的 hash field。
127.0.0.1:6379> hget student ema
(nil)
127.0.0.1:6379> hget student email
"xiaohong@qq.com"
127.0.0.1:6379> hget student qq
"123456"
127.0.0.1:6379> hget student tel
"15888888888"
127.0.0.1:6379>
# hmget 获取全部指定的 hash filed
127.0.0.1:6379> hmget student name age sex address email qq tel
1) "xiaohong"
2) "13"
3) "boy"
4) "shenzhen"
5) "xiaohong@qq.com"
6) "123456"
7) "15888888888"
# hincrby 指定的 hash filed 加上给定值
127.0.0.1:6379> hincrby student age 1
(integer) 14
127.0.0.1:6379> hincrby student name 1
(error) ERR hash value is not an integer
# hexists 测试指定 field 是否存在
127.0.0.1:6379> hexists student salary
(integer) 0
127.0.0.1:6379> hexists student qq
(integer) 1
127.0.0.1:6379> hget student qq
"123456"
# hlen 返回指定 hash 的 field 数量
127.0.0.1:6379> hlen student
(integer) 7
127.0.0.1:6379> hlen teacher
(integer) 0
127.0.0.1:6379> exists teacher
(integer) 0
# hkeys 返回 hash 的所有 field
127.0.0.1:6379> hkeys student
1) "name"
2) "age"
3) "sex"
4) "address"
5) "email"
6) "tel"
7) "qq"
127.0.0.1:6379> hkeys xxx
(empty array)
# hvals 返回 hash 的所有 value
127.0.0.1:6379> hvals student
1) "xiaohong"
2) "14"
3) "boy"
4) "xiaohong@qq.com"
5) "15888888888"
6) "123456"
127.0.0.1:6379> hvals abc
(empty array)
# hgetall 获取某个 hash 中全部的 filed 及 value
127.0.0.1:6379> hgetall student
1) "name"
2) "xiaohong"
3) "age"
4) "14"
5) "sex"
6) "boy"
7) "email"
8) "xiaohong@qq.com"
9) "tel"
10) "15888888888"
11) "qq"
12) "123456"
# hdel 命令用于删除哈希表 key 中的一个或多个指定字段,不存在的字段将被忽略。
127.0.0.1:6379> hkeys student
1) "name"
2) "age"
3) "sex"
4) "address"
5) "email"
6) "tel"
7) "qq"
127.0.0.1:6379> hdel student address xxx
(integer) 1
127.0.0.1:6379> hkeys student
1) "name"
2) "age"
3) "sex"
4) "email"
5) "tel"
6) "qq"
lists 类型及操作
list 是一个链表结构,主要功能是 push、pop、获取一个范围的所有值等等,操作中 key 理解为链表的名字
# lpush 在 key 对应 list 的头部添加字符串元素
127.0.0.1:6379> lpush week 1 2 3
(integer) 3
127.0.0.1:6379> lpush week 4
(integer) 4
其中 lrange 是用于取 mylist 的内容
127.0.0.1:6379> lrange week 0 -1
1) "4"
2) "3"
3) "2"
4) "1"
127.0.0.1:6379>
# rpush 在 key 对应 list 的尾部添加字符串元素
127.0.0.1:6379> lrange week 0 -1
1) "4"
2) "3"
3) "2"
4) "1"
127.0.0.1:6379> rpush week 11
(integer) 5
127.0.0.1:6379> lrange week 0 -1
1) "4"
2) "3"
3) "2"
4) "1"
5) "11"
# linsert 在 key 对应 list 的特定位置之前或之后添加字符串元素
127.0.0.1:6379> lrange week 0 -1
1) "12"
2) "4"
3) "3"
4) "2"
5) "1"
6) "11"
7) "13"
127.0.0.1:6379> linsert week before 3 33
(integer) 8
127.0.0.1:6379> lrange week 0 -1
1) "12"
2) "4"
3) "33"
4) "3"
5) "2"
6) "1"
7) "11"
8) "13"
127.0.0.1:6379> linsert week after 2 22
(integer) 9
127.0.0.1:6379> lrange week 0 -1
1) "12"
2) "4"
3) "33"
4) "3"
5) "2"
6) "22"
7) "1"
8) "11"
9) "13"
127.0.0.1:6379>
# lset 设置 list 中指定下标的元素值(下标从 0 开始)
127.0.0.1:6379> lset week 0 'zero'
OK
127.0.0.1:6379> lrange week 0 -1
1) "zero"
2) "4"
3) "33"
4) "3"
5) "2"
6) "22"
7) "1"
8) "11"
9) "13"
127.0.0.1:6379> lset week 1 'one'
OK
127.0.0.1:6379> lrange week 0 -1
1) "zero"
2) "one"
3) "33"
4) "3"
5) "2"
6) "22"
7) "1"
8) "11"
9) "13"
127.0.0.1:6379>
# lrem 从 key 对应 list 中删除 count 个和 value 相同的元素
count<0 时,按从尾到头的顺序删除;
count=0 时,删除全部;
127.0.0.1:6379> lrem week 1 1
(integer) 1
127.0.0.1:6379> lrange week 0 -1
1) "zero"
2) "one"
3) "33"
4) "3"
5) "2"
6) "22"
7) "11"
8) "13"
127.0.0.1:6379> lrem week 1 11
(integer) 1
127.0.0.1:6379> lrange week 0 -1
1) "zero"
2) "one"
3) "33"
4) "3"
5) "2"
6) "22"
7) "13"
# ltrim 保留指定 key 的值范围内的数据
127.0.0.1:6379> lrange week 0 -1
1) "zero"
2) "one"
3) "33"
4) "3"
5) "2"
6) "22"
7) "13"
127.0.0.1:6379> ltrim week 0 5
OK
127.0.0.1:6379> lrange week 0 -1
1) "zero"
2) "one"
3) "33"
4) "3"
5) "2"
6) "22"
# lpop 从 list 的头部删除元素,并返回删除元素
127.0.0.1:6379> lrange week 0 -1
1) "zero"
2) "one"
3) "33"
4) "3"
5) "2"
6) "22"
127.0.0.1:6379> lpop week
"zero"
127.0.0.1:6379> lrange week 0 -1
1) "one"
2) "33"
3) "3"
4) "2"
5) "22"
127.0.0.1:6379>
# rpop 从 list 的尾部删除元素,并返回删除元素
127.0.0.1:6379> lrange week 0 -1
1) "33"
2) "3"
3) "2"
127.0.0.1:6379> rpop week
"2"
127.0.0.1:6379> lrange week 0 -1
1) "33"
2) "3"
# rpoplpush 从第一个 list 的尾部移除元素并添加到第二个 list 的头部,最后返回被移除的元素值,整个操
作是原子的.如果第一个 list 是空或者不存在返回 nil
127.0.0.1:6379> lrange city 0 -1
1) "dongjing"
2) "xian"
3) "tianjin"
4) "beijing"
5) "hangzhou"
6) "guangzhou"
7) "shanghai"
8) "nanjing"
127.0.0.1:6379> lrange week 0 -1
1) "33"
2) "3"
127.0.0.1:6379> rpoplpush city week
"nanjing"
127.0.0.1:6379> lrange week 0 -1
1) "nanjing"
2) "33"
3) "3"
127.0.0.1:6379> rpoplpush xxxxx city
(nil)
# lindex 返回名称为 key 的 list 中 index 位置的元素
127.0.0.1:6379> lrange week 0 -1
1) "nanjing"
2) "33"
3) "3"
127.0.0.1:6379> lindex week 0
"nanjing"
127.0.0.1:6379> lindex week 2
"3"
127.0.0.1:6379> lindex week 33
(nil)
# llen 返回 key 对应 list 的长度
127.0.0.1:6379> lrange week 0 -1
1) "nanjing"
2) "33"
3) "3"
127.0.0.1:6379> llen week
(integer) 3
127.0.0.1:6379> lrange city 0 -1
1) "dongjing"
2) "xian"
3) "tianjin"
4) "beijing"
5) "hangzhou"
6) "guangzhou"
7) "shanghai"
127.0.0.1:6379> llen city
(integer) 7
sets 类型及操作,set 是集合,和我们数学中的集合概念相似,对集合的操作有添加删除元素,有对多个集合
求交并差等操作,操作中 key 理解为集合的名字。Redis 的 set 是 string 类型的无序集合。set 元素最大可以包含(2 的 32 次方)个元素
# sadd 向名称为 key 的 set 中添加元素
127.0.0.1:6379> sadd color red blue
(integer) 2
127.0.0.1:6379> smembers color
1) "blue"
2) "red"
127.0.0.1:6379> sadd color black
(integer) 1
127.0.0.1:6379> smembers color
1) "black"
2) "blue"
3) "red"
# smembers 来查看 myset 中的所有元素
127.0.0.1:6379> smembers color
1) "black"
2) "blue"
3) "red"
# srem 删除名称为 key 的 set 中的元素 member
127.0.0.1:6379> srem color black
(integer) 1
127.0.0.1:6379> smembers color
1) "blue"
2) "red"
# spop 随机返回并删除名称为 key 的 set 中一个元素
127.0.0.1:6379> smembers color
1) "green"
2) "red"
3) "yellow"
4) "orange"
5) "blue"
6) "white"
127.0.0.1:6379> spop color
"yellow"
127.0.0.1:6379> smembers color
1) "orange"
2) "blue"
3) "green"
4) "red"
5) "white"
# sdiff 返回所有给定 key 与第一个 key 的差集
127.0.0.1:6379> smembers color2
1) "black"
2) "red"
127.0.0.1:6379> smembers color
1) "orange"
2) "blue"
3) "green"
4) "red"
5) "white"
127.0.0.1:6379> sdiff color color2
1) "blue"
2) "green"
3) "white"
4) "orange"
127.0.0.1:6379> sdiff color2 color
1) "black"
127.0.0.1:6379>
# sdiffstore 返回所有给定 key 与第一个 key 的差集,并将结果存为另一个 key
127.0.0.1:6379> smembers color
1) "blue"
2) "orange"
3) "red"
4) "white"
5) "green"
127.0.0.1:6379> smembers color2
1) "red"
2) "green"
127.0.0.1:6379> sdiff color2 color
(empty array)
127.0.0.1:6379> sdiff color clolor2
1) "orange"
2) "red"
3) "white"
4) "blue"
5) "green"
127.0.0.1:6379> sdiffstore color_diff color color2
(integer) 3
127.0.0.1:6379> smembers color_diff
1) "orange"
2) "white"
3) "blue"
# sinter 返回所有给定 key 的交集
127.0.0.1:6379> sinter color color2
1) "red"
2) "green"
# sinterstore 返回所有给定 key 的交集,并将结果存为另一个 key
127.0.0.1:6379> sinter color color2
1) "red"
2) "green"
127.0.0.1:6379> sinterstore color_inter color color2
(integer) 2
127.0.0.1:6379> smembers color_inter
1) "red"
2) "green"
127.0.0.1:6379>
# sunion 返回所有给定 key 的并集
127.0.0.1:6379> sunion color color2
1) "white"
2) "blue"
3) "orange"
4) "red"
5) "green"
# sunionstore 返回所有给定 key 的并集,并将结果存为另一个 key
127.0.0.1:6379> sunion color color2
1) "white"
2) "blue"
3) "orange"
4) "red"
5) "green"
127.0.0.1:6379> sunionstore color_union color color2
(integer) 5
127.0.0.1:6379> smembers color_union
1) "white"
2) "blue"
3) "orange"
4) "red"
5) "green"
# smove 从第一个 key 对应的 set 中移除 member 并添加到第二个对应 set 中
127.0.0.1:6379> smove color color2 blue
(integer) 1
127.0.0.1:6379> smembers color
1) "orange"
2) "red"
3) "white"
4) "green"
127.0.0.1:6379> smembers color2
1) "red"
2) "blue"
3) "green"
# scard 返回名称为 key 的 set 的元素个数
127.0.0.1:6379> scard color2
(integer) 3
127.0.0.1:6379> scard color
(integer) 4
# sismember 测试 member 是否是名称为 key 的 set 的元素
127.0.0.1:6379> sismember color blue
(integer) 0
127.0.0.1:6379> sismember color bred
(integer) 0
127.0.0.1:6379> sismember color red
(integer) 1
# srandmember 随机返回名称为 key 的 set 的一个元素,但是不删除元素
127.0.0.1:6379> srandmember color
"white"
127.0.0.1:6379> srandmember color
"red"
127.0.0.1:6379> srandmember color
"red"
sorted sets 类型及操作
# zadd 向名称为 key 的 zset 中添加元素 member,score 用于排序。如果该元素已经存在,则根据
score 更新该元素的顺序
127.0.0.1:6379> zadd student-score 1 jack
(integer) 1
127.0.0.1:6379> zadd student-score 2 liu
(integer) 1
127.0.0.1:6379> zrange student-score 0 -1
1) "jack"
2) "liu"
127.0.0.1:6379> zrange student-score 0 -1 withscores
1) "jack"
2) "1"
3) "liu"
4) "2"
127.0.0.1:6379> zadd student-score 2 wang
(integer) 1
127.0.0.1:6379> zrange student-score 0 -1 withscores
1) "jack"
2) "1"
3) "liu"
4) "2"
5) "wang"
6) "2"
# zrem 删除名称为 key 的 zset 中的元素 member
127.0.0.1:6379> zrem student-score jack
(integer) 1
127.0.0.1:6379> zrange student-score 0 -1 withscores
1) "liu"
2) "2"
3) "wang"
4) "2"
# zincrby 如果在名称为 key 的 zset 中已经存在元素 member,则该元素的 score 增加 increment;否则
向集合中添加该元素,其 score 的值为 increment
127.0.0.1:6379> zrange student-score 0 -1 withscores
1) "liu"
2) "2"
3) "wang"
4) "2"
127.0.0.1:6379> zincrby student-score 3 wang
"5"
127.0.0.1:6379> zrange student-score 0 -1 withscores
1) "liu"
2) "2"
3) "wang"
4) "5"
127.0.0.1:6379> zincrby student-score 4 huang
"4"
127.0.0.1:6379> zrange student-score 0 -1 withscores
1) "liu"
2) "2"
3) "huang"
4) "4"
5) "wang"
6) "5"
# zrank 返回名称为 key 的 zset 中 member 元素的排名(按 score 从小到大排序)即下标,而不是 score
127.0.0.1:6379> zrange student-score 0 -1 withscores
1) "liu"
2) "2"
3) "huang"
4) "4"
5) "wang"
6) "5"
127.0.0.1:6379> zrank student-score liu
(integer) 0
127.0.0.1:6379> zrank student-score huang
(integer) 1
127.0.0.1:6379> zrank student-score wang
(integer) 2
# zrevrank 返回名称为 key 的 zset 中 member 元素的排名(按 score 从大到小排序)即下标
127.0.0.1:6379> zrange student-score 0 -1 withscores
1) "liu"
2) "2"
3) "huang"
4) "4"
5) "wang"
6) "5"
127.0.0.1:6379> zrevrank student-score liu
(integer) 2
127.0.0.1:6379> zrevrank student-score huang
(integer) 1
127.0.0.1:6379> zrevrank student-score wang
(integer) 0
# zrevrange 返回名称为 key 的 zset(按 score 从大到小排序)中的 index 从 start 到 end 的所有元素
127.0.0.1:6379> zrevrange student-score 0 -1 withscores
1) "wang"
2) "5"
3) "huang"
4) "4"
5) "liu"
6) "2"
# zrangebyscore 返回集合中 score 在给定区间的元素
127.0.0.1:6379> zrevrange student-score 0 -1 withscores
1) "wang"
2) "5"
3) "huang"
4) "4"
5) "liu"
6) "2"
127.0.0.1:6379> zrangebyscore student-score 4 5 withscores
1) "huang"
2) "4"
3) "wang"
4) "5"
127.0.0.1:6379> zrangebyscore student-score 2 3 withscores
1) "liu"
2) "2"
# zcount 返回集合中 score 在给定区间的数量
127.0.0.1:6379> zrange student-score 0 -1 withscores
1) "liu"
2) "2"
3) "huang"
4) "4"
5) "wang"
6) "5"
127.0.0.1:6379> zcount student-score 2 3
(integer) 1
127.0.0.1:6379> zcount student-score 2 4
(integer) 2
127.0.0.1:6379>
# zcard 返回集合中元素个数
127.0.0.1:6379> zcard student-score
(integer) 3
# zscore 返回给定元素对应的 score
127.0.0.1:6379> zscore student-score wang
"5"
127.0.0.1:6379> zscore student-score huang
"4"
127.0.0.1:6379>
# zremrangebyrank 删除集合中排名在给定区间的元素
127.0.0.1:6379> zrange student-score 0 -1 withscores
1) "liu"
2) "2"
3) "zhao"
4) "3"
5) "huang"
6) "4"
7) "li"
8) "5"
9) "wang"
10) "5"
127.0.0.1:6379> zremrangebyrank student-score 2 3
(integer) 2
127.0.0.1:6379> zrange student-score 0 -1 withscores
1) "liu"
2) "2"
3) "zhao"
4) "3"
5) "wang"
6) "5"
# zremrangebyscore 删除集合中 score 在给定区间的元素
127.0.0.1:6379> zrange student-score 0 -1 withscores
1) "liu"
2) "2"
3) "zhao"
4) "3"
5) "wang"
6) "5"
127.0.0.1:6379> zremrangebyscore student-score 2 2
(integer) 1
127.0.0.1:6379> zrange student-score 0 -1 withscores
1) "zhao"
2) "3"
3) "wang"
4) "5"
Redis 常用命令
# keys 返回满足给定 pattern 的所有 key
127.0.0.1:6379> keys *
1) "user"
2) "student"
3) "year"
4) "coder"
5) "color_union"
6) "girl"
7) "color_diff"
8) "week"
9) "color"
10) "code"
11) "city"
12) "php"
13) "student-score"
14) "years"
15) "email"
16) "key"
17) "age"
18) "month"
19) "color2"
20) "color_inter"
127.0.0.1:6379> keys c*
1) "coder"
2) "color_union"
3) "color_diff"
4) "color"
5) "code"
6) "city"
7) "color2"
8) "color_inter"
# exists 确认一个 key 是否存在
127.0.0.1:6379> exists xxxx
(integer) 0
127.0.0.1:6379> exists city
(integer) 1
# del 删除一个 key
127.0.0.1:6379> exists xxxx
(integer) 0
127.0.0.1:6379> exists city
(integer) 1
127.0.0.1:6379> del xxxx
(integer) 0
127.0.0.1:6379> del php
(integer) 1
127.0.0.1:6379> del email
(integer) 1
127.0.0.1:6379> type php
none
127.0.0.1:6379> type email
none
127.0.0.1:6379> type xxxx
none
# expire 设置一个 key 的过期时间(单位:秒)
127.0.0.1:6379> type age
string
127.0.0.1:6379> get age
"23"
127.0.0.1:6379> expire age 20
(integer) 1
127.0.0.1:6379> ttl age
(integer) 18
127.0.0.1:6379> ttl age
(integer) 16
127.0.0.1:6379> ttl age
(integer) 16
127.0.0.1:6379> ttl age
(integer) 14
127.0.0.1:6379> get age
"23"
127.0.0.1:6379> ttl age
(integer) 6
127.0.0.1:6379> ttl age
(integer) -2
127.0.0.1:6379> ttl age
(integer) -2
127.0.0.1:6379> get age
(nil)
127.0.0.1:6379> exists age
(integer) 0
# move 将当前数据库中的 key 转移到其它数据库中
127.0.0.1:6379> type years
string
127.0.0.1:6379> get years
"1"
127.0.0.1:6379> move years 1
(integer) 1
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> keys *
1) "name"
2) "color"
3) "years"
# persist 移除给定 key 的过期时间
127.0.0.1:6379> set hi 'hello world'
OK
127.0.0.1:6379> get hi
"hello world"
127.0.0.1:6379> expire hi 100000
(integer) 1
127.0.0.1:6379> ttl hi
(integer) 99998
127.0.0.1:6379> ttl hi
(integer) 99987
127.0.0.1:6379> persist hi
(integer) 1
127.0.0.1:6379> ttl hi
(integer) -1
127.0.0.1:6379> get hi
"hello world"
# randomkey 随机返回 key 空间的一个 key
127.0.0.1:6379> randomkey
"color"
127.0.0.1:6379> randomkey
"color_union"
# rename 重命名 key
127.0.0.1:6379> rename girl woman
OK
# type 返回值的类型
127.0.0.1:6379> type hi
string
127.0.0.1:6379> type woman
hash
127.0.0.1:6379> type color2
set
# ping 测试连接是否存活
127.0.0.1:6379> ping
PONG
# select 选择数据库。Redis 数据库编号从 0~15,我们可以选择任意一个数据库来进行数据的存取
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> select 3
OK
127.0.0.1:6379[3]> keys *
(empty array)
127.0.0.1:6379[3]> select 17
(error) ERR DB index is out of range
# quit 退出连接
# dbsize 返回当前数据库中 key 的数目
# info 获取服务器的信息和统计
# flushdb 删除当前选择数据库中的所有 key
# flushall 删除所有数据库中的所有 key