生活随笔
收集整理的這篇文章主要介紹了
redis实战:使用redis实现自动补全
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載:http://blog.csdn.net/u011250882/article/details/48632379
如果我想輸入“雄英”來找到游戲庫中的所有帶有這兩個字的游戲,該怎樣用redis來實現呢?
原理:
1, 將所有的游戲名字讀出來,拆分成單個漢字
2, 將這些漢字作為redis集合的鍵,寫入redis,每個集合里的值是所有那些游戲名字中包含此漢字的游戲的id
3, 當用戶輸入文字的時候通過ajax異步請求,將用戶輸入傳給PHP
4, 將輸入的文字拆分成單個漢字, 分別找到這些漢字在redis中的集合值
5, 取出來,求交集,就找到了同時包含這幾個漢字的游戲的id
6, 最后到數據庫里查出來相應的游戲信息即可
缺點: 刪除數據不方便
具體實現代碼:
redis代碼:iredis.php
[php]?view plaincopy
<?php?? class?iRedis?extends?Redis{?? ?? ????public?function?__construct(){?? ????????$this->connect('127.0.0.1',?6379);?? ????}?? ?? ?????? ????public?function?getIndex($key){?? ????????return?$this->sMembers($key);?? ????}?? ?? ?????? ????public?function?delIndex($key){?? ????????return?$this->sMembers($key);?? ????}?? ?????? }??
game模型類代碼:
[php]?view plaincopy
<?php??? require_once?'database.php';?? require_once?'iredis.php';?? class?Game?extends?Database{?? ?? ????public?$instance;?? ?? ????public?$redis;?? ?? ????public?$table?=?'game';?? ?? ????public?function?__construct(){?? ????????$this->instance?=?self::getInstance();?? ????????$this->redis?=?new?iRedis();?? ????}?? ?? ?????? ????public?function?createIndexes(){?? ????????$sql?=?"select?*?from?$this->table";?? ????????$games?=?$this->instance->query($sql)->fetch_all(MYSQLI_ASSOC);?? ????????$splitedGames?=?$this->splitGameName($games);?? ????????foreach($splitedGames?as?$k?=>?$v){?? ????????????$this->redis->sAdd($k,?serialize($v));?? ????????}?? ????}?? ?? ?????? ????public?function?getGameByGameid($gameid){?? ????????$gameidStr?=?implode(',',?$gameid);?? ????????$sql?=?"select?name?from?$this->table?where?id?in?($gameidStr)";?? ????????$gameName?=?$this->instance->query($sql)->fetch_all(MYSQLI_ASSOC);?? ????????return?$gameName;?? ????}?? ?? ?????? ????public?function?getHint($keyword){?? ????????if(empty($keyword)){?? ????????????return?"";???????????????? ????????}?? ?? ????????if(mb_strlen($keyword,?'utf-8')?==?1){?? ?????????????? ????????????$unserializedGameids?=?$this->redis->getIndex($keyword);?? ????????????$gameids?=?unserialize($unserializedGameids[0]);?? ????????????if(empty($gameids)?||?!is_array($gameids)){?? ????????????????return?"";?? ????????????}?? ????????????return?$this->getGameByGameid($gameids);?? ????????}?? ?? ????????$gameids?=?array();?? ????????$keywordLenth?=?mb_strlen($keyword,?'utf-8');?? ????????for?($i?=?0;?$i?<?$keywordLenth;?$i++)?{??? ????????????$keywordPiece?=?mb_substr($keyword,?$i,?1,?'utf-8');?? ????????????$unserializedGameids?=?$this->redis->getIndex($keywordPiece);?? ????????????$gameid?=?unserialize($unserializedGameids[0]);?? ????????????if(!empty($gameid)){?? ????????????????$gameids?=?empty($gameids)???$gameid?:?array_intersect($gameids,?$gameid);?? ????????????}?? ????????}?? ?? ????????if(empty($gameids)?||?!is_array($gameids)){?? ????????????????return?"";?? ????????}?? ????????return?$this->getGameByGameid($gameids);?? ?? ????}?? ?? ?????? ????private?function?splitGameName($games){?? ????????$splitedGames?=?array();?? ????????foreach?($games?as?$game)?{?? ????????????$ganeNameLenth?=?mb_strlen($game['name'],?'utf-8');?? ????????????for?($i?=?0;?$i?<?$ganeNameLenth;?$i++)?{??? ????????????????$gameNamePiece?=?mb_substr($game['name'],?$i,?1,?'utf-8');?? ????????????????$splitedGames[$gameNamePiece][]?=?$game['id'];?? ????????????}?? ????????}?? ????????return?$splitedGames;?? ????}?? ?? }??
database.php
[php]?view plaincopy
<?php??? class?Database{?? ?????? ????private?static?$_dbConfig?=?array(?? ????????'host'?=>?'127.0.0.1',?? ????????'username'?=>?'root',?? ????????'pwd'?=>?'',?? ????????'dbname'?=>?'bussiness'?? ????????);?? ?? ????private?static?$_instance;?? ?? ????public?static?function?getInstance(){?? ????????if(is_null(self::$_instance)){?? ????????????self::$_instance?=?new?mysqli(self::$_dbConfig['host'],?self::$_dbConfig['username'],?self::$_dbConfig['pwd'],?self::$_dbConfig['dbname']);??? ????????????if(self::$_instance->connect_errno){?? ????????????????throw?new?Exception(self::$_instance->connect_error);?? ????????????}?? ????????}?? ????????return?self::$_instance;?? ????}?? ?? ?? }??
index.php
[php]?view plaincopy
<?php??? require_once?'game.php';?? require_once?'iredis.php';?? ?? $redis?=?new?iRedis();?? $game?=?new?Game();?? $game->createIndexes();?? var_dump($game->getHint('雄英'));??
數據庫中game表中的測試數據入下截圖所示:
執行index.php中的代碼結果如下圖所示:
點評:可以進一步修改,直接使用redis自身的功能求交集。
總結
以上是生活随笔為你收集整理的redis实战:使用redis实现自动补全的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。