Ruby on Rails - generating bit.ly style uuids

后端 未结 4 1848
天涯浪人
天涯浪人 2021-02-10 12:54

I\'m trying to generate UUIDs with the same style as bit.ly urls like:

http://bit.ly/aUekJP

or cloudapp ones:

http://cl.ly/1hVU         


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-10 13:23

    You are confusing two different things here. A UUID is a universally unique identifier. It has a very high probability of being unique even if millions of them were being created all over the world at the same time. It is generally displayed as a 36 digit string. You can not chop off the first 8 characters and expect it to be unique.

    Bitly, tinyurl et-al store links and generate a short code to represent that link. They do not reconstruct the URL from the code they look it up in a data-store and return the corresponding URL. These are not UUIDS.

    Without knowing your application it is hard to advise on what method you should use, however you could store whatever you are pointing at in a data-store with a numeric key and then rebase the key to base32 using the 10 digits and 22 lowercase letters, perhaps avoiding the obvious typo problems like 'o' 'i' 'l' etc

    EDIT

    On further investigation there is a Ruby base32 gem available that implements Douglas Crockford's Base 32 implementation

    A 5 character Base32 string can represent over 33 million integers and a 6 digit string over a billion.

提交回复
热议问题