将长链接换成短链接
计算长链接
的 hashcode
,并将此hascode
做为一个key
与长链接对应, 存储在Redis
中
https://www.imyong.top ==> hascode d28e4411
key: d28e4411, value: https://www.imyong.top (in redis中)
short url: https://localhost:8080/t/d28e4411
当我们访问生成的短链接时 https://localhost:8080/t/d28e4411 后台将 d28e4411
取出来做为key
到Redis
中去查找相应的value ( 也就是长链接), 取出之后返回给前端( 这里也可以直接redirect到这个长链接)
长链接 转成 短链接 逻辑
+--------+ +--------+ +-----------------+
| | | | | |
| Long | hash | | Store Key to Redis | |
| URL |------------------> | Key | -------------------> | Redis |
| | | | | |
+--------+ +--------+ +-----------------+
短链接 变回 长链接
+-----------+ +-----------------+
| | /------------\ Using the Key | |
| Short | Get the Key / BackEnd \ to get the Long Url | |
| URL | -------------> ( Get the ) ---------------------> | Redis |
| | \ Key / <--------------------- | |
+-----------+ \___________/ Return Long URL +-----------------+
|
|
|
|
V
+-------------+
| |
| Redirect |
| To |
| Long URL |
+-------------+
sprint-boot:run
The visit https://localhost:8080 即可
- 本程序依赖了 swagger-ui-layer, 运行前需要安装此jar包