Kaynağa Gözat

add to local

stanley-king 8 yıl önce
ebeveyn
işleme
caec9dd3d6

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

@@ -434,32 +434,67 @@ function getIp(){
  */
 function Model($model = null, $base_path = null)
 {
-	static $_cache = array();
-	$cache_key = $model.'.'.$base_path;
-	if (!is_null($model) && isset($_cache[$cache_key]))
-	{
-		$ret = $_cache[$cache_key];
-        $ret->init($model);
-		return $_cache[$cache_key];
-    }
+    if(is_mobile())
+    {
+        static $name_cache = array();
 
-	$base_path = $base_path == null ? BASE_DATA_PATH : $base_path;
-	$file_name = $base_path.'/model/'.$model.'.model.php';
-	$class_name = $model.'Model';
+        $base_path = $base_path == null ? BASE_DATA_PATH : $base_path;
+        $file_name = $base_path.'/model/'.$model.'.model.php';
+        $class_name = $model.'Model';
 
-	if (!file_exists($file_name)){
-		return $_cache[$cache_key] =  new Model($model);
-	}
+        if(array_key_exists($class_name,$name_cache)) {
+            return new $class_name();
+        }
+        else
+        {
+            if (!file_exists($file_name)){
+                return new Model($model);
+            }
+            else
+            {
+                require_once($file_name);
+
+                if (!class_exists($class_name)) {
+                    $error = 'Model Error:  Class '.$class_name.' is not exists!';
+                    throw_exception($error);
+                } else {
+                    $name_cache[$class_name] = $class_name;
+                    return new $class_name();
+                }
+            }
+        }
+    }
     else
     {
-		require_once($file_name);
-		if (!class_exists($class_name)) {
-			$error = 'Model Error:  Class '.$class_name.' is not exists!';
-			throw_exception($error);
-		}else{
-			return $_cache[$cache_key] = new $class_name();
-		}
-	}
+        static $_cache = array();
+        $cache_key = $model.'.'.$base_path;
+        if (!is_null($model) && isset($_cache[$cache_key]))
+        {
+            $ret = $_cache[$cache_key];
+            $ret->init($model);
+            return $_cache[$cache_key];
+        }
+
+        $base_path = $base_path == null ? BASE_DATA_PATH : $base_path;
+        $file_name = $base_path.'/model/'.$model.'.model.php';
+        $class_name = $model.'Model';
+
+        if (!file_exists($file_name)){
+            return $_cache[$cache_key] =  new Model($model);
+        }
+        else
+        {
+            require_once($file_name);
+            if (!class_exists($class_name)) {
+                $error = 'Model Error:  Class '.$class_name.' is not exists!';
+                throw_exception($error);
+            }else{
+                return $_cache[$cache_key] = new $class_name();
+            }
+        }
+
+    }
+
 }
 
 /**

+ 5 - 1
core/framework/libraries/model.php

@@ -22,8 +22,12 @@ class Model
         $this->table_prefix = DBPRE;
         $this->init($table);
 	}
+	public function __destruct()
+    {
+        Log::record("{$this->table_name} __destruct",Log::DEBUG);
+    }
 
-	public function init($table)
+    public function init($table)
     {
         $this->options = [];
         $this->fields = [];

+ 14 - 5
test/DBTest.php

@@ -8,7 +8,7 @@
  */
 
 define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
-
+define('MOBILE_SERVER',true);
 require_once(BASE_ROOT_PATH . '/fooder.php');
 
 class DBTest extends PHPUnit_Framework_TestCase
@@ -29,11 +29,20 @@ class DBTest extends PHPUnit_Framework_TestCase
     }
     public function testModel()
     {
-        $member1 = Model('member');
-        $items1 = $member1->field('*')->limit(false)->select();
+        $result = Model()->table('config')->select();
+        
+        $y = Model('member');
+
+        $start = microtime(true);
+        for ($i = 0; $i < 100; ++$i) {
+            $member1 = Model('member');
+        }
+        $t = $member1;
+        $period = microtime(true) - $start;
+
+        $x = sprintf("%.6f",$period);
 
-        $member2 = Model('member');
-        $items2 = $member2->field('*')->limit(false)->select();
+        echo  $period;
     }
 
     public function testConnection()