创建自己的TinyURL [英] Creating your own TinyURL

查看:200
本文介绍了创建自己的TinyURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚找到了这个很棒的教程,因为这是我所需要的.

I have just found this great tutorial as it is something that I need.

但是,在看了一眼之后,这似乎效率很低.它的工作方式是,首先生成一个唯一密钥,然后检查它是否存在于数据库中以确保它确实是唯一的.但是,数据库越大,功能获取越慢,对吧?

However, after having a look, it seems that this might be inefficient. The way it works is, first generate a unique key then check if it exists in the database to make sure it really is unique. However, the larger the database gets the slower the function gets, right?

相反,我当时在想,是否可以向该功能添加顺序?因此,所有要做的就是检查数据库中的上一个条目并增加密钥.因此,它将永远是唯一的吗?

Instead, I was thinking, is there a way to add ordering to this function? So all that has to be done is check the previous entry in the DB and increment the key. So it will always be unique?

function generate_chars()

{

    $num_chars = 4; //max length of random chars
    $i = 0;
    $my_keys = "123456789abcdefghijklmnopqrstuvwxyz"; //keys to be chosen from
    $keys_length = strlen($my_keys);
    $url  = "";
    while($i<$num_chars)
    {
        $rand_num = mt_rand(1, $keys_length-1);
        $url .= $my_keys[$rand_num];
        $i++;
    }
    return $url;
}

function isUnique($chars)

{
    //check the uniqueness of the chars
    global $link;
    $q = "SELECT * FROM `urls` WHERE `unique_chars`='".$chars."'";
    $r = mysql_query($q, $link);
    //echo mysql_num_rows($r); die();
    if( mysql_num_rows($r)>0 ): 
        return false;
    else: 
        return true;
    endif;
}

推荐答案

微型url人们喜欢使用随机令牌,因为那样一来,您就不能只拖动微型url链接. #2往哪里去?" 哦,太酷了!" #3往哪里去?" 甚至更凉!"您可以输入随机字符,但不太可能会击中有效值.

The tiny url people like to use random tokens because then you can't just troll the tiny url links. "Where does #2 go?" "Oh, cool!" "Where does #3 go?" "Even cooler!" You can type in random characters but it's unlikely you'll hit a valid value.

由于键相当稀疏(4个值中的每一个具有36 *的可能性,因此您可以得到1,679,616个唯一值,5个值给您60,466,176个),发生冲突的机会很小(实际上,这是设计的理想部分),并且SQL索引良好将会使查找变得无关紧要(实际上,这是url的主要查找,因此它们可以对其进行优化).

Since the key is rather sparse (4 values each having 36* possibilities gives you 1,679,616 unique values, 5 gives you 60,466,176) the chance of collisions is small (indeed, it's a desired part of the design) and a good SQL index will make the lookup be trivial (indeed, it's the primary lookup for the url so they optimize around it).

如果您真的想避免查找,而只是自动递增,则可以创建一个函数,该函数将整数转换为看似随机的字符串,并具有转换回来的能力.因此,"1"变为"54jcdn","2"变为"pqmw21".与Base64编码相似,但不使用连续字符.

If you really want to avoid the lookup and just unse auto-increment you can create a function that turns an integer into a string of seemingly-random characters with the ability to convert back. So "1" becomes "54jcdn" and "2" becomes "pqmw21". Similar to Base64-encoding, but not using consecutive characters.

(*)我实际上喜欢使用少于36个字符-单写,无元音和类似字符(1、1,l,I).这样可以防止偶然的脏话,也使某人更容易将价值传达给其他人.我什至彼此映射相似的字符,接受"0"表示"O".如果您完全基于计算机,则可以使用大写和小写以及所有数字,以实现更大的可能性.

(*) I actually like using less than 36 characters -- single-cased, no vowels, and no similar characters (1, l, I). This prevents accidental swear words and also makes it easier for someone to speak the value to someone else. I even map similar charactes to each other, accepting "0" for "O". If you're entirely machine-based you could use upper and lower case and all digits for even greater possibilities.

这篇关于创建自己的TinyURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