日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

redis实战:使用redis实现自动补全

發布時間:2024/1/23 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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);??
  • ????}??
  • ??
  • ????//獲取某個key之下的redis數據??
  • ????public?function?getIndex($key){??
  • ????????return?$this->sMembers($key);??
  • ????}??
  • ??
  • ????//刪除某個key之下的redis數據??
  • ????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));??
  • ????????}??
  • ????}??
  • ??
  • ????//根據id獲取游戲名??
  • ????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){??
  • ????????????//?$gameids?=?unserialize($this->redis->getIndex($keyword));??
  • ????????????$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实现自动补全的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。