仅由数字组成的唯一 ID [英] Unique id consisting of only numbers
问题描述
我需要获得基于时间的唯一 id,它只包含数字,我想简单地使用这样的东西:
I need to get time based unique id which would only consist of numbers, i was thinking to simply use something like this:
str_replace(".", "", microtime(true))
我会在很多事情上使用这个 id,例如发表评论,我预计会有相当高的流量,所以我想知道与 microtime
函数发生冲突的几率有多大?
I would use this id for many things, for example comments posting, and i'm expecting quite high traffic so i would like to know how high are the chances of collision with microtime
function?
也许有更好的方法来获取数字唯一 ID?请记住,它必须基于时间,以便进行排序.
And perhaps there is a better way to get numerical unique id? Just remember that it has to based on time so sorting could be done.
推荐答案
如果您需要使用纯基于时间的 ID,您需要确保避免冲突,因此 a) 使用数据库中的主键,然后 b) 使用递归检查以确保您的基于时间的 ID 尚未被占用:
If you need to use a pure time based ID you need to ensure that you avoid collisions so a) use a primary key in the database and then b) use a recursive check to ensure that your time based ID has not already been taken:
$id = getTimeId();
function getTimeId() {
// Generate microtime
// Query to ensure it does not exist
// Return microtime
}
我将把它留给你好先生来享受递归.
I shall leave it upto you good sir to enjoy making it recursive.
这篇关于仅由数字组成的唯一 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!