Parcourir la source

fix old cache redis client cannot connect server when server has down.

stanley-king il y a 3 ans
Parent
commit
5b54638a79

+ 6 - 6
core/framework/cache/cache.redis.php

@@ -106,7 +106,7 @@ class CacheRedis extends Cache
 
     public function get_org($key,$prefix = '')
     {
-        $this->init_master();
+        $this->init_slave();
         if (!$this->enable) return false;
         $this->type = $prefix;
         $result = $this->handler->get($this->_key($key));
@@ -240,6 +240,7 @@ class CacheRedis extends Cache
     }
 
     public function hIncrByFloat($name, $prefix, $key, $num = 1) {
+        $this->init_master();
         if ($this->hget($name, $prefix,$key) !== false) {
             $this->handler->hIncrByFloat($this->_key($name), $key, floatval($num));
         }
@@ -262,13 +263,12 @@ class CacheRedis extends Cache
 		return $this->prefix.$this->type.$str;
 	}
 
-    public function keys($key = '') {
-        $this->init_master();
-
+    public function keys($key = '')
+    {
+        $this->init_slave();
         if(!isset($key) || empty($key)) {
             $key = '*';
         }
-
         return $this->handler->keys($this->_key($key));
     }
 
@@ -279,7 +279,7 @@ class CacheRedis extends Cache
     }
 
     public function lLen($key) {
-        $this->init_master();
+        $this->init_slave();
         if (!$this->enable) return false;
         return $this->handler->lLen($key);
     }

+ 22 - 21
core/framework/function/core.php

@@ -1685,27 +1685,28 @@ function compress_code($content)
  */
 function get_obj_instance($class, $method='', $args = array())
 {
-	static $_cache = array();
-	$key = $class.$method.(empty($args) ? null : md5(serialize($args)));
-	if (isset($_cache[$key])){
-		return $_cache[$key];
-	}else{
-		if (class_exists($class)){
-			$obj = new $class;
-			if (method_exists($obj,$method)){
-				if (empty($args)){
-					$_cache[$key] = $obj->$method();
-				}else{
-					$_cache[$key] = call_user_func_array(array(&$obj, $method), $args);
-				}
-			}else{
-				$_cache[$key] = $obj;
-			}
-			return $_cache[$key];
-		}else{
-			throw_exception('Class '.$class.' isn\'t exists!');
-		}
-	}
+    static $_cache = [];
+    $key = $class . $method . (empty($args) ? null : md5(serialize($args)));
+    if (isset($_cache[$key])) {
+        return $_cache[$key];
+    }
+    elseif (class_exists($class))
+    {
+        $obj = new $class;
+        if (method_exists($obj, $method))
+        {
+            if (empty($args)) {
+                $_cache[$key] = $obj->$method();
+            } else {
+                $_cache[$key] = call_user_func_array([&$obj, $method], $args);
+            }
+        } else {
+            $_cache[$key] = $obj;
+        }
+        return $_cache[$key];
+    } else {
+        throw_exception('Class ' . $class . ' isn\'t exists!');
+    }
 }
 
 /**

+ 0 - 2
helper/message/msgutil.php

@@ -138,8 +138,6 @@ class StatesHelper
 
 function handler_redis($redis, $chan, $msg)
 {
-    Log::record("message: {$chan} -- {$msg}",Log::DEBUG);
-
     if (defined('UGC_SUBSCRIBER_PROC') && UGC_SUBSCRIBER_PROC == true)
     {
         if($chan == 'special') {

+ 1 - 1
helper/message/subscriber.php

@@ -58,7 +58,7 @@ class subscriber extends Thread
             catch (Exception $ex)
             {
                 Log::record("subscriber quit err={$ex->getMessage()} code={$ex->getCode()}");
-                sleep(1);
+                sleep(5);
             }
         }
     }

+ 4 - 2
test/TestHttp.php

@@ -29,6 +29,8 @@ class TestHttp extends TestCase
 
     public function testIPS()
     {
+        $ins = Cache::getInstance('cacheredis');
+        $ins->hdel('', '', '');
         $x = 'a:2:{i:0;s:14:"222.129.49.232";i:1;s:14:"222.129.55.246";}';
         $x = unserialize($x);
         $x[] = '';
@@ -37,8 +39,8 @@ class TestHttp extends TestCase
 
     public function testRefillAdd()
     {
-        $time = time() * 1000;
-        for ($i = 1;$i < 1000; $i++) {
+        $time = time() * 10;
+        for ($i = 1;$i < 10; $i++) {
             $this->push_order(1092, $time, $i);
         }
     }