如何使毫秒唯一?

时间:2022-11-25 11:21:08

I'm using NodeJs.

我用NodeJs。

I received constantly request from server. I'm added some variable like createdTime to it and saved to the database. when I sorted data by createdTime in some case It is not reliable, It is Repeated

我经常收到服务器的请求。我添加了一些变量,比如createdTime,并保存到数据库中。当我按创建时间对数据进行排序时,在某些情况下它是不可靠的,它是重复的

How can I make differentiate between them ?

如何区分它们?

  • I do not want to count request.
  • 我不想计算请求。
  • I do not like to change timestamp's format.
  • 我不喜欢改变时间戳的格式。

var createdTime = new Date().getTime();

var createdTime = new Date().getTime();

2 个解决方案

#1


3  

Here's a method of combining a counter with the current time to allow you to have as many as 1000 separate transactions within the same ms that are all uniquely numbered, but still a time-based value.

这里有一种将计数器与当前时间相结合的方法,允许您在相同的ms中拥有多达1000个独立的事务,这些事务都是惟一编号的,但仍然是基于时间的值。

And, here's a working snippet to illustrate:

这里有一个工作片段来说明:

// this guarantees a unique time-based id
// as long as you don't have more than 1000
// requests in the same ms
var getTransactionID = (function() {
  var lastTime, counter = 0;
  return function() {
    var now = Date.now();
    if (now !== lastTime) {
      lastTime = now;
      counter = 0;
    } else {
      ++counter;
    }
    return (now * 1000) + counter;
  }
})();

for (var i = 0; i < 100; i++) {
    document.write(getTransactionID() + "<br>");
}

If you want something that is likely to work across clusters, you can use process.hrtime() to use the high resolution timer instead of the counter and then make the id be a string that could be parsed into a relative time if needed. Since this requires node.js, I can't make a working snippet here in the browser, but here's the idea:

如果您想要跨集群工作的东西,可以使用process.hrtime()来使用高分辨率计时器而不是计数器,然后将id设置为一个字符串,如果需要,可以将其解析为相对时间。因为这需要节点。js,我无法在浏览器中创建一个可用的代码片段,但我的想法是:

// this makes a unique time-based id
function getTransactionID () {
    var now = Date.now();
    var hrtime = process.hrtime();
    return now + "." + ((hrtime[0] * 1e9) + hrtime[1]);
}

#2


1  

Due to my low rep I can't add a comment but it looks like you are needing to go beyond milliseconds.Maybe this * question can help you

由于我的低代表,我不能添加评论,但看起来您需要超越毫秒。也许这个*问题可以帮助你

How to get a microtime in Node.js?

如何在Node.js中获得微时间?

#1


3  

Here's a method of combining a counter with the current time to allow you to have as many as 1000 separate transactions within the same ms that are all uniquely numbered, but still a time-based value.

这里有一种将计数器与当前时间相结合的方法,允许您在相同的ms中拥有多达1000个独立的事务,这些事务都是惟一编号的,但仍然是基于时间的值。

And, here's a working snippet to illustrate:

这里有一个工作片段来说明:

// this guarantees a unique time-based id
// as long as you don't have more than 1000
// requests in the same ms
var getTransactionID = (function() {
  var lastTime, counter = 0;
  return function() {
    var now = Date.now();
    if (now !== lastTime) {
      lastTime = now;
      counter = 0;
    } else {
      ++counter;
    }
    return (now * 1000) + counter;
  }
})();

for (var i = 0; i < 100; i++) {
    document.write(getTransactionID() + "<br>");
}

If you want something that is likely to work across clusters, you can use process.hrtime() to use the high resolution timer instead of the counter and then make the id be a string that could be parsed into a relative time if needed. Since this requires node.js, I can't make a working snippet here in the browser, but here's the idea:

如果您想要跨集群工作的东西,可以使用process.hrtime()来使用高分辨率计时器而不是计数器,然后将id设置为一个字符串,如果需要,可以将其解析为相对时间。因为这需要节点。js,我无法在浏览器中创建一个可用的代码片段,但我的想法是:

// this makes a unique time-based id
function getTransactionID () {
    var now = Date.now();
    var hrtime = process.hrtime();
    return now + "." + ((hrtime[0] * 1e9) + hrtime[1]);
}

#2


1  

Due to my low rep I can't add a comment but it looks like you are needing to go beyond milliseconds.Maybe this * question can help you

由于我的低代表,我不能添加评论,但看起来您需要超越毫秒。也许这个*问题可以帮助你

How to get a microtime in Node.js?

如何在Node.js中获得微时间?