Giter Site home page Giter Site logo

short-url's Introduction

短链项目介绍

背景

  1. 什么是短链

    短链即短链接,是将原始长网址压缩而成的短网址。

  2. 短链原理

    通过短链找到原始链接(长链接),然后重定向到原始链接地址即可。

  3. 为什么需要短链

    可为短信预留更多的内容空间。比如微博、极客时间等网站都在使用短链。

技术栈

  • SpringBoot
  • Redis
  • 通用 mapper
  • Guava

核心职责

  • 通过原始链接生成唯一短链地址。
  • 短链地址重定向到原始链接地址。

架构图

以下是该项目的架构图:

短链项目架构图

数据库表设计

create schema shortUrl collate utf8mb4_bin;

-- auto-generated definition
create table url_map
(
    id        int auto_increment comment '主键'
        primary key,
    short_url varchar(200) default '' not null comment '短链接',
    long_url  varchar(500) default '' not null comment '长链接',
    constraint uk_short_url
        unique (short_url)
)
    comment '长链短链对应表';

面试问题挖掘

以下为本项目的面试问题挖掘:

  • 如何保证短链唯一性?

    本项目生成唯一短链的方法为HASH + 六十二进制表示法,六十二进制是使用常用的0-9、a-z、A-Z,也就是10个数字 + 26个小写 + 26个大写 = 62位。7位长度62进制可以表示的范围是 62^7 = 3,521,614,606,208 (合计3.5万亿),完全足够使用。

  • 如何判断是否发生 HASH 冲突?

    可以使用关系型数据库中的唯一索引来判断,本项目为了提高性能采用的是布隆过滤器来判断是否存在 HASH 冲突。

  • 如何处理 HASH 冲突?

    如果发生 HASH 冲突,就在生成的字符串后面再随机拼一个字符串。如果还是发生冲突,就再在后面拼接一个字符串。

  • 如何解决布隆过滤器误判的情况?

    本项目在布隆过滤器之后,还增加了数据库校验。默认布隆过滤器中提示存在的短链地址,都要去数据库中再次检验一遍。

short-url's People

Watchers

huaixia18 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.