use*_*781 5 redis node.js node-redis
我开始学习 redis 和 nodejs ,我从第三方 api 调用获取数据。我想将此数据保存到我的 redis 服务器中。我能够做到这一点,但我遇到了一个问题,如果我在具有多个键的 json 数组中获取数据,我将无法插入它。我该如何解决这个问题?
我的数据是:
keys
[123,3435,455,455]
value
[{name:'shakti'},{name:'amit'},{name:'amiit'},{name:'sad'}]
Run Code Online (Sandbox Code Playgroud)
我想立即将这些数据保存为键值形式,而不使用任何 for 循环。目前我正在使用 for 循环
for(var i=0i<keys.length;;i++){
redisClient.set(keys[i],data);
}
Run Code Online (Sandbox Code Playgroud)
请帮我解决这个问题。谢谢
我在我的应用程序中做了同样的事情。我试图通过 unique 来保存每条记录placeId。我的记录是
[
{
"name": "Ranchi Railway Station",
"placeId": "RAIL001",
"category": "railway",
"latitude": 3.09072,
"longitude": 101.70076
},
{
"name": "Delhi Airport",
"placeId": "AIR129",
"category": "airport",
"latitude": 4.09072,
"longitude": 100.70076
}
]
Run Code Online (Sandbox Code Playgroud)
我想通过使用placeIdredis 立即保存从查询中获取的每条记录。我使用以下方法来缓存这些记录。
const placesRecords = {};
places.forEach((element) => {
placesRecords[element.placeId] = JSON.stringify(element);
});
redisClient.mset(placesRecords, (err, reply) => {
if(err) {
console.log(" err: " + err);
} else {
console.log(" reply: " + reply);
}
});
Run Code Online (Sandbox Code Playgroud)
使用MSET。在节点js中:
var keys = [123,3435,455,455]
var values = [{name:'shakti'},{name:'amit'},{name:'amiit'},{name:'sad'}]
var arr = [];
for (var i = 0; i < keys.length; i++) {
arr.push(keys[i]);
arr.push(JSON.stringify(values[i]));
}
client.mset(arr, function(err, reply) {
console.log(" reply: " + reply);
console.log(" err: " + err);
client.quit();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2137 次 |
| 最近记录: |