Creating your own Tinyurl style uid

前端 未结 8 1953
抹茶落季
抹茶落季 2020-12-02 11:30

I\'m writing a small article on humanly readable alternatives to Guids/UIDs, for example those used on TinyURL for the url hashes (which are often printed in magazines, so n

8条回答
  •  离开以前
    2020-12-02 12:10

    Some time ago I did exactly this, and I followed the way Sklivvz mentioned. The whole logic was developed with a SQL server stored procedure and a couple of UDF (user defined functions). The steps were:

    • say that you want to shorten this url: Creating your own Tinyurl style uid
    • Insert the URL in a table
    • Obtain the @@identity value of the last insert (a numeric id)
    • Transform the id in a corresponding alphanumeric value, based on a "domain" of letters and numbers (I actually used this set: "0123456789abcdefghijklmnopqrstuvwxyz")
    • Return that value back, something like 'cc0'

    The conversion was realized thru a couple of very short UDF.

    Two conversion called one after the other would return "sequential" values like these:

    select dbo.FX_CONV (123456) -- returns "1f5n"
    
    select dbo.FX_CONV (123457) -- returns "1f5o"
    

    If you are interested I can share the UDF's code.

提交回复
热议问题