当前位置:网站首页>短链的设计
短链的设计
2022-07-06 15:51:00 【OoZzzy】
目前在很多场景下,都需要短链,尤其是涉及到一些 URL 下发的逻辑。之前做小马 AI 课的业务时,销售通过短信下发的链接就是一个短链。为什么需要短链呢?考虑到一个 URL 上有 path、query 等参数,各种参数拼接在一起就成了一个长的字符串。
1.短链组成
协议 + 域名 + path
,协议可以直接忽略。域名是必须的(废话),并且足够短,否则的话就变成了长的短链(挺傻的)。最后 path 的部分才是关键,看起来是一个由 6 个字符组成的字符串,并且字符的范围是大小写字母+数字。
2.Path 的生成
2.1 哈希算法
Path 的其中一种方式就是通过哈希算法计算得到。常见的哈希函数有 MD5、SHA1 等大家常见的加密型哈希算法,也有 HighwayHash、MurmurHash 等非加密型哈希函数。以 MurmurHash 为例,目前已经迭代到 MurmurHash 3,能够产生 32bit 和 128 bit 的哈希值,并且对于规律性较强的 key,随机分布的特征表现的很不错。
不过哈希冲突是不可控的,我们虽然有 N 种解决哈希冲突的方式,但是会增加整个系统的整体复杂度。
2.2 自增 ID
也可以维护一个 ID 自增生成器,对于每一个长链生成1、2、3等自增的序号,然后把长链和序号的映射保存在数据库里面,然后得到如 https://fake.short/1、https://fake.short/2 等短链。考虑到单机容易造成单点故障,所以一般都是分布式的 ID 生成器。
- Mysql
- SnowFlake
- UidGenerator 百度
- Leaf 美团
2.3 进一步缩短
如果我们得到『1536389934』这个序号的话,看起来还是有点长,如果想进一步缩短,可以把十进制数转换成62进制数。然后就得到一个比原序号更短的 ID 了。
为什么要用62进制转换?
62进制转换是因为62进制转换后只含数字+小写+大写字母。而64进制转换会含有/,+这样的符号(不符合正常URL的字符)encodeURIComponent(‘+’) => %xx
10进制转62进制可以缩短字符,如果我们要6位字符的话,已经有560亿个组合了。
边栏推荐
- 11 preparations for Web3 and Decentralization for traditional enterprises
- DevSecOps软件研发安全实践——发布篇
- mysql连接vscode成功了,但是报这个错
- One minute to learn how to install the system, win7 XP, win10 and win11 become very simple
- Computer reinstallation system teaching, one click fool operation, 80% of people have learned
- Precise drag and drop within a contentable
- What should I do if the USB flash disk data is formatted and how can I recover the formatted USB flash disk data?
- Dockermysql modifies the root account password and grants permissions
- 问下各位,有没有flink sql生成作业的文档啊或是案列啊知道flink cli可以建表和指定目
- Bipartite graph determination
猜你喜欢
AI金榜题名时,MLPerf榜单的份量究竟有多重?
js对JSON数组的增删改查
Station B Big utilise mon monde pour faire un réseau neuronal convolutif, Le Cun Forward! Le foie a explosé pendant 6 mois, et un million de fois.
Cloud native (32) | kubernetes introduction to platform storage system
浅谈现在的弊端与未来的发展
基础图表解读“东方甄选”爆火出圈数据
DockerMySQL无法被宿主机访问的问题解决
MySQL implementation of field segmentation from one line to multiple lines of example code
Coscon'22 community convening order is coming! Open the world, invite all communities to embrace open source and open a new world~
The problem that dockermysql cannot be accessed by the host machine is solved
随机推荐
dockermysql修改root账号密码并赋予权限
企業不想換掉用了十年的老系統
koa2对Json数组增删改查
Stop saying that microservices can solve all problems
Huawei cloud gaussdb (for redis) unveils issue 21: using Gauss redis to achieve secondary indexing
室内LED显示屏应该怎么选择?这5点注意事项必须考虑在内
The tutorial of computer reinstallation win10 system is simple and easy to understand. It can be reinstalled directly without U disk
Enterprises do not want to replace the old system that has been used for ten years
儿童睡衣(澳大利亚)AS/NZS 1249:2014办理流程
借助这个宝藏神器,我成为全栈了
基础图表解读“东方甄选”爆火出圈数据
同一个作业有两个source,同一链接不同数据库账号,为何第二个链接查出来的数据库列表是第一个账号的
Dockermysql modifies the root account password and grants permissions
Introduction to network basics
(shuttle) navigation return interception: willpopscope
Summary of three methods for MySQL to view table structure
云原生(三十二) | Kubernetes篇之平台存储系统介绍
On file uploading of network security
GPT-3当一作自己研究自己,已投稿,在线蹲一个同行评议
What should I do if the USB flash disk data is formatted and how can I recover the formatted USB flash disk data?