本文給大家介紹的是關(guān)于PHP項目中redis短線重連的實現(xiàn),下文有給大家介紹redis短線重連以及兩種實現(xiàn),都有具體的代碼及詳解供大家參考,需要的朋友可以參考了解看看,那么接下來就跟隨小編一起了解一下吧。
????在swoole ,workerman等cli長連接模式下,遇到Redis異常斷開,后面又開啟的情況,一般得重新啟動程序才能正常使用,本文介紹在不重啟服務(wù),實現(xiàn)原來的Redis斷線重連
????原理
????Redis 斷開的情況下調(diào)用
$Redis->ping()會觸發(fā)Notice錯誤,Notice: Redis::ping(): send of 14 bytes failed with errno=10054
????當獲取redis實例時,如果ping不通或者出現(xiàn)異常,就重新連接
????實現(xiàn)1
????因為try catch? 捕捉不到notice異常,所以ping不通直接重新連接,catch捕捉新連接的實例沒有連接上,下次執(zhí)行ping觸發(fā)
Redis server went away 異常
public static function getInstance( ) { try { if (!self::$_instance) { new self(); } else { if (!self::$_instance->ping()) new self(); } } catch (\Exception $e) { // 斷線重連 new self(); } return self::$_instance; }
????實現(xiàn)2
????1.調(diào)用ping之前先拋出個notice異常,
????2.調(diào)用ping
????3.用error_get_last獲取最后一個錯誤,如果錯誤信息跟我們拋出的一樣,說明ping通了,否則拋出個異常 ,讓catch捕捉到執(zhí)行重連,
????當重連一次沒連上再次調(diào)用$_instance->ping()會直接拋出Redis server went away異常讓catch捕捉到
public static function getInstance( ) { if (!self::$_instance) { new self(); } else{ try { @trigger_error('flag', E_USER_NOTICE); self::$_instance->ping(); $error = error_get_last(); if($error['message'] != 'flag') throw new \Exception('Redis server went away'); } catch (\Exception $e) { // 斷線重連 new self(); } } return self::$_instance; }
????Redis類完整代碼
<?php namespace lib; class Redis { private static $_instance; //存儲對象 private function __construct( ){ $config = Config::get('redis'); self::$_instance = new \Redis(); //從配置讀取 self::$_instance->pconnect($config['host'], $config['port']); if ('' != $config['password']) { self::$_instance->auth($config['password']); } } public static function getInstance( ) { if (!self::$_instance) { new self(); } else{ try { @trigger_error('flag', E_USER_NOTICE); self::$_instance->ping(); $error = error_get_last(); if($error['message'] != 'flag') throw new \Exception('Redis server went away'); } catch (\Exception $e) { // 斷線重連 new self(); } } return self::$_instance; } // public static function getInstance( ) // { // try { // if (!self::$_instance) { // new self(); // } else { // if (!self::$_instance->ping()) // new self(); // } // } catch (\Exception $e) { // // 斷線重連 // new self(); // } // return self::$_instance; // } /** * 禁止clone */ private function __clone(){} /** * 其他方法自動調(diào)用 * @param $method * @param $args * @return mixed */ public function __call($method,$args) { return call_user_func_array([self::$_instance, $method], $args); } /** * 靜態(tài)調(diào)用 * @param $method * @param $args * @return mixed */ public static function __callStatic($method,$args) { self::getInstance(); return call_user_func_array([self::$_instance, $method], $args); } }
????調(diào)用
$this->handler = Redis::getInstance(); $key = $this->getCacheKey($name); $value = $this->handler->get($key);
????補充
????pconnect建立連接后重連失敗問題
????經(jīng)測試長連接下使用pconnect建立連接后,redis超時被動斷開了鏈接,
$res = self::$_instance->pconnect($config['host'], $config['port']);
????$res 會返回true,但不是新建的鏈接,調(diào)用$res-get()會報錯
????原因
????研究發(fā)現(xiàn)
????使用pconnect,鏈接在php進程的整個生命周期內(nèi)被重用,?close的作用僅是使當前php不能再進行redis請求,但無法真正關(guān)閉redis長連接,連接在后續(xù)請求中仍然會被重用,直至fpm進程生命周期結(jié)束。
????長連接中只有進程被停止,連接才會斷開,所以連接斷開時new不起作用,返回連接成功,而事實上已經(jīng)斷了,還是最早的那個連接,從而導致不能進行后續(xù)讀取數(shù)據(jù)操作,所以長連接中請使用connect。
? ? 關(guān)于PHP項目中redis短線重連的實現(xiàn)就介紹到這,本文方法有一定的參考價值,感興趣的朋友可以參考,希望能對大家有幫助,想要了解更多PHP的內(nèi)容,大家可以關(guān)注其它的相關(guān)文章。