【重点】 redis为什么这么快? (应届)
①redis是纯内存结构,避免磁盘I/O的耗时
②redis核心模块是一个单进程,减少线程切换和回收线程资源时间
③redis采用的是I/O的多路复用机制(每一个执行线路可以同时完成读、写),提升高并发效率(注:redis的读写仍然是单进程处理)
1、redis的服务控制命令
/etc/init.d/redis_6379 restart stop start status

2、redis的命令工具
redis-server:直接启动redis,只能启动
redis-benchmark:检测redis在本机的运行效果
redis-cli:命令行工具
redis-check-aof:检测AOF文件是否正常
redis-check-rdb:检测RDB文件是否正常
3、redis-benchmark语法
-h指定服务器的主机名(IP地址)
-p指定服务器的端口号6379
-c指定并发连接数
-n指定请求数
-a指定登录密码


4、登录redis
①方法1:redis-cli -h 20.0.0.14 -p 6379远程登录本机或别的服务器
②方法2:redis-cli直接使用,仅限于本地,远程登录仍需要指定目标服务器的IP地址


|-------------|------------------------------------------|
| redis整个库 ||
| 查看当前库中的键的数量 | DBSIZE |
| 切换库 | SELECT 2 |
| 移动键值到指定库15 | move guoqi1 15 |
| 设置密码 | config set requirepass 123456 |
| 在外声明密码 | redis-cli -h 20.0.0.14 -p 6379 -a 123456 |
| 在内声明密码 | auth 123456 |
①查看当前库中的键的数量DBSIZE

redis默认有16个库,第一个是0,最后一个是15,每个数据库之间互相独立、互不干扰
②切换库SELECT 2

③移动键值到指定库15
move guoqi1 15

④设置密码config set requirepass 123456

在内声明密码auth 123456

在外声明密码redis-cli -h 20.0.0.14 -p 6379 -a 123456


⑤删除当前库FLUSHDB(删库跑路!坐牢)

⑥删除所有库FLUSHALL(删库跑路!坐牢)

5、redis五大数据类型 【重点】
1) string 字符串 类型------string
默认类型,最大存储512MB的数据,可以存储任何数据,比如数字、文字、图片等
|---------------------------|---------------------------------------------|
| string字符串类型 ||
| 查看当前库的键值对 | KEYS * KEYS test KEYS t* KEYS t? KEYS t?? |
| 创建string类型的键值对 | set test guoqi |
| 查看键值对 | get test |
| 判断键是否存在(返回1代表存在;返回0代表不存在) | EXISTS test |
| 删除键 | del test1 test2 |
| 查看键的类型 | type test |
| 追加键值 | append test1 hello |
| 查看键的长度 | STRLEN test1 |
| 自增键值 自减键值 | INCR test1 decr test1 |
| 给键值增加/减少指定数值 | DECRBY test1 3 INCRBY test1 50 |
| 给键值设置过期时间 | SETEX test2 15 lo |
| 对已有键值对修改生命周期 | EXPIRE test 30 |
| 批量设置键值对 | MSET key1 hello key2 world |
| 批量打印多个键值对 | MGET key1 key2 |
| 对键重命名 | RENAME test guoqi |
①查看当前库的键值对KEYS test

②创建键值对set test guoqi
③查看键值对get test



④判断键值对test是否存在EXISTS test

⑤删除键值对del test1 test2

⑥查看键值对的类型type test

⑦追加键值append test1 hello
键已存在,会追加到原有键值后,返回拼接后键值的长度
键不存在,会创建键,直接将键值写入,返回当前键值长度


⑧查看键值长度STRLEN test1

⑨自增键值INCR test1
自减键值 decr test1
注:只能对数字进行操作

⑩给键值增加/减少指定数值
DECRBY test1 3
INCRBY test1 50

⑪给键值设置过期时间 SETEX test2 15 lo

-2表示键值对已过期
-1表示键值对永不过期
⑫对已有键值对修改生命周期EXPIRE test 30

⑬批量设置键值对
MSET key1 hello key2 world
⑭打印多个键值对
duMGET key1 key2

⑮对键重命名RENAME test guoqi

2)list列表类型------string
列表中的元素是string类型
|--------------------------|-----------------------------------------------|
| list列表类型 ||
| 创建list类型的键值对 | lpush guoqi a b c d e f g |
| 查看键值对 | lrange guoqi 0 -1 |
| 查看某个键值对【下标索引】 | lindex guoqi1 0 |
| 查看1-2范围内的键值对 【下标索引,从0开始】 | lrange guoqi1 1 2 |
| 插入数值 | lpush guoqi1 10插入左侧 rpush guoqi1 20插入右侧 |
| 删除数值 | LPOP guoqi1 RPOP guoqi1 |
| 修改数值 | LSET guoqi1 3 shuai (根据下表索引,3代表下表索引) |
| 指定数值前插入 | LINSERT guoqi1 before 3 zhende (3代表数值,不是下表索引) |
①创建列表键值对
lpush guoqi a b c d e f g倒序
rpush guoqi a b c d e f g正序


