前言
redis8
发布之后,新增了几项特性,其中将RedisJSON支持到redis了,支持了对json的操作了
redis8中的json操作
redis8安装
为了方便,采用docker安装
bash
docker run -d \
--name redis \
--restart=always \
--privileged=true \
-p 6380:6379 \
-v /opt/docker/redis/conf/redis.conf:/etc/redis/redis.conf \
-v /opt/docker/redis/data:/data \
redis:8.0 \
redis-server /etc/redis/redis.conf
本文因为服务器安装了redis了,占用了默认端口6379
,所以映射端口改为6380

命令操作
1、进入到容器内部
bash
docker exec -it 容器id /bin/bash
2、然后执行以下命令进入到redis客户端
redis-cli

redis8语法参考文档
redis8中的json语法参考文档
ruby
https://redis.io/docs/latest/commands/json.set/
JSON.SET
设置一个json值
json
JSON.SET hello $ '{"id":1, "userName": "aaa"}'
输出结果为

往json里面设值值
javascript
JSON.SET hello $.age '8'
输出结果为

JSON.GET
使用JSON.GET
获取json值
javascript
JSON.GET hello
输出结果为

获取json指定值
javascript
JSON.GET hello $.id
输出结果为

JSON.DEL
使用JSON.DEL
删除键值
javascript
JSON.GET hello
输出结果为

删除json中的某个值
javascript
JSON.DEL hello $.userName
输出结果为

JSON.MGET批量获取
使用JSON.MGET
批量获取键值
javascript
JSON.MGET hello hello1 $
输出结果为

JSON.MSET批量插入
json
JSON.MSET test1 $ '{"id":1, "userName": "aaa"}' test2 $ '{"id":1, "userName": "aaa"}'
输出结果为

数组命令
JSON.ARRAPPEND 数组追加元素
使用命令
json
JSON.SET hello1 $ '{"id":1, "userName": "aaa", "role": ["aa", "bb"]}'
javascript
JSON.ARRAPPEND hello1 $.role '"cc"'
输出结果为

JSON.ARRINSERT插入数据
往数组中第二个索引位插入(从0开始计算)
javascript
JSON.ARRINSERT hello1 $.role 2 '"dd"' '"ee"'
输出结果为

JSON.ARRLEN获取数组长度
javascript
JSON.ARRLEN hello1 '$.role'
输出结果为

JSON.ARRPOP移除元素
从数组的索引中移除并返回一个元素
javascript
JSON.ARRPOP hello1 $.role 0
输出结果为

总结
Redis8
中新增了一些实用特性,可以使用试试看