3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php //https://github.com/nicolasff/phpredis 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 redis2'); $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(DEBUG_INFO) { $handle2=fopen('redis_multi_log.txt','a'); fwrite($handle2,date('H:i:s')."\t$msg"); fclose($handle2); } 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']); // call_user_func_array(array( $this->obj, $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'); } } } } class Redis_Cache { private $obj; private $ip; private $port; private $timeout; //public $site_id = 0; protected static $key_request = array(); // private static $instance; protected static $count_requests = 0; /* public static function getInstance() { self::$count_requests++; return (self::$instance === null) ? self::$instance = new self() : self::$instance; }*/ public function set_settings($redis , $ip = '127.0.0.1' , $port ='6379' , $timeout = 0) { $this->ip=$ip; $this->port=$port; $this->timeout=$timeout; $this->obj = $redis; $this->rconnect($redis); } private function rconnect() { $this->obj->pconnect($this->ip,$this->port,$this->timeout) or die('Error connecting redis'); } public function __call($name, $arguments) { $i=0; self::$key_request[] = $arguments[0]; while(true) { try{ j2_timeStamps::getInstance()->addCacheTime($arguments[0] . ' (' . $name . ')'); $return = call_user_func_array(array($this->obj, $name), $arguments); j2_timeStamps::getInstance()->addCacheTime($arguments[0] . ' (' . $name . ')'); return $return; 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(DEBUG_INFO) { $handle2=fopen('redis_log.txt','a'); fwrite($handle2,date('H:i:s')."\t$msg"); fclose($handle2); } if(substr(trim($err),0,37)=='Caught exception: protocol error, got') die('bye'); $this->rconnect(); } /* public function set($name , $data) { return $this->obj->set($name , serialize($data)); } */ public function set($name , $time=null , $data) { // self::$key_request[] = $name; if (empty($time)) { j2_timeStamps::getInstance()->addCacheTime($name . ' (set)'); $return = $this->obj->set($name , serialize($data)); j2_timeStamps::getInstance()->addCacheTime($name . ' (set)'); return $return; } else { j2_timeStamps::getInstance()->addCacheTime($name . ' (setex)'); $return = $this->obj->setex($name , $time , serialize($data)); j2_timeStamps::getInstance()->addCacheTime($name . ' (setex)'); return $return; } } public function destroy($name) { j2_timeStamps::getInstance()->addCacheTime($name . ' (del)'); $return = $this->obj->del($name); j2_timeStamps::getInstance()->addCacheTime($name . ' (del)'); return $return; } public function get($name) { j2_timeStamps::getInstance()->addCacheTime($name . ' (get)'); $return = unserialize($this->obj->get($name)); j2_timeStamps::getInstance()->addCacheTime($name . ' (get)'); return $return; } public function get_real($name) { j2_timeStamps::getInstance()->addCacheTime($name . ' (get_real)'); $return = $this->obj->get($name); j2_timeStamps::getInstance()->addCacheTime($name . ' (get_real)'); return $return; } public function get_keys_by_prefix($name) { j2_timeStamps::getInstance()->addCacheTime($name . ' (keys)'); $return = $this->obj->keys($name . "*"); j2_timeStamps::getInstance()->addCacheTime($name . ' (keys)'); return $return; } //purges all cache public function purge() { j2_timeStamps::getInstance()->addCacheTime($name . ' (flushAll)'); $return = $this->obj->flushAll(); j2_timeStamps::getInstance()->addCacheTime($name . ' (flushAll)'); return $return; } /* public function exists($name) { return $this->obj->exists($name); } */ public function destroy_by_prefix($prefix) { j2_timeStamps::getInstance()->addCacheTime($prefix . ' (destroy_by_prefix)'); $return = $this->destroy($this->obj->keys($prefix . "*")); j2_timeStamps::getInstance()->addCacheTime($prefix . ' (destroy_by_prefix)'); return $return; } public function get_requests() { return self::$count_requests; } public function get_key_requests() { return self::$key_request; } public function lifetime($name) { j2_timeStamps::getInstance()->addCacheTime($name . ' (TTL)'); $return = $this->obj->TTL($name); j2_timeStamps::getInstance()->addCacheTime($name . ' (TTL)'); return $return; } /* public function sAdd($keys , $member) { return $this->obj->sAdd($keys , $member); }/**/ /* public function multi($type) { return new RedisMultiErrHandler($this->obj,$type,$this->ip,$this->port,$this->timeout); } */ } /** * Сохраняет данные кеша в статическом массыве, чтобы по несколько раз не дергать кеш */ class Cache extends Redis_Cache { private static $instance; static $static_cache = array(); public static function getInstance() { parent::$count_requests++; return (self::$instance === null) ? self::$instance = new self() : self::$instance; } public function destroy($name, $static_cache = true) { if(is_array($name)) { foreach ($name as $value) { $key = md5($value); if($static_cache) { unset(self::$static_cache[$key]); } parent::destroy($value); } return true; } $key = md5($name); if(array_key_exists($key, self::$static_cache)) { unset(self::$static_cache[$key]); } return parent::destroy($name); } public function get($name, $static_cache = true) { if($static_cache) { $key = md5($name); if(array_key_exists($key, self::$static_cache)) { return self::$static_cache[$key]; } } $return = parent::get($name); if($static_cache) { if($return !== false) { self::$static_cache[$key] = $return; } } return $return; } public function set($name , $time=null , $data = null, $static_cache = true) { $key = md5($name); if($static_cache) { self::$static_cache[$key] = $data; } return parent::set($name, $time, $data); } public function purge() { self::$static_cache = array(); return parent::purge(); } public function hSet($name, $index, $data, $static_cache = true) { if($static_cache) { $key = md5($name); //$index_key = md5($index); $index_key = $index; if(!array_key_exists($key, self::$static_cache)) { self::$static_cache[$key] = array(); } self::$static_cache[$key][$index_key] = $data; } return parent::hSet($name, $index, $data); } public function hGet($name, $index, $static_cache = true) { if($static_cache) { $key = md5($name); //$index_key = md5($index); $index_key = $index; if(array_key_exists($key, self::$static_cache) && array_key_exists($index, self::$static_cache[$key])) { return self::$static_cache[$key][$index_key]; } } $return = parent::hGet($name, $index); if($static_cache) { if($return !== false) { if(!array_key_exists($key, self::$static_cache)) { self::$static_cache[$key] = array(); } self::$static_cache[$key][$index_key] = $return; } } return $return; } /* public function hGetAll($name) { $key = md5($name); if(array_key_exists($key, self::$static_cache)) { return self::$static_cache[$key]; } $return = parent::hGetAll($name); if($return !== false) { self::$static_cache[$key] = $return; } return $return; }*/ }
Output for 5.3.0 - 7.1.0