久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      PHP實戰(zhàn)之Redis常見7種使用場景

      PHP實戰(zhàn)之Redis常見7種使用場景

      Redis是一個開源的使用ANSI C語言編寫、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫,并提供多種語言的API。

      本篇文章,主要介紹利用PHP使用Redis,主要的應(yīng)用場景。

      簡單字符串緩存實戰(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ù)復(fù)制代碼

      簡單隊列實戰(zhàn)

      $redis->connect('127.0.0.1', 6379);$strQueueName  = 'Test_bihu_queue';  //進(jìn)隊列$redis->rpush($strQueueName, json_encode(['uid' => 1,'name' => 'Job']));$redis->rpush($strQueueName, json_encode(['uid' => 2,'name' => 'Tom']));$redis->rpush($strQueueName, json_encode(['uid' => 3,'name' => 'John']));echo "---- 進(jìn)隊列成功 ---- <br /><br />";  //查看隊列$strCount = $redis->lrange($strQueueName, 0, -1);echo "當(dāng)前隊列數(shù)據(jù)為: <br />"; print_r($strCount);  //出隊列$redis->lpop($strQueueName);echo "<br /><br /> ---- 出隊列成功 ---- <br /><br />";  //查看隊列$strCount = $redis->lrange($strQueueName, 0, -1);echo "當(dāng)前隊列數(shù)據(jù)為: <br />"; print_r($strCount);復(fù)制代碼

      簡單發(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();復(fù)制代碼
      //以下是 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 	]); }復(fù)制代碼

      簡單計數(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}。 ---- ";復(fù)制代碼

      排行榜實戰(zhàn)

      $redis->connect('127.0.0.1', 6379);$strKey = 'Test_bihu_score';  //存儲數(shù)據(jù)$redis->zadd($strKey, '50', json_encode(['name' => 'Tom']));$redis->zadd($strKey, '70', json_encode(['name' => 'John']));$redis->zadd($strKey, '90', json_encode(['name' => 'Jerry']));$redis->zadd($strKey, '30', json_encode(['name' => 'Job']));$redis->zadd($strKey, '100', json_encode(['name' => 'LiMing']));$dataOne = $redis->ZREVRANGE($strKey, 0, -1, true);echo "---- {$strKey}由大到小的排序 ---- <br /><br />"; print_r($dataOne);$dataTwo = $redis->ZRANGE($strKey, 0, -1, true);echo "<br /><br />---- {$strKey}由小到大的排序 ---- <br /><br />"; print_r($dataTwo);復(fù)制代碼

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

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

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

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

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

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

      /**  * 獲取鎖  * @param  String  $key    鎖標(biāo)識  * @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 鎖標(biāo)識  * @return Boolean  */ public function unlock($key = ''){	return $this->_redis->del($key); }  // 定義鎖標(biāo)識$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>'; }復(fù)制代碼

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

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

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

      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ù)會失敗復(fù)制代碼

      Thanks ~


      推薦教程:《php教程》

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號