短网址指在形式上比较短的网址。国内短网址用的比较重的产品属新浪微博(t。cn),随时随地可以在微博上看到http:t。cn开头的网址。短网址一般应用于对字数有限制的产品业务中。例如发送短信消息、发微博。短网址一方面可以让内容看起来精简美观。另一方面可以节省存储空间。借助短网址您可以用简短的网址替代原来冗长的网址,让使用者可以更容易地分享访问链接。 短网址整体算是个功能单一的平台化服务。产品需求包含生成短网址、短网址去重、管理平台。但技术难点不小包含高并发、热点网址、短网址重复率。下面是我总结的一些短网址生成算法和技术难点的解决方案。 1、crc32算法functionshorturlcrc32(strUrl){strShortUrl;求字符串crc32数字intCrc32floatval(sprintf(u,crc32(strUrl,u)));循环取余求ascii码while(intCrc32){intfModfmod(intCrc32,62);if(intfMod0intfMod9){intfModchr(intfMod48);}elseif(intfMod10intfMod35){intfModchr(intfMod55);}elseif(intfMod36intfMod62){intfModchr(intfMod61);}strShortUrl。intfMod;intCrc32floor(intCrc3262);}returnstrShortUrl;}strUrlhttps:www。baidu。comabc;vardump(shorturlcrc32(strUrl));运行结果:string(5)MajOz 2、crc64算法functioncrc64Table(){crc64tab〔〕;ECMApolynomialpoly64rev(0xC96C579532)0xD7870F42;ISOpolynomialpoly64rev(0xD856);for(i0;i256;i){for(parti,bit0;bit8;bit){if(part1){part((part1)(0x860))poly64rev;}else{part(part1)(0x860);}}crc64tab〔i〕part;}returncrc64tab;}paramstringstringparamstringformatreturnmixedFormats:crc64(php);afe4e823e7cef190crc64(php,0xx);0xafe4e823e7cef190crc64(php,0xX);0xAFE4E823E7CEF190crc64(php,d);5772233581471534704signedintcrc64(php,u);12674510492238016912unsignedintfunctioncrc64(string,formatx){staticcrc64tab;if(crc64tabnull){crc64tabcrc64Table();}crc0;for(i0;istrlen(string);i){crccrc64tab〔(crcord(string〔i〕))0xff〕((crc8)(0xff56));}returnsprintf(format,crc);}functionshorturlcrc64(strUrl){strShortUrl;求字符串crc64数字intCrc64floatval(sprintf(u,crc64(strUrl,u)));循环取余求ascii码while(intCrc64){intfModfmod(intCrc64,62);if(intfMod0intfMod9){intfModchr(intfMod48);}elseif(intfMod10intfMod35){intfModchr(intfMod55);}elseif(intfMod36intfMod62){intfModchr(intfMod61);}strShortUrl。intfMod;intCrc64floor(intCrc6462);}return((strlen(strShortUrl)6)?substr(strShortUrl,6):strShortUrl);}strUrlhttps:www。baidu。comabc;vardump(shorturlcrc64(strUrl));运行结果:string(6)vlm2UF 3、HashMD5算法functionshorturlhashmd5(strUrl){arrCharsarray(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z);arrShortUrlarray();求字符串md532字节值strMd5md5(strUrl);for(i0;i4;i){strShortUrl;每段8字节subHexsubstr(strMd5,i8,8);取8字节的前30位intHex0x3FFFFFFF(1bin2hex(0x。subHex));for(j0;j6;j){与0x0000003E进行位与运算,取得字符数组chars索引intIndex0x0000003EintHex;strShortUrl。arrChars〔intIndex〕;intHexintHex5;}arrShortUrl〔〕strShortUrl;}intRandIndexarrayrand(arrShortUrl,1);returnarrShortUrl〔intRandIndex〕;}strUrlhttps:www。baidu。comabc;vardump(shorturlhashmd5(strUrl));运行结果:string(6)aaVqLw 4、自增idopenssl加密算法functionshorturlopenssl(intUrlId){strEncodeUrlopensslencrypt(intUrlId,aes128,2022,false,1234567890123456);returnsubstr(strEncodeUrl,0,6);}intUrlId112;vardump(shorturlopenssl(intUrlId));运行结果:string(6)W7noUA 5、随机字符串算法functionshorturlrandchar(){strCharsABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz;strRandCharsubstr(strshuffle(strChars),mtrand(0,strlen(strChars)11),6);returnstrRandChar;}vardump(shorturlrandchar());运行结果:string(6)OZYzkH 6、数据效验(效验位)functionshorturlvalid(strShortUrl){intSum0;intLenstrlen(strShortUrl);for(i0;iintLen;i){intSumord(strShortUrl〔i〕);}strOddX;intOddintSum62;if(intOdd0intOdd9){strOddchr(intOdd48);}elseif(intOdd10intOdd35){strOddchr(intOdd55);}elseif(intOdd36intOdd62){strOddchr(intOdd61);}returnstrOdd;}strShortUrlW7noUA;vardump(shorturlvalid(strShortUrl));运行结果:string(1)H 7、NginxRewrite配置location〔azAZ09〕{roothomeworkshorturl;fastcgipass127。0。0。1:9000;fastcgiindexshorturl。php;fastcgiparamSCRIPTFILENAMEdocumentrootfastcgiscriptname;includefastcgiparams;rewrite(〔azAZ09〕)shorturl。phplast;} 8、技术难点 高并发:水平扩容Web服务器分布式Redis集群MySQL一主多从(写少读多) 热点网址增加Web服务器的本地缓存管理平台同步热点网址到Redis集群ALL服务器管理平台 热点网址监控RedisSortSet集合排序(短时间内访问量达到一定阈值的key才入Set)大数据实时数据监控(前端打点日志)。 9、总结根据存储量和重复率分析5种生成算法个人推荐使用crc64算法。原因是扩展性强、重复率低。短网址增加数据效验位减少恶意流量的无效业务逻辑处理。Redis内存使用量根据产品前期需要计算清楚。 感谢大家的评论、点赞、分享