溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

利用PHP使用Redis常見的使用場景有哪些

發(fā)布時間:2021-11-25 17:47:06 來源:億速云 閱讀:170 作者:小新 欄目:大數(shù)據(jù)

這篇文章主要為大家展示了“利用PHP使用Redis常見的使用場景有哪些”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“利用PHP使用Redis常見的使用場景有哪些”這篇文章吧。

簡單字符串緩存實戰(zhàn)

$redis->connect('127.0.0.1', 6379);
$strCacheKey  = 'Test_bihu'; //SET 應(yīng)用 $arrCacheData = [ 'name' => 'job', 'sex' => '男', 'age' => '30' ];
$redis->set($strCacheKey, json_encode($arrCacheData));
$redis->expire($strCacheKey, 30); # 設(shè)置30秒后過期 $json_data = $redis->get($strCacheKey);
$data = json_decode($json_data);
print_r($data->age); //輸出數(shù)據(jù) //HSET 應(yīng)用 $arrWebSite = [ 'google' => [ 'google.com', 'google.com.hk' ],
];
$redis->hSet($strCacheKey, 'google', json_encode($arrWebSite['google']));
$json_data = $redis->hGet($strCacheKey, 'google');
$data = json_decode($json_data);
print_r($data); //輸出數(shù)據(jù)

簡單隊列實戰(zhàn)

 $redis->connect(, );
$strQueueName  = ;  $redis->rpush($strQueueName, json_encode([ => , => ]));
$redis->rpush($strQueueName, json_encode([ => , => ]));
$redis->rpush($strQueueName, json_encode([ => , => ]));  ;  $strCount = $redis->lrange($strQueueName, , );  ;
print_r($strCount);  $redis->lpop($strQueueName);  ;  $strCount = $redis->lrange($strQueueName, , );  ;
print_r($strCount);

簡單發(fā)布訂閱實戰(zhàn)

//以下是 pub.php 文件的內(nèi)容 cli下運行 ini_set('default_socket_timeout', -1);
$redis->connect('127.0.0.1', 6379);
$strChannel = 'Test_bihu_channel'; //發(fā)布 $redis->publish($strChannel, "來自{$strChannel}頻道的推送"); echo "---- {$strChannel} ---- 頻道消息推送成功~ <br/>";
$redis->close();
//以下是 sub.php 文件內(nèi)容 cli下運行 ini_set('default_socket_timeout', -1);
$redis->connect('127.0.0.1', 6379);
$strChannel = 'Test_bihu_channel'; //訂閱 echo "---- 訂閱{$strChannel}這個頻道,等待消息推送...----  <br/><br/>";
$redis->subscribe([$strChannel], 'callBackFun'); function callBackFun($redis, $channel, $msg) {
    print_r([ 'redis' => $redis, 'channel' => $channel, 'msg' => $msg
    ]);
}

簡單計數(shù)器實戰(zhàn)

$redis->connect('127.0.0.1', 6379);
$strKey = 'Test_bihu_comments'; //設(shè)置初始值 $redis->set($strKey, 0);

$redis->INCR($strKey); //+1 $redis->INCR($strKey); //+1 $redis->INCR($strKey); //+1 $strNowCount = $redis->get($strKey); echo "---- 當(dāng)前數(shù)量為{$strNowCount}。 ---- ";

排行榜實戰(zhàn)

$redis->connect(, );
$strKey = ;  $redis->zadd($strKey, , json_encode([ => ]));
$redis->zadd($strKey, , json_encode([ => ]));
$redis->zadd($strKey, , json_encode([ => ]));
$redis->zadd($strKey, , json_encode([ => ]));
$redis->zadd($strKey, , json_encode([ => ]));

$dataOne = $redis->ZREVRANGE($strKey, , , );  ;
print_r($dataOne);

$dataTwo = $redis->ZRANGE($strKey, , , );  ;
print_r($dataTwo);

簡單字符串悲觀鎖實戰(zhàn)

解釋:悲觀鎖(Pessimistic Lock), 顧名思義,就是很悲觀。

每次去拿數(shù)據(jù)的時候都認為別人會修改,所以每次在拿數(shù)據(jù)的時候都會上鎖。

場景:如果項目中使用了緩存且對緩存設(shè)置了超時時間。

當(dāng)并發(fā)量比較大的時候,如果沒有鎖機制,那么緩存過期的瞬間,

大量并發(fā)請求會穿透緩存直接查詢數(shù)據(jù)庫,造成雪崩效應(yīng)。

/**
 * 獲取鎖
 * @param String  $key    鎖標識
 * @param Int     $expire 鎖過期時間
 * @return Boolean
 */ public function lock($key = '', $expire = 5) {
    $is_lock = $this->_redis->setnx($key, time()+$expire); //不能獲取鎖 if(!$is_lock){ //判斷鎖是否過期 $lock_time = $this->_redis->get($key); //鎖已過期,刪除鎖,重新獲取 if (time() > $lock_time) {
            unlock($key);
            $is_lock = $this->_redis->setnx($key, time() + $expire);
        }
    } return $is_lock? true : false;
} /**
 * 釋放鎖
 * @param String  $key 鎖標識
 * @return Boolean
 */ public function unlock($key = ''){ return $this->_redis->del($key);
} // 定義鎖標識 $key = 'Test_bihu_lock'; // 獲取鎖 $is_lock = lock($key, 10); if ($is_lock) { echo 'get lock success<br>'; echo 'do sth..<br>';
    sleep(5); echo 'success<br>';
    unlock($key);
} else { //獲取鎖失敗 echo 'request too frequently<br>';
}

簡單事務(wù)的樂觀鎖實戰(zhàn)

解釋:樂觀鎖(Optimistic Lock), 顧名思義,就是很樂觀。

每次去拿數(shù)據(jù)的時候都認為別人不會修改,所以不會上鎖。

watch命令會監(jiān)視給定的key,當(dāng)exec時候如果監(jiān)視的key從調(diào)用watch后發(fā)生過變化,則整個事務(wù)會失敗。

也可以調(diào)用watch多次監(jiān)視多個key。這樣就可以對指定的key加樂觀鎖了。

注意watch的key是對整個連接有效的,事務(wù)也一樣。

如果連接斷開,監(jiān)視和事務(wù)都會被自動清除。

當(dāng)然了exec,discard,unwatch命令都會清除連接中的所有監(jiān)視。

$strKey = 'Test_bihu_age';

$redis->set($strKey,10);

$age = $redis->get($strKey); echo "---- Current Age:{$age} ---- <br/><br/>";

$redis->watch($strKey); // 開啟事務(wù) $redis->multi(); //在這個時候新開了一個新會話執(zhí)行 $redis->set($strKey,30); //新會話 echo "---- Current Age:{$age} ---- <br/><br/>"; //30 $redis->set($strKey,20);

$redis->exec();

$age = $redis->get($strKey); echo "---- Current Age:{$age} ---- <br/><br/>"; //30 //當(dāng)exec時候如果監(jiān)視的key從調(diào)用watch后發(fā)生過變化,則整個事務(wù)會失敗

以上是“利用PHP使用Redis常見的使用場景有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI