生活随笔
收集整理的這篇文章主要介紹了
redis实战:使用redis实现自动补全
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
轉(zhuǎn)載:http://blog.csdn.net/u011250882/article/details/48632379
如果我想輸入“雄英”來(lái)找到游戲庫(kù)中的所有帶有這兩個(gè)字的游戲,該怎樣用redis來(lái)實(shí)現(xiàn)呢?
原理:
1, 將所有的游戲名字讀出來(lái),拆分成單個(gè)漢字
2, 將這些漢字作為redis集合的鍵,寫入redis,每個(gè)集合里的值是所有那些游戲名字中包含此漢字的游戲的id
3, 當(dāng)用戶輸入文字的時(shí)候通過(guò)ajax異步請(qǐng)求,將用戶輸入傳給PHP
4, 將輸入的文字拆分成單個(gè)漢字, 分別找到這些漢字在redis中的集合值
5, 取出來(lái),求交集,就找到了同時(shí)包含這幾個(gè)漢字的游戲的id
6, 最后到數(shù)據(jù)庫(kù)里查出來(lái)相應(yīng)的游戲信息即可
缺點(diǎn): 刪除數(shù)據(jù)不方便
具體實(shí)現(xiàn)代碼:
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('雄英'));??
數(shù)據(jù)庫(kù)中g(shù)ame表中的測(cè)試數(shù)據(jù)入下截圖所示:
執(zhí)行index.php中的代碼結(jié)果如下圖所示:
點(diǎn)評(píng):可以進(jìn)一步修改,直接使用redis自身的功能求交集。
總結(jié)
以上是生活随笔為你收集整理的redis实战:使用redis实现自动补全的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。