我想创建一个URL缩短服务,你可以写一个长URL到输入字段和服务缩短URL为“http://www.example.org/abcdef”。

可以用包含a-z, a-z和0-9的6个字符的字符串代替"abcdef"。这样就有560 ~ 570亿个可能的字符串。

我的方法:

我有一个有三列的数据库表:

Id,整数,自动递增 long, string,用户输入的长URL 短,字符串,缩短的URL(或只有六个字符)

然后将长URL插入到表中。然后,我将为“id”选择自动递增的值,并构建它的散列。这个散列应该作为“short”插入。但是我应该构建什么样的哈希呢?像MD5这样的哈希算法会创建太长的字符串。我认为我不用这些算法。一个自建的算法也可以。

我的想法:

对于“http://www.google.de/”,我得到了自动增量id 239472。然后我执行以下步骤:

short = '';
if divisible by 2, add "a"+the result to short
if divisible by 3, add "b"+the result to short
... until I have divisors for a-z and A-Z.

可以不断重复,直到这个数不能被整除为止。你认为这是一个好方法吗?你有更好的主意吗?

由于对这个主题的持续兴趣,我已经发布了一个高效的GitHub解决方案,包括JavaScript、PHP、Python和Java的实现。如果你喜欢,添加你的解决方案:)


当前回答

我不断增加数据库中每个域的整数序列,并使用Hashids将整数编码为URL路径。

static hashids = Hashids(salt = "my app rocks", minSize = 6)

我运行了一个脚本,看看它需要多长时间才能耗尽字符长度。对于6个字符,它可以做164,916,224个链接,然后增加到7个字符。Bitly使用7个字符。在我看来,不到五个字很奇怪。

哈希id可以将URL路径解码为整数,但更简单的解决方案是使用整个短链接sho。Rt /ka8ds3作为主键。

以下是完整的概念:

function addDomain(domain) {
    table("domains").insert("domain", domain, "seq", 0)
}

function addURL(domain, longURL) {
    seq = table("domains").where("domain = ?", domain).increment("seq")
    shortURL = domain + "/" + hashids.encode(seq)
    table("links").insert("short", shortURL, "long", longURL)
    return shortURL
}

// GET /:hashcode
function handleRequest(req, res) {
    shortURL = req.host + "/" + req.param("hashcode")
    longURL = table("links").where("short = ?", shortURL).get("long")
    res.redirect(301, longURL)
}

其他回答

alphabet = map(chr, range(97,123)+range(65,91)) + map(str,range(0,10))

def lookup(k, a=alphabet):
    if type(k) == int:
        return a[k]
    elif type(k) == str:
        return a.index(k)


def encode(i, a=alphabet):
    '''Takes an integer and returns it in the given base with mappings for upper/lower case letters and numbers 0-9.'''
    try:
        i = int(i)
    except Exception:
        raise TypeError("Input must be an integer.")

    def incode(i=i, p=1, a=a):
        # Here to protect p.                                                                                                                                                                                                                
        if i <= 61:
            return lookup(i)

        else:
            pval = pow(62,p)
            nval = i/pval
            remainder = i % pval
            if nval <= 61:
                return lookup(nval) + incode(i % pval)
            else:
                return incode(i, p+1)

    return incode()



def decode(s, a=alphabet):
    '''Takes a base 62 string in our alphabet and returns it in base10.'''
    try:
        s = str(s)
    except Exception:
        raise TypeError("Input must be a string.")

    return sum([lookup(i) * pow(62,p) for p,i in enumerate(list(reversed(s)))])a

这是我的版本,给任何需要的人。

public class UrlShortener {
    private static final String ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    private static final int    BASE     = ALPHABET.length();

    public static String encode(int num) {
        StringBuilder sb = new StringBuilder();
        while ( num > 0 ) {
            sb.append( ALPHABET.charAt( num % BASE ) );
            num /= BASE;
        }
        return sb.reverse().toString();   
    }

    public static int decode(String str) {
        int num = 0;
        for ( int i = 0; i < str.length(); i++ )
            num = num * BASE + ALPHABET.indexOf(str.charAt(i));
        return num;
    }   
}

这是我最初的想法,可以做更多的思考,或者做一些模拟,看看是否有效或需要改进:

我的答案是记住数据库中的长URL,并使用ID 0到999999999999999999(或所需的任意大的数字)。

但是ID 0到999999999999999999可能是个问题,因为

如果我们使用十六进制,甚至base62或base64,它可以更短。(base64就像YouTube使用A-Z A-Z 0-9 _和-) 如果它从0均匀地增加到999999999999999999,那么黑客就可以按照这个顺序访问它们,并知道人们相互发送的url,所以这可能是一个隐私问题

我们可以这样做:

have one server allocate 0 to 999 to one server, Server A, so now Server A has 1000 of such IDs. So if there are 20 or 200 servers constantly wanting new IDs, it doesn't have to keep asking for each new ID, but rather asking once for 1000 IDs for the ID 1, for example, reverse the bits. So 000...00000001 becomes 10000...000, so that when converted to base64, it will be non-uniformly increasing IDs each time. use XOR to flip the bits for the final IDs. For example, XOR with 0xD5AA96...2373 (like a secret key), and the some bits will be flipped. (whenever the secret key has the 1 bit on, it will flip the bit of the ID). This will make the IDs even harder to guess and appear more random

按照这种方案,分配id的单个服务器可以组成id,请求分配id的20或200个服务器也可以组成id。分配服务器必须使用锁/信号量来防止两个请求服务器获得相同的批处理(或者如果它一次接受一个连接,这已经解决了问题)。因此,我们不希望等待分配的队列太长。所以这就是为什么一次分配1000或10000个可以解决问题。

我的方法是:获取数据库ID,然后对其进行Base36编码。我不会同时使用大写字母和小写字母,因为这会使通过电话传输这些url成为一场噩梦,但您当然可以轻松地将该功能扩展为62 en/解码器。

我有一个问题的变体,因为我存储了来自许多不同作者的网页,需要防止通过猜测发现页面。所以我的短url在Base-62字符串中添加了几个额外的数字作为页码。这些额外的数字是由页面记录本身的信息生成的,它们确保3844个url中只有一个是有效的(假设2位Base-62)。您可以在http://mgscan.com/MBWL上看到概要描述。