public function __call($name, $arguments) { $i=0; while(true) { try{ return call_user_func_array(array($this->obj, $name), $arguments); break; } catch (Exception $e) { $this->handle_exception($e,$name,$arguments); if($i<5) $i++; else die('5 time redis lug'); } } }
class RedisErrHandler { private $obj; private $ip; private $port; private $timeout; public function __construct($ip,$port,$timeout=0) { $this->ip=$ip; $this->port=$port; $this->timeout=$timeout; $this->rconnect(); } private function rconnect() { $this->obj=new Redis; $this->obj->connect($this->ip,$this->port,$this->timeout) or die('Error connecting redis'); } public function __call($name, $arguments) { $i=0; while(true) { try{ return call_user_func_array(array($this->obj, $name), $arguments); break; } catch (Exception $e) { $this->handle_exception($e,$name,$arguments); if($i<5) $i++; else die('5 time redis lug'); } } } private function handle_exception($e,$name,$args) { $err=$e->getMessage(); $msg="Caught exception: ".$err."\tcall ".$name."\targs ".implode(" ",$args)."\n"; if($_SERVER['LOG']) { $handle2=fopen('redis_log.txt','a'); fwrite($handle2,date('H:i:s')."\t$msg"); fclose($handle2); } echo $msg; if(substr(trim($err),0,37)=='Caught exception: protocol error, got') die('bye'); $this->rconnect(); } }
$r=new Redis(); $r->connect('127.0.0.1',6379,10);
on $r=new RedisErrHandler('127.0.0.1',6379,10);
public function multi($type) { return new RedisMultiErrHandler($this->obj,$type,$this->ip,$this->port,$this->timeout); }
class RedisMultiErrHandler { private $obj; private $ip; private $port; private $timeout; private $m; private $type; private $commands; public function __construct(&$redis,$type,$ip,$port,$timeout=0) { $this->ip=$ip; $this->port=$port; $this->timeout=$timeout; $this->type=$type; $this->obj=$redis; $this->m=$this->obj->multi($type); } private function rconnect() { $this->obj=new Redis; $this->obj->connect($this->ip,$this->port,$this->timeout) or die('Error connecting redis'); $this->m=$this->obj->multi($this->type); } public function __call($name, $arguments) { $this->commands[]=array('name'=>$name, 'arguments'=>$arguments); return $this; } private function handle_exception($e) { $err=$e->getMessage(); $msg=''; foreach($this->commands as $command) { $msg.="Multi sent\tcall ".$command['name']."\targs ".implode(" ",$command['arguments'])."\n"; } $msg.="Caught exception: ".$err."\n"; if($_SERVER['LOG']) { $handle2=fopen('redis_multi_log.txt','a'); fwrite($handle2,date('H:i:s')."\t$msg"); fclose($handle2); } echo $msg; if(substr(trim($err),0,37)=='Caught exception: protocol error, got') die('bye'); $this->rconnect(); } public function exec() { $i=0; while(true) { foreach($this->commands as $command) { call_user_func_array(array($this->m, $command['name']), $command['arguments']); } try{ return $this->m->exec(); break; } catch (Exception $e) { $this->handle_exception($e); if($i<5) $i++; else die('5 time mredis lug'); } } } }
Source: https://habr.com/ru/post/137536/
All Articles