YouTube-like GUID

匿名 (未验证) 提交于 2019-12-03 01:06:02

问题:

Is it possible to generate short GUID like in YouTube (N7Et6c9nL9w)?

How can it be done? I want to use it in web app.

回答1:

You could use Base64:

string base64Guid = Convert.ToBase64String(Guid.NewGuid().ToByteArray()); 

That generates a string like E1HKfn68Pkms5zsZsvKONw==. Since a GUID is always 128 bits, you can omit the == that you know will always be present at the end and that will give you a 22 character string. This isn't as short as YouTube though.



回答2:

9 chars is not a Guid. Given that, you could use the hexadecimal representation of an int, which gives you a 8 char string.

Update 1: Dunno why the above got a downvote, but for anyone wondering:

You can use an id you might already have. Also you can use .GetHashCode against different simple types and there you have a different int. You can also xor different fields. And if you are into it, you might even use a Random number - hey, you have well above 2.000.000.000+ possible values if you stick to the positives ;)



回答3:

Like mentioned in the accepted answer, it can make problems if you're using the GUID in the URL. Here is a more complete answer:

    public string ToShortString(Guid guid)     {         var base64Guid = Convert.ToBase64String(guid.ToByteArray());          // Replace URL unfriendly characters with better ones         base64Guid = base64Guid.Replace('+', '-').Replace('/', '_');          // Remove the trailing ==         return base64Guid.Substring(0, base64Guid.Length - 2);     }      public Guid FromShortString(string str)     {         str = str.Replace('_', '/').Replace('-', '+');         var byteArray = Convert.FromBase64String(str + "==");         return new Guid(byteArray);     } 

Usage:

        var guid = Guid.NewGuid();         var shortStr = ToShortString(guid);         // shortStr will look something like 2LP8GcHr-EC4D__QTizUWw         var guid2 = FromShortString(shortStr);         Assert.AreEqual(guid, guid2); 


回答4:

As others have mentioned, YouTube's VideoId is not technically a GUID since it's not inherently unique.

As per Wikipedia:

The total number of unique keys is 2128 or 3.4×1038. This number is so large that the probability of the same number being generated randomly twice is negligible.

The uniqueness YouTube's VideoId is maintained by their generator algorithm.

You can either write your own algorithm, or you can use some sort of random string generator and utilize the UNIQUE CONSTRAINT constraint in SQL to enforce its uniqueness.

First, create a UNIQUE CONSTRAINT in your database:

ALTER TABLE MyTable ADD CONSTRAINT UniqueUrlId UNIQUE (UrlId); 

Then, for example, generate a random string (from philipproplesch's answer):

string shortUrl = System.Web.Security.Membership.GeneratePassword(11, 0); 

If the generated UrlId is sufficiently random and sufficiently long you should rarely encounter the exception that is thrown when SQL encounters a duplicate UrlId. In such an event, you can easily handle the exception in your web app.



回答5:

Technically it's not a Guid. Youtube has a simple randomized string generator that you can probably whip up in a few minutes using an array of allowed characters and a random number generator.



回答6:

It might be not the best solution, but you can do something like that:

string shortUrl = System.Web.Security.Membership.GeneratePassword(11, 0); 


回答7:

This id is probably not globally unique. GUID's should be globally unique as they include elements which should not occur elsewhere (the MAC address of the machine generating the ID, the time the ID was generated, etc.)

If what you need is an ID that is unique within your application, use a number fountain - perhaps encoding the value as a hexadecimal number. Every time you need an id, grab it from the number fountain.

If you have multiple servers allocating id's, you could grab a range of numbers (a few tens or thousands depending on how quickly you're allocating ids) and that should do the job. an 8 digit hex number will give you 4 billion ids - but your first id's will be a lot shorter.



文章来源: YouTube-like GUID
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!