在我们的平台上,我们会将课程所有需要的环境配置好,30分钟速成技术
https://www.guoyasoft.com/
感兴趣的可以在文末联系我
这篇文章详细讲解了如何使用AnotherRedis、navicat以及命令行客户端如何对Redis键值存储数据库中的各种数据进行增删改查操作
环境要求
一台预装好Redis的服务器,本地自行部署也可以
以下均以公网服务器为例,服务器配置如下
一、AnotherRedisDesktop客户端
1.连接数据库
新建连接


3.增删改查String数据
一、增
场景一:手机发送验证码
key=sms:code:13800138000
value=123456
1.新建String数据(text)


2.新建String数据(Json)
场景二:存储商品信息
key=product:hot:1001
value={\"id\":1001,\"name\":\"iPhone14\",\"price\":6999}

访问网站格式化json数据
https://www.bejson.com/

填写数据

特点:存储类型:字符串、整数、浮点数、二进制数据(如图片序列化) 最大容量:512MB(比Memcached的1MB大很多)
二、查
1.查看所有数据

2.查String数据

三、改

四、删(自动删除)
1.设置过期时间

2.确定修改后开始计时

3.自动删除

4.刷新后数据消失

4.增删改查List数据
一、增
场景:订单队列
key=task:queue:urgent
value1=process_order_12345
value2=process_order_12346

1.删除默认数据
在 Another Redis Desktop Manager (ARDM) 中,当你新建一个 Redis List 时,默认会包含一个值为 “New member” 的成员,这是该工具的默认行为,主要是为了方便用户快速开始操作。

2.新增数据
属性 | 值 |
---|---|
key | task:queue:urgent |
value1 | process_order_12345 |


属性 | 值 |
---|---|
key | task:queue:urgent |
value2 | process_order_12346 |


特点:一个key对应多个有序value( 顺序固定 )(可以重复)(可以双端操作)
二、查数据
1.查所有数据

2.查List数据

三、改

四、删除(自动删除)
1.设置过期时间

2.确定修改后开始计时

3.自动删除

4.刷新后数据消失

5.增删改查Hash数据
一、增
场景:添加人物信息
key=user:profile:1001
key1=name value1=张三
key2=age value2=30
key3=email value3=zhangsan@example.com
1.尝试切换数据库

2.新建Hash列表

3.删除默认数据

4.新增数据

属性 | 值 |
---|---|
key1 | name |
value1 | 张三 |

属性 | 值 |
---|---|
key2 | age |
value2 | 30 |

属性 | 值 |
---|---|
key3 | |
value3 | zhangsan@example.com |


特点:一个key对应多个字段-value对象(无序)(字段唯一)(适合存储对象)
二、查数据
1.查看所有数据

2.查Hash数据

只搜索1001对应的key有三个,查出来多个数据,所以要搜索完整的key保证数据准确性,这里体现了前缀的重要性

三、改

四、删(自动删除)
1.设置过期时间

2.自动删除

6.增删改查Set数据
一、增
场景:用户标签管理
key ="user:tags:1001";
value1 = "vip"
value2 = "active"
value3 = "new"
1.新建Set集合

2.删除默认数据

3.新增数据
属性 | 值 |
---|---|
value1 | vip |

属性 | 值 |
---|---|
value2 | active |

属性 | 值 |
---|---|
value3 | new |


特点:一个key对应多个唯一value (无序)(不允许重复)(可以求交集/并集)
二、查数据
1.查看所有数据

2.查Set数据

三、改Set数据

四、删(自动删除)
1.设置过期时间

2.自动删除

7.增删改查SortedSet数据
一、增
场景:商品排行榜
key=product:ranking
value=product:1001
score=4.8
key=product:ranking
value=product:1002
score=4.5
key=product:ranking
value=product:1003
score=4.7
1.新建Sorted Set集合

2.删除默认数据

3.新增数据
属性 | 值 |
---|---|
key | product:ranking |
value | product:1001 |
score | 4.8 |

属性 | 值 |
---|---|
key | product:ranking |
value | product:1002 |
score | 4.5 |

属性 | 值 |
---|---|
key | product:ranking |
value | product:1003 |
score | 4.7 |


特点:每个value附带排序分数score (按分数排序)(不允许重复)(可以范围查询)
二、查数据
1.查看所有数据

2.查Sorted Set数据

三、改

四、删(自动删除)
1.设置过期时间

2.自动删除

8.手工删除
手动删除


二、navicat客户端
1.连接navicat
连接navicat


2.增删改查String数据
String类型
1.增
SET sms:code:13800138000 123456

2.查
GET sms:code:13800138000

3. 改
改(直接 SET 新值)
SET sms:code:13800138000 654321

4.删
DEL sms:code:13800138000

5.再查
GET sms:code:13800138000

3.增删改查List数据
List类型
# 增(从左压入)
LPUSH task:queue:urgent process_order_12345
LPUSH task:queue:urgent process_order_12346
# 查(获取全部列表)
LRANGE task:queue:urgent 0 -1
# 改(修改指定下标的值)
LSET task:queue:urgent 0 process_order_99999
# 删(删除最右边的一个任务)
RPOP task:queue:urgent
# 删除整个列表
DEL task:queue:urgent
1.增
增(从左压入)
LPUSH task:queue:urgent process_order_12345
LPUSH task:queue:urgent process_order_12346

2.查
查(获取全部列表)
LRANGE task:queue:urgent 0 -1

3.改(先查再改)
改(修改指定下标的值)
LSET task:queue:urgent 0 process_order_99999


4.删
# 删(删除最右边的一个任务)
RPOP task:queue:urgent

5.再查
# 查(获取全部列表)
LRANGE task:queue:urgent 0 -1

4.增删改查Hash数据
Hash类型
# 增 / 改(HSET)
HSET user:profile:1001 name "张三"
HSET user:profile:1001 age 30
HSET user:profile:1001 email "zhangsan@example.com"
# 查(获取所有字段)
HGETALL user:profile:1001
# 查单个字段
HGET user:profile:1001 name
# 改(继续 HSET)
HSET user:profile:1001 name "李四"
# 删除一个字段
HDEL user:profile:1001 email
# 删除整个 hash
DEL user:profile:1001
1.增
增 / 改(HSET)
HSET user:profile:1001 name "张三"
HSET user:profile:1001 age 30
HSET user:profile:1001 email "zhangsan@example.com"

2.查(查所有、查单个字段)
查(获取所有字段)
HGETALL user:profile:1001

查单个字段
HGET user:profile:1001 name

3.改(先改再查)
改(继续 HSET)
HSET user:profile:1001 name "李四"

查(获取所有字段)
HGETALL user:profile:1001

4.删
删除一个字段
HDEL user:profile:1001 email

5.再查
查(获取所有字段)
HGETALL user:profile:1001

5.增删改查Set数据
Set类型
# 增
SADD user:tags:1001 vip active new
# 查(所有元素)
SMEMBERS user:tags:1001
# 改(先删再加)
SREM user:tags:1001 new
SADD user:tags:1001 loyal
# 删(删除整个 Set)
DEL user:tags:1001
1.增
SADD user:tags:1001 vip active new

2.查
查(所有元素)
SMEMBERS user:tags:1001

3.改(先改再查)
先改(先删再加)
SREM user:tags:1001 new
SADD user:tags:1001 loyal

再查
SMEMBERS user:tags:1001

4.删(先删再查)
删(删除整个 Set)
DEL user:tags:1001

查(所有元素)
SMEMBERS user:tags:1001

6.增删改查SortedSet数据
Sorted Set类型
# 增(ZADD 分数 成员)
ZADD product:ranking 4.8 product:1001
ZADD product:ranking 4.5 product:1002
ZADD product:ranking 4.7 product:1003
# 查(按分数降序)
ZREVRANGE product:ranking 0 -1 WITHSCORES
# 改(修改 product:1002 的分数为 4.9)
ZADD product:ranking 4.9 product:1002
# 删除某个成员
ZREM product:ranking product:1001
# 删除整个 ZSet
DEL product:ranking
1.增
增(ZADD 分数 成员)
ZADD product:ranking 4.8 product:1001
ZADD product:ranking 4.5 product:1002
ZADD product:ranking 4.7 product:1003

2.查
查(按分数降序)
ZREVRANGE product:ranking 0 -1 WITHSCORES

3.改
改(修改 product:1002 的分数为 4.9)
ZADD product:ranking 4.9 product:1002

4.删(先删再查)
删除某个成员
ZREM product:ranking product:1001
查(按分数降序)
ZREVRANGE product:ranking 0 -1 WITHSCORES

删除整个 Set
DEL product:ranking
查(按分数降序)
ZREVRANGE product:ranking 0 -1 WITHSCORES

三、命令行客户端
1.命令行客户端连接redis
代码示例
代码块分开
docker ps //展示容器
docker ps | grep redis //筛选出redis
docker exec -it 67ad //67ad改成容器id前四位
redis-cli -a com.guoyasoft
或者
docker exec -it 67ad redis-cli -a com.guoyasoft //67ad改成容器id前四位
操作示例
docker ps //展示容器

docker ps | grep redis //筛选出redis

docker exec -it 24ae67d26eff redis-cli -a com.guoyasoft //24ae67d26eff改成容器id前四位

String codeKey = "sms:code:13800138000";
String verificationCode = "123456";
代码示例
SET sms:code:13800138000 123456 增
GET sms:code:13800138000 查
SET sms:code:13800138000 654321 改
DEL sms:code:13800138000 删
操作示例
1. 增
SET sms:code:13800138000 123456
查
GET sms:code:13800138000

2.改
SET sms:code:13800138000 654321
GET sms:code:13800138000

3.删
DEL sms:code:13800138000
GET sms:code:13800138000

3.增删改查List数据
原Java语句
String taskQueueKey = "task:queue:urgent";
String newTask1 = "process_order_12345";
String newTask2 = "process_order_12346";
代码示例
# 增(从左侧压入队列)
LPUSH task:queue:urgent process_order_12345
LPUSH task:queue:urgent process_order_12346
# 查(获取整个列表)
LRANGE task:queue:urgent 0 -1
# 改(列表不能直接改,用 LSET 改指定下标)
LSET task:queue:urgent 0 process_order_00000
# 删(从右侧弹出一个任务,模拟处理)
RPOP task:queue:urgent
# 删整个队列
DEL task:queue:urgent
操作示例
1. 增
增(从左侧压入队列)
LPUSH task:queue:urgent process_order_12345
LPUSH task:queue:urgent process_order_12346
LRANGE task:queue:urgent 0 -1

2.改
改(列表不能直接改,用 LSET 改指定下标)
LSET task:queue:urgent 0 process_order_00000
查(获取整个列表)
LRANGE task:queue:urgent 0 -1

3.删
删(从右侧弹出一个任务,模拟处理)
RPOP task:queue:urgent
查(获取整个列表)
LRANGE task:queue:urgent 0 -1
删整个队列
DEL task:queue:urgent
查(获取整个列表)
LRANGE task:queue:urgent 0 -1

4.增删改查Hash数据
原Java语句
String userKey = "user:profile:1001";
put(userKey, "name", "张三");
put(userKey, "age", 30);
put(userKey, "email", "zhangsan@example.com");
代码示例
# 增(或改)字段
HSET user:profile:1001 name "张三"
HSET user:profile:1001 age 30
HSET user:profile:1001 email "zhangsan@example.com"
# 查(所有字段)
HGETALL user:profile:1001
# 查单个字段
HGET user:profile:1001 email
# 改(直接用 HSET)
HSET user:profile:1001 name "李四"
# 删字段
HDEL user:profile:1001 email
操作示例
1. 增
增(或改)字段
HSET user:profile:1001 name "张三"
HSET user:profile:1001 age 30
HSET user:profile:1001 email "zhangsan@example.com"
查(所有字段)
HGETALL user:profile:1001

2.查
查单个字段
HGET user:profile:1001 email

3.改
改(直接用 HSET)
HSET user:profile:1001 age 50
查单个字段
HGET user:profile:1001 age

4.删
删字段
HDEL user:profile:1001 email
查单个字段
HGET user:profile:1001 email

5.增删改查Set数据
原Java语句
String userTagsKey = "user:tags:1001";
add(userTagsKey, "vip", "active", "new");
代码示例
# 增(添加多个元素)
SADD user:tags:1001 vip active new
# 查(获取所有标签)
SMEMBERS user:tags:1001
# 改(先删后加)
SREM user:tags:1001 new
SADD user:tags:1001 loyal
# 删(删除整个 set)
DEL user:tags:1001
操作示例
1. 增
增(添加多个元素)
SADD user:tags:1001 vip active new
查(获取所有标签)
SMEMBERS user:tags:1001

2.改
改(先删后加)
SREM user:tags:1001 new
SADD user:tags:1001 loyal
查(获取所有标签)
SMEMBERS user:tags:1001

3.删
删(删除整个 set)
DEL user:tags:1001
查(获取所有标签)
SMEMBERS user:tags:1001

6.增删改查SortedSet数据
原Java语句
String rankingKey = "product:ranking";
add(rankingKey, "product:1001", 4.8);
add(rankingKey, "product:1002", 4.5);
add(rankingKey, "product:1003", 4.7);
代码示例
# 增(或更新分数)
ZADD product:ranking 4.8 product:1001
ZADD product:ranking 4.5 product:1002
ZADD product:ranking 4.7 product:1003
# 查(按分数降序排名)
ZREVRANGE product:ranking 0 -1 WITHSCORES
# 查某个成员的排名(从 0 开始)
ZREVRANK product:ranking product:1002
# 改(更新某个商品分数)
ZADD product:ranking 4.9 product:1002
# 删
ZREM product:ranking product:1001
操作示例
1. 增
增(或更新分数)
ZADD product:ranking 4.8 product:1001
ZADD product:ranking 4.5 product:1002
ZADD product:ranking 4.7 product:1003
查(按分数降序排名)
ZREVRANGE product:ranking 0 -1 WITHSCORES

2.查排名
查某个成员的排名(从 0 开始)
ZREVRANK product:ranking product:1002

3.改
改(更新某个商品分数)
ZADD product:ranking 4.9 product:1002
查(按分数降序排名)
ZREVRANGE product:ranking 0 -1 WITHSCORES

4.删
删
ZREM product:ranking product:1001
查(按分数降序排名)
ZREVRANGE product:ranking 0 -1 WITHSCORES

以上为全部内容
我们致力于制作最好的跟练教程,欢迎大家加入学习!!!
有任何问题欢迎联系!!
v×:WanCC2001