淺談redis在項目中的應(yīng)用
更新時間:2016年12月14日 15:04:12 投稿:jingxian
下面小編就為大家?guī)硪黄獪\談redis在項目中的應(yīng)用。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
redis在項目中的應(yīng)用 ps:PHP 會自動 關(guān)redis連接 不需要手動關(guān) 對于臨時的數(shù)據(jù) 可以不經(jīng)過數(shù)據(jù)庫直接redis上操作
/*消息隊列實例 */
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();
}
/*讀取實例*/
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();
}
/*更新實例*/
public function updateinfo(){
//運行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');
}
/*刪除實例*/
public function deleteinfo(){
//運行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在項目中的應(yīng)用全部內(nèi)容了,希望大家多多支持腳本之家~
相關(guān)文章
Redis實現(xiàn)主從復制方式(Master&Slave)
這篇文章主要介紹了Redis實現(xiàn)主從復制方式(Master&Slave),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
Redis中的3種特殊數(shù)據(jù)結(jié)構(gòu)詳解
在本文中,我們對三種特殊的數(shù)據(jù)類型進行了介紹,它們分別是geospatial(地理空間數(shù)據(jù)類型)、HyperLogLogs和Bitmaps(位圖),這些數(shù)據(jù)類型在不同的領(lǐng)域和應(yīng)用中發(fā)揮著重要作用,并且具有各自獨特的特性和用途,對Redis特殊數(shù)據(jù)結(jié)構(gòu)相關(guān)知識感興趣的朋友一起看看吧2024-02-02
如何利用Redis?List實現(xiàn)Java數(shù)據(jù)庫分頁快速查詢
這篇文章主要給大家介紹了關(guān)于如何利用Redis?List實現(xiàn)Java數(shù)據(jù)庫分頁快速查詢的相關(guān)資料,Redis是一個高效的內(nèi)存數(shù)據(jù)庫,它支持包括String、List、Set、SortedSet和Hash等數(shù)據(jù)類型的存儲,需要的朋友可以參考下2024-02-02
讓Redis在你的系統(tǒng)中發(fā)揮更大作用的幾點建議
Redis在很多方面與其他數(shù)據(jù)庫解決方案不同:它使用內(nèi)存提供主存儲支持,而僅使用硬盤做持久性的存儲;它的數(shù)據(jù)模型非常獨特,用的是單線程。另一個大區(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

