前言
redis8发布之后,新增了几项特性,其中将RedisJSON支持到redis了,支持了对json的操作了
redis8中的json操作
redis8安装
为了方便,采用docker安装
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、进入到容器内部
docker exec -it 容器id /bin/bash
2、然后执行以下命令进入到redis客户端
redis-cli
redis8语法参考文档
redis8中的json语法参考文档
https://redis.io/docs/latest/commands/json.set/
JSON.SET
设置一个json值
JSON.SET hello $ '{"id":1, "userName": "aaa"}'
输出结果为
往json里面设值值
JSON.SET hello $.age '8'
输出结果为
JSON.GET
使用JSON.GET获取json值
JSON.GET hello
输出结果为
获取json指定值
JSON.GET hello $.id
输出结果为
JSON.DEL
使用JSON.DEL删除键值
JSON.GET hello
输出结果为
删除json中的某个值
JSON.DEL hello $.userName
输出结果为
JSON.MGET批量获取
使用JSON.MGET批量获取键值
JSON.MGET hello hello1 $
输出结果为
JSON.MSET批量插入
JSON.MSET test1 $ '{"id":1, "userName": "aaa"}' test2 $ '{"id":1, "userName": "aaa"}'
输出结果为
数组命令
JSON.ARRAPPEND 数组追加元素
使用命令
JSON.SET hello1 $ '{"id":1, "userName": "aaa", "role": ["aa", "bb"]}'
JSON.ARRAPPEND hello1 $.role '"cc"'
输出结果为
JSON.ARRINSERT插入数据
往数组中第二个索引位插入(从0开始计算)
JSON.ARRINSERT hello1 $.role 2 '"dd"' '"ee"'
输出结果为
JSON.ARRLEN获取数组长度
JSON.ARRLEN hello1 '$.role'
输出结果为
JSON.ARRPOP移除元素
从数组的索引中移除并返回一个元素
JSON.ARRPOP hello1 $.role 0
输出结果为
总结
Redis8中新增了一些实用特性,可以使用试试看