当前位置:网站首页>Design of short chain

Design of short chain

2022-07-06 23:34:00 OoZzzy

At present, in many scenarios , All need short chains , Especially involving some URL The logic of distribution . I used to be a pony AI Class business , The link sent by sales through SMS is a short chain . Why need a short chain ? Consider a URL There are path、query Equal parameter , Various parameters are spliced together to form a long string .

1. Short chain composition

agreement + domain name + path, The agreement can be directly ignored . Domain name is a must ( crap ), And short enough , Otherwise, it will become a long short chain ( It's silly ). Last path Is the key , It seems to be a 6 A string of characters , And the range of characters is upper and lower case letters + Numbers .

2.Path Generation

2.1 The hash algorithm

Path One of the ways is to calculate by hash algorithm . Common hash functions are MD5、SHA1 And other common cryptographic hash algorithms , Also have HighwayHash、MurmurHash And other non encrypted hash functions . With MurmurHash For example , It has been iterated to MurmurHash 3, Capable of producing 32bit and 128 bit Hash value of , And for those with strong regularity key, The characteristics of random distribution are very good .

However, hash conflicts are uncontrollable , Although we have N A way to resolve hash conflicts , But it will increase the overall complexity of the whole system .

2.2 Self increasing ID

You can also maintain one ID Auto generator , For each long chain generation 1、2、3 And so on , Then save the mapping of long chain and serial number in the database , Then get such as https://fake.short/1、https://fake.short/2 Equal short chain . Considering that a single machine is easy to cause a single point of failure , So it is generally distributed ID generator .

  • Mysql
  • SnowFlake
  • UidGenerator Baidu
  • Leaf Meituan

2.3 Further shortening

If we get 『1536389934』 This serial number , It still looks a little long , If you want to further shorten , You can convert decimal numbers into 62 Hexadecimal number . Then you get a shorter number than the original ID 了 .

Why use 62 Hexadecimal conversion ?

62 Base conversion is because 62 After binary conversion, only numbers are included + A lowercase letter + Capital . and 64 The base conversion will contain /,+ Such symbols ( It's not normal URL The characters of )encodeURIComponent(‘+’) => %xx
10 Turn into the system 62 Hexadecimal can shorten characters , If we want to 6 Bit words , There has been a 560 Billion combinations .

原网站

版权声明
本文为[OoZzzy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061551333806.html