短网址:最好的编码方法? [英] URL shortener: best encoding method?

查看:195
本文介绍了短网址:最好的编码方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个链接缩短服务和我使用base64编码/递增的ID字段的解码创建我的网址。 ID为6A网址是: http://mysite.com/Ng==

我还需要允许用户创建自定义URL的名称,如 http://mysite.com/music

下面是我的(可能是错误的)方法为止。帮助修复这将是AP preciated。

当有人创建一个新的链接:


  • 我得到的最大的链路ID从数据库(它不是自动递增)

  • 1
  • 递增的ID
  • 将base64_encoding生成短URL code(http://website.com/[short URL名称])的ID

  • 插入链接表:ID,short_url_ code,DESTINATION_URL

当有人创建一个新的链接,并通过自定义短网址:


  • 我的计划是base64_de code自己的自定义字符串并将其用作链接ID,但我并没有意识到,你不能只是base64_de code的任何字母数字字符串,并把它变成一个数字。

有没有更好的编码方法,可以让我把任何数量成短串,任何字符串转换成数字,这样我就可以随时查找短网址(无论是定制或自动生成)转动名成若干和查询对于等于多少?

一个ID的链接
解决方案

首先,确保您在 ID具有唯一性约束 short_url_ code 列。

当有人创建一个新的链接:


  1. 从数据库(获取下一个最大的链接 ID 你真的应该真正使用性能方面的原因自动增量 SEQUENCE ,取决于你的RDBMS的优惠;否则,继续前进,选择 MAX(ID)+1

  2. 生成 ID短网址code( http://website.com/ [短网址名称] 使用 base64_en code 的其他自定义或标准的编码方案

  3. 插入到链接表: ID,short_url_ code,DESTINATION_URL

  4. 如果插入失败,因为违反约束的回到步骤1去尝试一个新的 ID ;你可能有一个冲突,因为:


    1. 相同ID已经被使用(即插入)平行的另一个线程/进程等(这将的的发生,如果你使用自动增量 SEQUENCE ,并可能发生经常以其他方式),和/或

    2. 相同 short_url_ code 已被用来作为自定义URL(这将除非有人试图使您的网站上的麻烦非常罕发生)


  5. 如果插入成功,提交和短URL返回给用户


当有人创建一个新的链接,并通过自定义短网址:


  1. 执行与上述相同的步骤1

  2. 代替生成短网址的一部分从 ID 如上述步骤2,使用自定义的 short_url_ code 由用户提供的

  3. 执行与上述相同的步骤3

  4. 如果插入失败,因为:

    1. 约束违反了 ID :回到步骤1去尝试一个新的 ID

    2. 约束违反了 short_url_ code :返回一个错误给用户,要求他选择一个不同的自定义URL,如短网址他/她提供了已使用


  5. 执行与上述相同的步骤5

I'm creating a link shortening service and I'm using base64 encoding/decoding of an incremented ID field to create my urls. A url with the ID "6" would be: http://mysite.com/Ng==

I need to also allow users to create a custom url name, like http://mysite.com/music

Here's my (possibly faulty) approach so far. Help in fixing it would be appreciated.

When someone creates a new link:

  • I get the largest link ID from the database (it's not auto incremented)
  • Increment the ID by 1
  • Generate a short URL code (http://website.com/[short url name]) by base64_encoding that ID
  • Insert into links table: id, short_url_code, destination_url

When someone creates a new link and passes a custom short URL:

  • My plan was base64_decode their custom string and use that as the link ID, but I didn't realize that you can't just base64_decode any alphanumeric string and turn it into a number.

Is there a better encoding method that will let me turn any number into a short string, and any string into a number, so I can always lookup short urls (whether custom or autogenerated) by turning the name into a number and querying for a link with an ID equal to that number?

解决方案

First and foremost, make sure you have unicity constraints in place on the ID and short_url_code columns.

When someone creates a new link:

  1. Get the next largest link ID from the database (for performance reasons you should really REALLY use autoincrement or SEQUENCE, depending on what your RDBMS offers; otherwise go ahead and select MAX(ID)+1 )
  2. Generate a short URL code (http://website.com/[short url name]) from ID using base64_encode or any other custom or standard encoding scheme
  3. Insert into the links table: ID, short_url_code, destination_url
  4. If the insert fails because of a constraint violation go back to step 1 to try a new ID; you may have had a violation because:

    1. the same ID has already been used (i.e. inserted) in parallel by another thread/process etc. (this will not happen if you used autoincrement or SEQUENCE, and may happen quite often otherwise), and/or
    2. the same short_url_code has already been used as a custom URL (this will happen very seldomly unless someone is trying to cause trouble on your site)

  5. If the insert succeeded, commit and return the short URL to the user

When someone creates a new link and passes a custom short URL:

  1. Perform the same step 1 as above
  2. Instead of generating the short URL part from ID as in step 2 above, use the custom short_url_code provided by the user
  3. Perform the same step 3 as above
  4. If the insert failed because of:

    1. a constraint violation on ID: go back to step 1 to try a new ID
    2. a constraint violation on short_url_code: return an error to the user asking him to pick a different custom URL, as the short URL he/she provided has already been used

  5. Perform the same step 5 as above

这篇关于短网址:最好的编码方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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