前言
我看到php框架性能比较发现有个php框架micromvc,性能测试仅次于yaf和phancon,于是好奇这是个什么样的框架
git下来后看了下代码,感觉不太给力,但有些地方还是可以借鉴的。看了一下他的orm,很简单,但不支持读写分离,于是自己动手改了一下:
上代码
配置
Config/Config.php
$config['database'] = array(
'dns' => "mysql:host=127.0.0.1;port=3306;dbname=micromvc,mysql:host=127.0.0.1;port=3306;dbname=micromvcslave",
'username' => 'root',
'password' => '',
'params' => array(),
'separate' => true, // 主从分离
'rand_rw' => false, // 随机读取
);
ORM
vendor/micro/micro/Micro/ORM.php
/**
* Create a new database entity object
*
* @param int|mixed $id of the row or row object
*/
public function __construct($id = 0, $name = 'database')
{
self::load_database($name);
$this->data = array();
if(! $id) return;
if(is_numeric($id))
{
$this->data[static::$key] = $id;
}
else
{
$this->data = (array) $id;
$this->loaded = 1;
}
$this->saved = 1;
}
/**
* Load database connection
*/
public static function load_database($name = 'database')
{
// Set default ORM database connection
if(empty(self::$db))
{
// Load database
$db = new Database(config()->$name);
self::$db = $db;
}
return self::$db;
}
Database
vendor/micro/micro/Micro/Database.php
定义:
public $pdo = array();
public function connect($type = 'master')
{
extract($this->config);
if(isset($separate) and $separate){
$dns = explode(',', $dns);
if($type == 'master')
$dns = $dns['0'];
else{
if(isset($rand_rw) and $rand_rw)
$dns = $dns[mt_rand(0, count($dns) - 1)];
else
$dns = $dns['1'];
}
}
// Clear config for security reasons
// $this->config = NULL;
// Connect to PDO
$this->pdo[$type] = new PDO($dns, $username, $password, $params);
// PDO should throw exceptions
$this->pdo[$type]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
/**
* Run a SQL query and return the statement object
*
* @param string $sql query to run
* @param array $params the prepared query params
* @return PDOStatement
*/
public function query($sql, array $params = NULL, $cache_statement = FALSE)
{
$time = microtime(TRUE);
self::$last_query = $sql;
// Connect if needed
if( ! isset($this->pdo['slave'])) $this->connect('slave');
// Should we cached PDOStatements? (Best for batch inserts/updates)
if($cache_statement)
{
$hash = md5($sql);
if(isset($this->statements[$hash]))
{
$statement = $this->statements[$hash];
}
else
{
$statement = $this->statements[$hash] = $this->pdo['slave']->prepare($sql);
}
}
else
{
$statement = $this->pdo['slave']->prepare($sql);
}
$statement->execute($params);
//$statement = $this->pdo->query($sql);
// Save query results by database type
self::$queries[$this->type][] = array(microtime(TRUE) - $time, $sql);
return $statement;
}
public function execute($sql, array $params = NULL, $cache_statement = FALSE)
{
$time = microtime(TRUE);
self::$last_query = $sql;
// Connect if needed
if( ! isset($this->pdo['master'])) $this->connect('master');
// Should we cached PDOStatements? (Best for batch inserts/updates)
if($cache_statement)
{
$hash = md5($sql);
if(isset($this->statements[$hash]))
{
$statement = $this->statements[$hash];
}
else
{
$statement = $this->statements[$hash] = $this->pdo['master']->prepare($sql);
}
}
else
{
$statement = $this->pdo['master']->prepare($sql);
}
$statement->execute($params);
//$statement = $this->pdo->query($sql);
// Save query results by database type
self::$queries[$this->type][] = array(microtime(TRUE) - $time, $sql);
return $statement;
}
至此,该orm的自动读写分离就ok了,如果手动执sql语句,则对于所有写操作,excute,所有读操作query。
如果您觉得您在我这里学到了新姿势,博主支持转载,姿势本身就是用来相互学习的。同时,本站文章如未注明均为 hisune 原创 请尊重劳动成果 转载请注明 转自: micromvc实现自动读写分离 - hisune.com
6 Comments