淺談redis在項(xiàng)目中的應(yīng)用
redis在項(xiàng)目中的應(yīng)用 ps:PHP 會自動(dòng) 關(guān)redis連接 不需要手動(dòng)關(guān) 對于臨時(shí)的數(shù)據(jù) 可以不經(jīng)過數(shù)據(jù)庫直接redis上操作
/*消息隊(duì)列實(shí)例 */ public function insertinfo(){ //連接本地的 Redis 服務(wù) $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); //存儲數(shù)據(jù)到列表中 $infos = array('info1' => 66, 'info2' => 88); $redis->lpush($key, json_encode($infos)); // 獲取存儲的數(shù)據(jù)并輸出 $arList = $redis->lrange("tutorial-list", 0, 30); print_r($arList); exit(); } /*讀取實(shí)例*/ public function getinfo(){ //連接本地的 Redis 服務(wù) $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); // 獲取存儲的數(shù)據(jù)并輸出 $result = json_decode($redis->get("tutoriallist"),'true'); if(empty($result)){ $sql="select * from mobantestinfo"; $VModel = new HuanShanVoteModel(); $result = $VModel->query($sql); //重新將緩存放入數(shù)據(jù)庫 redis不能直接存數(shù)組需要轉(zhuǎn)成json $redis->set(json_encode($result)); }else{ //連接本地的 Redis 服務(wù) $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); // 獲取存儲的數(shù)據(jù)并輸出 $result = json_decode($redis->get("tutoriallist"),'true'); } print_r($result); exit(); }
/*更新實(shí)例*/ public function updateinfo(){ //運(yùn)行sql語句 $sql="update mobantestinfo set info1=1 where id=40"; $VModel = new HuanShanVoteModel(); $isOk = $VModel->execute($sql); //連接本地的 Redis 服務(wù) $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); /*刪除key*/ $redis->del('tutoriallist'); }
/*刪除實(shí)例*/ public function deleteinfo(){ //運(yùn)行sql語句 $sql="delete from mobantestinfo where id=40"; $VModel = new HuanShanVoteModel(); $isOk = $VModel->execute($sql); //連接本地的 Redis 服務(wù) $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); $redis->del('tutoriallist'); }
以上就是小編為大家?guī)淼臏\談redis在項(xiàng)目中的應(yīng)用全部內(nèi)容了,希望大家多多支持腳本之家~
相關(guān)文章
Redis實(shí)現(xiàn)主從復(fù)制方式(Master&Slave)
這篇文章主要介紹了Redis實(shí)現(xiàn)主從復(fù)制方式(Master&Slave),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06Redis中的3種特殊數(shù)據(jù)結(jié)構(gòu)詳解
在本文中,我們對三種特殊的數(shù)據(jù)類型進(jìn)行了介紹,它們分別是geospatial(地理空間數(shù)據(jù)類型)、HyperLogLogs和Bitmaps(位圖),這些數(shù)據(jù)類型在不同的領(lǐng)域和應(yīng)用中發(fā)揮著重要作用,并且具有各自獨(dú)特的特性和用途,對Redis特殊數(shù)據(jù)結(jié)構(gòu)相關(guān)知識感興趣的朋友一起看看吧2024-02-02如何利用Redis?List實(shí)現(xiàn)Java數(shù)據(jù)庫分頁快速查詢
這篇文章主要給大家介紹了關(guān)于如何利用Redis?List實(shí)現(xiàn)Java數(shù)據(jù)庫分頁快速查詢的相關(guān)資料,Redis是一個(gè)高效的內(nèi)存數(shù)據(jù)庫,它支持包括String、List、Set、SortedSet和Hash等數(shù)據(jù)類型的存儲,需要的朋友可以參考下2024-02-02讓Redis在你的系統(tǒng)中發(fā)揮更大作用的幾點(diǎn)建議
Redis在很多方面與其他數(shù)據(jù)庫解決方案不同:它使用內(nèi)存提供主存儲支持,而僅使用硬盤做持久性的存儲;它的數(shù)據(jù)模型非常獨(dú)特,用的是單線程。另一個(gè)大區(qū)別在于,你可以在開發(fā)環(huán)境中使用Redis的功能,但卻不需要轉(zhuǎn)到Redis2014-06-06關(guān)于Redis數(shù)據(jù)持久化的概念介紹
Redis是內(nèi)存數(shù)據(jù)庫,數(shù)據(jù)都是存儲在內(nèi)存中,需要定期將Redis中的數(shù)據(jù)以某種形式(或命數(shù)據(jù)令)從內(nèi)存保存到硬盤,今天給大家分享Redis數(shù)據(jù)的持久化的概念介紹,需要的朋友參考下吧2021-08-08