首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 短網址雙向轉換實現:Base62 編碼、Memcached 存儲、自動遞增 ID

     編輯:PHP 相關知識

核心演算法:Base62 編碼

字符集:0-9a-zA-Z (62 字符),將數字 ID 轉為短字串,反之解碼。

完整實現代碼

class ShortUrl {
    private $mem;
    private $baseUrl = 'https://xxx.com/';
    private $charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    
    public function __construct() {
        $this->mem = new Memcached();
        $this->mem->addServer('192.168.10.90', 11116);
        $this->mem->addServer('192.168.10.90', 11117);
    }
    
    // 編碼:長網址 -> 短碼
    public function encode($url) {
        // 過濾協議頭
        $url = preg_replace('#^[https://|https://|ftp://]#iS', '', $url);
        
        // 查快取
        $key = 'url:' . md5($url);
        if (($code = $this->mem->get($key)) !== false) {
            return $this->baseUrl . $code;
        }
        
        // 獲取/生成自動遞增 ID
        if (($id = $this->mem->increment('auto_increment_id')) === false) {
            $this->mem->set('auto_increment_id', 10000);
            $id = $this->mem->increment('auto_increment_id');
        }
        
        // Base62 編碼
        $code = $this->dec2any($id);
        
        // 雙向快取
        $this->mem->set($key, $code);
        $this->mem->set('code:' . $code, $url);
        
        return $this->baseUrl . $code;
    }
    
    // 解碼:短碼 -> 原網址
    public function decode($shortUrl) {
        $code = str_replace($this->baseUrl, '', $shortUrl);
        $key = 'code:' . $code;
        return $this->mem->get($key) ?: 'Not Found';
    }
    
    // 10進制轉62進制
    private function dec2any($num, $base = 62) {
        if ($num == 0) return '0';
        $result = '';
        while ($num > 0) {
            $result = $this->charset[$num % $base] . $result;
            $num = floor($num / $base);
        }
        return $result;
    }
    
    // 62進制轉10進制
    private function any2dec($str, $base = 62) {
        $len = strlen($str);
        $result = 0;
        for ($i = 0; $i < $len; $i++) {
            $result = $result * $base + strpos($this->charset, $str[$i]);
        }
        return $result;
    }
}

// 測試
$su = new ShortUrl();
$urls = ['https://www.baidu.com', 'https://www.google.com', 'https://www.jbxue.com'];
foreach ($urls as $url) {
    $short = $su->encode($url);
    echo "Short: $short\n";
    echo "Original: " . $su->decode($short) . "\n\n";
}

架構優勢與擴展建議

  • 分散式 ID:Memcached increment 原子操作保證唯一性
  • 雙向映射:MD5 長網址為 key 查重、短碼反查原址
  • 高可用:多 Memcached 節點自動故障轉移
  • 擴展方向:加入點擊統計、過期時間、自定義短碼、API 限流
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved