nodejs和redis-rejson的使用
npm install redis-rejson之后即可使用rejson,直接上代码,有不明白的地方直接联系我。
const redis = require("redis");
const rejson = require('redis-rejson');
rejson(redis);//将redis的所有包含到rejson中
client = redis.createClient(6379, '127.0.0.1', {});//连接redis服务
client.on("error", function (err) {
console.log("Error " + err);
});
key = '20180727';
value = {
"vxvjm": {
"A4da82795_4d50_46be_8830_106cf12ce45c": {
"ChargeState": {
"NUM": 2,
"DID": "4da82795-4d50-46be-8830-106cf12ce45c"
}
}
}
};
//rejson插入key-value(其他的rejson指令看这里:redis和rejson的安装与使用)
client.json_set(key, '.', JSON.stringify(value), (err, data) => {
if (err) {
console.log("json_set error:" + err);
} else {
console.log("cerate success"+data);
}
});
想要json第归生成如下操作,先看json格式:
{
"PID": {
"DID": {
"RCPNAME": {
"NUM": 1
}
}
}
}
其中所有的参数都是变化生成的。以此为一组数据,我们想要执行一些动态生成json的变化,例如:
{
"PID": {
"DID": {
"RCPNAME": {
"NUM": 1
}
}
},
"PID1": {
"DID1": {
"RCPNAME1": {
"NUM": 1
}
}
}
}
又或者是:
{
"PID": {
"DID": {
"RCPNAME": {
"NUM": 1
}
},
"DID1": {
"RCPNAME1": {
"NUM": 1
}
}
}
}
再或者是:
{
"PID": {
"DID": {
"RCPNAME": {
"NUM": 1
},
"RCPNAME1": {
"NUM": 1
}
}
}
}
再或者是NUM数量变化,这些操作均可set进redis里,直接上代码:
client.json_type(key, '.', (err, obj) => {
if (err) {
cb("check root error! " + err);
} else {
if (!obj) {
Msg("create new root! the key is: " + key);
let newValue = JSON.parse('{"' + PID + '": {"' + DID + '": {"' + RCPNAME + '": {"NUM": 1}}}}');
client.json_set(key, '.', JSON.stringify(newValue), (err, obj) => {
if (err) {
cb("ROOT json_set error: " + err);
} else {
cb(null, "create root success!");
}
});
} else {
client.json_type(key, '.' + PID, (err, obj) => {
if (err) {
cb("check PID error! " + err);
} else {
if (!obj) {
Msg("This PID not exist, create new PID! ");
let pidValue = JSON.parse('{"' + DID + '": {"' + RCPNAME + '": {"NUM": 1}}}');
client.json_set(key, '.' + PID, JSON.stringify(pidValue), (err, obj) => {
if (err) {
cb("PID json_set error: " + err);
} else {
cb(null, "create PID success! ");
}
});
} else {
client.json_type(key, '.' + PID + '.' + DID, (err, obj) => {
if (err) {
cb("check DID error! " + err);
} else {
if (!obj) {
Msg("This DID not exist, create new DID! ");
let didValue = JSON.parse('{"' + RCPNAME + '": {"NUM": 1}}');
client.json_set(key, '.' + PID + '.' + DID, JSON.stringify(didValue), (err, obj) => {
if (err) {
cb("DID json_set error: " + err);
} else {
cb(null, "create DID success!");
}
});
} else {
client.json_type(key, '.' + PID + '.' + DID + '.' + RCPNAME, (err, obj) => {
if (err) {
cb("check RCPNAME error! " + err);
} else {
if (!obj) {
Msg("This RCPNAME not exist, create new RCPNAME! ");
let rcpValue = JSON.parse('{"NUM": 1}');
client.json_set(key, '.' + PID + '.' + DID + '.' + RCPNAME, JSON.stringify(rcpValue), (err, obj) => {
if (err) {
cb("RCPNAME json_set error: " + err);
} else {
cb(null, "create RCPNAME success!");
}
});
} else {
// Msg("This message does exists, do num+1 ! ");
client.json_numincrby(key, '.' + PID + '.' + DID + '.' + RCPNAME + '.NUM', 1, (err, obj) => {
if (err) {
cb("json_numincrby error: " + err);
} else {
console.log(key + '.' + PID + '.' + DID + '.' + RCPNAME + '.NUM:' + obj);
cb(null, key + '.' + PID + '.' + DID + '.' + RCPNAME + '.NUM:' + obj);
}
});
}
}
});
}
}
});
}
}
});
}
}
});