用node序列化redis数据
我正在使用 Redis 和 Node(使用 node_redis),我想将 Redis 中的数据序列化为 XML 文件(使用simple-xml-writer),但我偶然发现了节点的异步行为。
我有 a、b、c、d 和 e 的数据集作为哈希存储在 Redis 中,键是 data:a、data:b data:c ...,每个键访问一个哈希。现在我的 XML 文件应该如下所示:
<root>
<data record="a">
(data for a)
</data>
<data record="b">
(data for b)
</data>
...
</root>
我的方法是执行以下操作:
myobjects = Array.new();
["a","b","c","d","e"].forEach(function(str) {
database.hmget("data:" + str,function(err,obj){ myobjects.push(obj) });
});
now_serialize_myobjects();
是否可以等待 forEach
循环完成并确保 < 中的所有对象code>database.hmget() 被存储?那么当调用函数 now_serialize_myobjects()
时,所有对象都被覆盖了吗?
解决这个问题的最佳方法是什么?
I am using redis and node (with node_redis) and I would like to serialize data from redis into an XML file (using simple-xml-writer), but I stumble upon the asynchronous behavior of node.
I have datasets for a,b,c,d and e stored as a hash in redis, the keys are data:a, data:b data:c ... and each key accesses a hash. Now my XML file should look like this:
<root>
<data record="a">
(data for a)
</data>
<data record="b">
(data for b)
</data>
...
</root>
My approach is to do something like:
myobjects = Array.new();
["a","b","c","d","e"].forEach(function(str) {
database.hmget("data:" + str,function(err,obj){ myobjects.push(obj) });
});
now_serialize_myobjects();
Is it possible to wait for the forEach
-loop to finish and be sure all objects in database.hmget()
are stored? So when calling the function now_serialize_myobjects()
, all objects covered?
What is the best approach to that problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法
但是为什么不在请求返回时序列化呢?
如果你要加载一大堆东西,我认为第二个更好
Simplest approach
But why not serialize as requests return?
if you are loading a ton of stuff second one is better I think