②查看某个键值对lindex guoqi1 0

③查看1-2范围内的键值对lrange guoqi1 1 2

④插入数值
lpush guoqi1 10在左侧插入
rpush guoqi1 20在右侧插入

⑤删除数值
LPOP guoqi1
RPOP guoqi1

⑥修改数值
LSET guoqi1 3 shuai(根据下表索引,3代表下表索引)

⑦指定数值前插入LINSERT guoqi1 before 3 zhende(3代表数值,不是下表索引)

3 ) hash类型
hash类型用于存储对象,采用hash格式进行操作,占用磁盘空间少,一个hash类型可以存储4294967295个(42亿个)键值对
|--------------|---------------------------------------|
| hash类型 ||
| 创建hsah类型的键值对 | HSET guoqi2 tall yes(tall代表字段类型) |
| 查看键值对 | hget guoqi2 tall |
| 添加新字段 | hset guoqi2 tall yes |
| 删除字段 | hdel guoqi2 hansdon tall |
| 创建多个字段和数值 | hmset guoqi3 tall rich age 23 fcz yes |
| 查询字段 | hgetall guoqi3 |
| 直接查询字段及数值 | hkeys guoqi3 |
| 直接查询数值 | hvals guoqi3 |
| 删除键值对 | del guoqi3 |
①创建hsah类型的键值对
HSET guoqi2 tall yes
②添加新字段hset guoqi2 tall yes

③查看多个字段的内容hmget info name address iphone

④删除字段 hdel info iphone

⑤创建多个字段和数值hmset guoqi3 tall rich age 23 fcz yes

⑥查询字段及数值、直接查询字段、直接查询数值
hgetall guoqi3
hkeys guoqi3
hvals guoqi3

⑦删除键值对del guoqi3

4 ) set 无序集合 类型------string
元素类型也是string,元素是唯一的,不允许重复,多个集合类型可以进行并集、交集、差集进行运算
使用场景:set中的元素类型是唯一的,可以跟踪一些唯一性数据,例如访问微博的用户名,只要把对应名称写入redis,set自动保存唯一性,方便下次访问
|----------------|-------------------|
| set无序集合类型 ||
| 创建set类型的键值对 | SADD myset a c |
| 查看成员 | SMEMBERS myset |
| 查看当前集合中的元素是否存在 | SISMEMBER myset a |
| 随机返回集合中的一个元素 | SRANDMEMBER myset |
| 随机移除 | SPOP myset |
| 指定移除 | SREM myset h k |
①创建set类型键值 SADD myset a c

②查看成员SMEMBERS myset

③查看当前集合中的元素是否存在SISMEMBER myset a

④随机返回集合中的一个元素SRANDMEMBER myset

⑤随机移除SPOP myset

⑥指定移除 SREM myset h k

5 ) zset 有序集合 类型------string
元素类型也是string,元素唯一,不能重复。每个元素都会关联一个double类型(小数点)的分数(score,表示权重),可以通过权重的大小进行排序,元素的权重可以相同
使用场景:在线积分的排行榜,实时更新用户的分数
zrange命令获取积分top10的用户,zrank命令通过username获取用户的排行信息
|-------------------|-------------------------------|
| zset 有序集合 类型 ||
| 创建zset类型的键值 | ZADD myzset 1 one |
| 查看元素和权重 | ZRANGE myzset 0 -1 withscores |
| 查看成员的权重 | ZSCORE myzset two |
| 查询成员数量 | ZCARD myzset |
| 统计权重在1~2范围内的成员数量 | ZCOUNT myzset 1 2 |
| 删除成员 | ZREM myzset three |
| 获取位置索引 | ZRANK myzset one |
①创建zset类型的键值ZADD myzset 1 one

②查看元素和权重ZRANGE myzset 0 -1 withscores

③查看成员的权重ZSCORE myzset two

④获取位置索引 ZRANK myzset one

⑤查询成员数量ZCARD myzset

⑥统计权重在1~2范围内的成员数量ZCOUNT myzset 1 2


⑦删除成员ZREM myzset three

6、普通类型set和hash类型hset该如何选择?
一般情况下,无特殊需求时,普通创建方式即可;但对一个键值进行多字段存储及节省内存可以使用hash方式