微博的興起,帶來了一個(gè)新的詞語:短鏈。
何謂短鏈?如果我們?cè)谖⒉├锇l(fā)布一條帶網(wǎng)址的信息,微博會(huì)把里面的網(wǎng)址轉(zhuǎn)化成一個(gè)更短的網(wǎng)址。我們只要訪問這個(gè)短網(wǎng)址,就相當(dāng)于訪問原始的網(wǎng)址。
那如何實(shí)現(xiàn)一個(gè)短鏈系統(tǒng)呢?核心包括以下兩點(diǎn):
1、在系統(tǒng)中生成全局唯一的數(shù)字。有很多方式可以實(shí)現(xiàn),詳情請(qǐng)參考我的另一篇文章:分布式系統(tǒng)唯一ID生成技術(shù)方案
2、如何表示該數(shù)學(xué)?如果直接顯示,那么數(shù)字可能很長(zhǎng),比如:5632033057037877251,沒有體現(xiàn)出短鏈的優(yōu)勢(shì)。我們可以將一個(gè)十進(jìn)制的整數(shù)轉(zhuǎn)換成十六進(jìn)制,這么顯示的范圍就是0123456789ABCDEF。也可以用字母+數(shù)字來表示。
而為了得到盡可能短的內(nèi)容,我們可以使用數(shù)字+大寫字母+小寫字母一起來表示一個(gè)十進(jìn)制數(shù)。這樣能顯示的范圍就是0-9a-zA-Z,能夠極大地縮短內(nèi)容的長(zhǎng)度。
為了設(shè)計(jì)一個(gè)更一般的系統(tǒng),我特地實(shí)現(xiàn)了十進(jìn)制轉(zhuǎn)換為任意進(jìn)制的實(shí)現(xiàn)。如下所示:
/*
一個(gè)處理不同進(jìn)制的工具包;用于將十進(jìn)制和其它進(jìn)制進(jìn)行互相轉(zhuǎn)換
*/
package baseUtil
import (
"fmt"
"math"
)
// 進(jìn)制對(duì)象定義
type Base struct {
elementList []string
base uint64
}
// 將10進(jìn)制的uint64類型數(shù)據(jù)轉(zhuǎn)換為字符串形式
// source:10進(jìn)制的uint64類型數(shù)據(jù)
// 返回值:
// 對(duì)應(yīng)進(jìn)制的字符串形式
func (this *Base) Transform(source uint64) (result string) {
quotient, remainder := uint64(0), source
for {
quotient, remainder = remainder/this.base, remainder%this.base
result = this.elementList[remainder] + result
if quotient == 0 {
break
}
remainder = quotient
}
return
}
// 將字符串解析為10進(jìn)制的uint64類型
// source:對(duì)應(yīng)進(jìn)制的字符串形式
// 返回值:10進(jìn)制的uint64類型數(shù)據(jù)
func (this *Base) Parse(source string) (result uint64) {
if source == "" {
return
}
sourceList := make([]string, 0, len(source))
for _, v := range source {
sourceList = append(sourceList, string(v))
}
for idx, exp := len(sourceList)-1, 0; idx >= 0; idx, exp = idx-1, exp+1 {
sourceItem := sourceList[idx]
// Find the source item in the elementList
for i, v := range this.elementList {
if sourceItem == v {
result += uint64(float64(i) * math.Pow(float64(this.base), float64(exp)))
}
}
}
return
}
// 以指定的任意非重復(fù)的數(shù)組,來指定基于的進(jìn)制數(shù)
func New(elements string) (baseObj *Base, err error) {
if len(elements) == 0 {
err = fmt.Errorf("輸入的字符數(shù)串為空")
return
}
elementList := make([]string, 0, len(elements))
elementMap := make(map[rune]struct{}, len(elements))
for _, v := range elements {
if _, exist := elementMap[v]; exist {
err = fmt.Errorf("輸入的字符串中含有重復(fù)的字符:%s", string(v))
return
} else {
elementMap[v] = struct{}{}
elementList = append(elementList, string(v))
}
}
baseObj = &Base{
elementList: elementList,
base: uint64(len(elementList)),
}
return
}
// 包含01
func NewBase2() (baseObj *Base, err error) {
return New("01")
}
// 包含0-7
func NewBase8() (baseObj *Base, err error) {
return New("01234567")
}
// 包含0-9,a-x
func NewBase16() (baseObj *Base, err error) {
return New("0123456789abcdef")
}
// 包含a-z
func NewBase26() (baseObj *Base, err error) {
return New("abcdefghijklmnopqrstuvwxyz")
}
// 包含0-9,a-z
func NewBase36() (baseObj *Base, err error) {
return New("0123456789abcdefghijklmnopqrstuvwxyz")
}
// 包含0-9,a-z,A-Z
func NewBase62() (baseObj *Base, err error) {
return New("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
}
完整的代碼,請(qǐng)參考:https://github.com/Jordanzuo/goutil/tree/master/baseUtil