短网址:最好的编码方法? [英] URL shortener: best encoding method?
问题描述
我创建一个链接缩短服务和我使用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
列。
当有人创建一个新的链接:
- 从数据库(获取下一个最大的链接
ID
你真的应该真正使用性能方面的原因自动增量
或SEQUENCE
,取决于你的RDBMS的优惠;否则,继续前进,选择MAX(ID)+1
) - 生成
ID短网址code(
使用http://website.com/ [短网址名称]
)base64_en code
或的的其他自定义或标准的编码方案 - 插入到
链接
表:ID,short_url_ code,DESTINATION_URL
-
如果插入失败,因为违反约束的回到步骤1去尝试一个新的
ID
;你可能有一个冲突,因为:- 相同ID已经被使用(即插入)平行的另一个线程/进程等(这将的不的发生,如果你使用
自动增量
或SEQUENCE
,并可能发生经常以其他方式),和/或 - 相同
short_url_ code
已被用来作为自定义URL(这将除非有人试图使您的网站上的麻烦非常罕发生)
- 相同ID已经被使用(即插入)平行的另一个线程/进程等(这将的不的发生,如果你使用
-
如果插入成功,提交和短URL返回给用户
当有人创建一个新的链接,并通过自定义短网址:
- 执行与上述相同的步骤1
- 代替生成短网址的一部分从
ID
如上述步骤2,使用自定义的short_url_ code
由用户提供的 - 执行与上述相同的步骤3
- 如果插入失败,因为:
- 约束违反了
ID
:回到步骤1去尝试一个新的ID
- 约束违反了
short_url_ code
:返回一个错误给用户,要求他选择一个不同的自定义URL,如短网址他/她提供了已使用
- 约束违反了
- 执行与上述相同的步骤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:
- Get the next largest link
ID
from the database (for performance reasons you should really REALLY useautoincrement
orSEQUENCE
, depending on what your RDBMS offers; otherwise go ahead and selectMAX(ID)+1
) - Generate a short URL code (
http://website.com/[short url name]
) fromID
usingbase64_encode
or any other custom or standard encoding scheme - Insert into the
links
table:ID, short_url_code, destination_url
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:- 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
orSEQUENCE
, and may happen quite often otherwise), and/or - 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)
- the same ID has already been used (i.e. inserted) in parallel by another thread/process etc. (this will not happen if you used
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:
- Perform the same step 1 as above
- Instead of generating the short URL part from
ID
as in step 2 above, use the customshort_url_code
provided by the user - Perform the same step 3 as above
- If the insert failed because of:
- a constraint violation on
ID
: go back to step 1 to try a newID
- 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
- a constraint violation on
- Perform the same step 5 as above
这篇关于短网址:最好的编码方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!