foreword
Share an online short link generation system based on SpringBoot, an online short link generator.
The complete project source code is at the end of the article, you can download it by yourself~
1. Realize the function
1. Convert the long link to a short link. When accessing the short link, 302 redirects to the original long link
2. Support setting short chain validity period
3. Support recording the number of visits
2. Interface display
3. Technology selection
rely | illustrate |
---|---|
SpringBoot | basic framework |
Thymeleaf | template engine |
JdbcTemplate | Persistence layer framework |
Redis | cache |
guava | Hash algorithm, Bloom filter |
4. Realize the logic
1. Use the MurmurHash algorithm to hash the original long link into a 32-bit hash value, and convert the hash value to BASE62 encoding, which is a short link.
2. When a user accesses a short link, check whether there is a cache in Redis, and if it exists, refresh the cache time; if it does not exist in the cache, go to the database to search, if the search is successful, it will be added to the Redis cache, 302 redirects to the original long link, and automatically Increase the number of link visits; if it does not exist in the database, jump to the 404 page.
5. Some technical introduction
1、MurmurHash
MurmurHash is a non-cryptographic hash function suitable for general hash retrieval operations. Compared with other popular hash functions such as MD5, the random distribution characteristics of MurmurHash perform better for keys with strong regularity. Non-encryption means that compared with MD5, the performance of SHA functions must be higher (in fact, the performance is more than ten times that of encryption algorithms such as MD5). MurmurHash has 32 bit, 64 bit, and 128 bit implementations, and 32 bit is enough to represent nearly 4.3 billion short links. If Java is used, there is a corresponding implementation in Google's
guava or hutool, and guava is used here.
2、base62
The hash value generated by MurmurHash has a maximum of 10 decimal digits. In order to further shorten the length of the short link, the hash value can be converted to base62 encoding, so that the maximum length is only 6 characters.
3. The difference between 301 and 302 redirection
- 301, stands for permanent redirection , that is to say, after the first request to get a long link, if the browser requests a short link next time, it will not request the short URL server, but directly get it from the browser's cache , so that the number of clicks on the short URL cannot be obtained at the server level. If the link happens to be a link of a certain activity, the effect of this activity cannot be analyzed. So we generally don't use 301.
- 302, stands for temporary redirection , that is to say, every time you request a short link, you will request the short URL server (unless Cache-Control or Expired is used in the response to imply browser caching), which is convenient for the server to count the number of hits, so although 302 is used It will add a little pressure to the server, but today when the data is extremely important, this bit of code is worth it, so 302 is recommended!
6. Project source code download
Project source code download link : https://download.csdn.net/download/weixin_47367099/85397360