How does the website shortening program work?

Source: Internet
Author: User
How does the website shortening program work?

[Author: Sun Li link: http://www.cnblogs.com/sunli/ updated: 2010-03-25]

Recently, Weibo is very popular. Due to word limit, many websites have been used to shorten this kind of service. For example, sina has used its own sinaurl.cn and has been registering a very short domain name k6.hk for a long time, I have been idle and don't know what to do. I suddenly thought it would be nice to use it to shorten the website. Just do it and write it in a moment. For example, my blog address can be shortened to: http://k6.hk/u

The program design is very simple. The following describes the principle,

The database only has two fields: seq (self-increasing number) and url (the url address of the number, index creation ).

Enter a url address to check whether the table contains this url. If so, the seq number is returned,

If it does not exist, insert it into the database to get a new auto-increment seq number. To reduce the number of characters occupied by the number, we can use uppercase and lowercase letters such as abc. In this way, there are 10 digits, 26 lower-case letters, and 26 uppercase and lowercase letters which constitute a 62-digit system. For example, after the number 10000000000 (10 billion) is converted to aUKYOA, there are only six digits, which can shorten the number of URLs.

The following is the php hexadecimal conversion code, which comes from the php Manual (simple). Of course, the implementation in other languages is also very simple,

 

 <? Php
// Convert the value in decimal format to another format.
Function dec2any ($ num, $ base = 62, $ index = false ){
If (! $ Base ){
$ Base = strlen ($ index );
} Else if (! $ Index ){
$ Index = substr( "0123456789 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 0, $ base );
}
$ Out = "";
For ($ t = floor (log10 ($ num)/log10 ($ base); $ t> = 0; $ t --){
$ A = floor ($ num/pow ($ base, $ t ));
$ Out = $ out. substr ($ index, $ a, 1 );
$ Num = $ num-($ a * pow ($ base, $ t ));
}
Return $ out;
}

Function any2dec ($ num, $ base = 62, $ index = false ){
If (! $ Base ){
$ Base = strlen ($ index );
} Else if (! $ Index ){
$ Index = substr( "0123456789 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 0, $ base );
}
$ Out = 0;
$ Len = strlen ($ num)-1;
For ($ t = 0; $ t <= $ len; $ t ++ ){
$ Out = $ out + strpos ($ index, substr ($ num, $ t, 1) * pow ($ base, $ len-$ t );
}
Return $ out;
}

?>

The above is just about the implementation principle. If you want to use it on a large scale, the backend can discard the data and store it in the key-value database, such as the ttserver, which will have a high performance improvement.

If you change the source code of the ttserver and directly jump through the http interface of the ttserver, the performance will be very high. It is not a problem for a single machine to provide up to 1 billion times a day. Two machines can be used to achieve high availability. Such services do not consume traffic.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.