生成的可读/可用,简短而唯一的ID [英] Generating human-readable/usable, short but unique IDs
问题描述
-
需要处理> 1000,但是<每天10000新记录
-
不能使用GUID / UUID的,自动递增的数字等。
-
在理想情况下应该是5或6个字符长,当然可以阿尔法
-
想重用现有的,知名的交易算法,如果有
什么了吗?
解决方案基地62使用TinyURL的和bit.ly的缩略网址
基地62唯一指标
5个字符的基地62会给你62 ^ 5唯一的ID = 916132832(约1十亿) 在每天的10K的ID,你将确定为91K +天
6个字符的基地62会给你62 ^ 6唯一的ID = 56800235584(56+十亿) 在每天的10K的ID,你将确定为5+亿天
基地36唯一指标
6个字符会给你36 ^ 6唯一的ID = 2176782336(2 +十亿)
7个字符会给你36 ^ 7唯一的ID = 78364164096(78+十亿)
code:
公共无效TestRandomIdGenerator()
{
//创建6五个标识,基地62个字符
的for(int i = 0;我小于5;我++)Console.WriteLine(RandomIdGenerator.GetBase62(6));
//创建八个基地36个字符5标识
的for(int i = 0;我小于5;我++)Console.WriteLine(RandomIdGenerator.GetBase36(8));
}
公共静态类RandomIdGenerator
{
私有静态的char [] _base62chars =
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
.ToCharArray();
私有静态随机_random =新的随机();
公共静态字符串GetBase62(INT长度)
{
VAR SB =新的StringBuilder(长度);
的for(int i = 0; I<长度;我++)
sb.Append(_base62chars [_random.Next(62)]);
返回sb.ToString();
}
公共静态字符串GetBase36(INT长度)
{
VAR SB =新的StringBuilder(长度);
的for(int i = 0; I<长度;我++)
sb.Append(_base62chars [_random.Next(36)]);
返回sb.ToString();
}
}
输出:
z5KyMg wd4SUp uSzQtH UPrGAT UIf2IS QCF9GNM5 0UV3TFSS 3MG91VKP 7NTRF10T AJK3AJU7
Need to handle > 1000 but < 10000 new records per day
Cannot use GUID/UUIDs, auto increment numbers etc.
Ideally should be 5 or 6 chars long, can be alpha of course
Would like to reuse existing, well-known algos, if available
Anything out there ?
Base 62 is used by tinyurl and bit.ly for the abbreviated URLs. It's a well-understood method for creating "unique", human-readable IDs. Of course you will have to store the created IDs and check for duplicates on creation to ensure uniqueness. (See code at bottom of answer)
Base 62 uniqueness metrics
5 chars in base 62 will give you 62^5 unique IDs = 916,132,832 (~1 billion) At 10k IDs per day you will be ok for 91k+ days
6 chars in base 62 will give you 62^6 unique IDs = 56,800,235,584 (56+ billion) At 10k IDs per day you will be ok for 5+ million days
Base 36 uniqueness metrics
6 chars will give you 36^6 unique IDs = 2,176,782,336 (2+ billion)
7 chars will give you 36^7 unique IDs = 78,364,164,096 (78+ billion)
Code:
public void TestRandomIdGenerator()
{
// create five IDs of six, base 62 characters
for (int i=0; i<5; i++) Console.WriteLine(RandomIdGenerator.GetBase62(6));
// create five IDs of eight base 36 characters
for (int i=0; i<5; i++) Console.WriteLine(RandomIdGenerator.GetBase36(8));
}
public static class RandomIdGenerator
{
private static char[] _base62chars =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
.ToCharArray();
private static Random _random = new Random();
public static string GetBase62(int length)
{
var sb = new StringBuilder(length);
for (int i=0; i<length; i++)
sb.Append(_base62chars[_random.Next(62)]);
return sb.ToString();
}
public static string GetBase36(int length)
{
var sb = new StringBuilder(length);
for (int i=0; i<length; i++)
sb.Append(_base62chars[_random.Next(36)]);
return sb.ToString();
}
}
Output:
z5KyMg wd4SUp uSzQtH UPrGAT UIf2IS QCF9GNM5 0UV3TFSS 3MG91VKP 7NTRF10T AJK3AJU7
这篇关于生成的可读/可用,简短而唯一的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!