How the Web site shortening program is done

Source: Internet
Author: User
Tags lowercase pow strlen

Recently Weibo is very hot, because of the word limit, there are a lot of web sites to shorten this service, such as Sina himself with their own sinaurl.cn, think of their own has also registered a very short domain name k6.hk for a long time, has been idle, do not know why use, suddenly thought can be used to do the site shortened, not bad. Just do it, and it'll be ready in a few words. For example, my blog address can be shortened to: http://k6.hk/u

The design of the program is very simple, the following principle,

The database has only two fields of seq (self-growth number) and URL (numeric URL address, indexed).

The user enters a URL address, whether the query table contains this URL, and if so, returns the number of Seq.

If not, insert the database to get a newly incremented SEQ number, and in order to shorten the number of characters, we can use the uppercase and lowercase letters of ABC. So 10 digits, 26 lowercase letters, and 26 letters of size make up a 62. For example, the number 10000000000 (10 billion) after the conversion is AUKYOA, only 6, so you can shorten a lot of URLs.

Here is the PHP conversion code, from the PHP manual (Simple), of course, other language implementation is also very simple,

<?php
Decimal go to other system
function Dec2any ($num, $base =62, $index =false) {
if (! $base) {
$base = strlen ($index);
else if (! $index) {
$index = substr ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 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 ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 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;
}
?>

Above just said the principle of implementation, if to large-scale use, the backend can discard data, with Key-value database storage, such as Ttserver, there will be a high performance improvement.

If you change the Ttserver source code, through the Ttserver HTTP interface to jump directly, then performance will be very high, a machine one day to provide 1 billion of secondary school is not a problem. Use two machines to achieve high availability, this service is not very traffic-intensive.

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.