用node序列化redis数据

发布于 2024-12-26 18:04:18 字数 928 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〃温暖了心ぐ 2025-01-02 18:04:18

最简单的方法

myobjects = Array.new();
var something = ["a","b","c","d","e"]; 
something.forEach(function(str) {
   database.hmget("data:" + str,function(err,obj){ 
     myobjects.push(obj);
     if(myobjects.length === something.length){
        now_serialize_myobjects();
     }
 });
});

但是为什么不在请求返回时序列化呢?

 startxmlfile();
 var something = ["a","b","c","d","e"]; 
 var completionCounter = 0;
 something.forEach(function(str) {
   database.hmget("data:" + str,function(err,obj){ 
     completionCounter++;
     if (!err)
        serialize(obj);
     if (completionCounter ===something.length)
        finalizexmlfile();

 });
});

如果你要加载一大堆东西,我认为第二个更好

Simplest approach

myobjects = Array.new();
var something = ["a","b","c","d","e"]; 
something.forEach(function(str) {
   database.hmget("data:" + str,function(err,obj){ 
     myobjects.push(obj);
     if(myobjects.length === something.length){
        now_serialize_myobjects();
     }
 });
});

But why not serialize as requests return?

 startxmlfile();
 var something = ["a","b","c","d","e"]; 
 var completionCounter = 0;
 something.forEach(function(str) {
   database.hmget("data:" + str,function(err,obj){ 
     completionCounter++;
     if (!err)
        serialize(obj);
     if (completionCounter ===something.length)
        finalizexmlfile();

 });
});

if you are loading a ton of stuff second one is better I think

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文