淺談redis在項目中的應用_Redis

來源:互聯網
上載者:User

redis在項目中的應用 ps:PHP 會自動 關redis串連 不需要手動關 對於臨時的資料 可以不經過資料庫直接redis上操作

/*訊息佇列執行個體 */public function insertinfo(){ //串連本地的 Redis 服務    $redis = new \Redis();    $redis->connect('127.0.0.1', 6379);    //儲存資料到列表中  $infos = array('info1' => 66, 'info2' => 88);    $redis->lpush($key, json_encode($infos));    // 擷取儲存的資料並輸出    $arList = $redis->lrange("tutorial-list", 0, 30);    print_r($arList);    exit();}/*讀取執行個體*/public function getinfo(){//串連本地的 Redis 服務$redis = new \Redis();$redis->connect('127.0.0.1', 6379);// 擷取儲存的資料並輸出$result = json_decode($redis->get("tutoriallist"),'true');if(empty($result)){$sql="select * from mobantestinfo";$VModel = new HuanShanVoteModel();$result = $VModel->query($sql);//重新將緩衝放入資料庫 redis不能直接存數組需要轉成json$redis->set(json_encode($result));}else{//串連本地的 Redis 服務$redis = new \Redis();$redis->connect('127.0.0.1', 6379);// 擷取儲存的資料並輸出$result = json_decode($redis->get("tutoriallist"),'true');}print_r($result);exit();}
/*更新執行個體*/public function updateinfo(){//運行sql語句$sql="update mobantestinfo set info1=1 where id=40";$VModel = new HuanShanVoteModel();$isOk = $VModel->execute($sql);//串連本地的 Redis 服務$redis = new \Redis();$redis->connect('127.0.0.1', 6379);/*刪除key*/$redis->del('tutoriallist');}
/*刪除執行個體*/public function deleteinfo(){//運行sql語句$sql="delete from mobantestinfo where id=40";$VModel = new HuanShanVoteModel();$isOk = $VModel->execute($sql);//串連本地的 Redis 服務$redis = new \Redis();$redis->connect('127.0.0.1', 6379);$redis->del('tutoriallist');}

以上就是小編為大家帶來的淺談redis在項目中的應用全部內容了,希望大家多多支援雲棲社區~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.