* support for database persistent and socket connections (performance boost)

This commit is contained in:
slawkens 2018-06-01 18:56:57 +02:00
parent adbec47fad
commit 510459b046
3 changed files with 39 additions and 14 deletions

View File

@ -64,6 +64,8 @@ $config = array(
'database_password' => '', 'database_password' => '',
'database_name' => '', 'database_name' => '',
'database_log' => false, // should database queries be logged and displayed in the page source? They will be included at the end of the .html source of the page 'database_log' => false, // should database queries be logged and displayed in the page source? They will be included at the end of the .html source of the page
'database_socket' => '', // set if you want to connect to database through socket (example: /var/run/mysqld/mysqld.sock)
'database_persistent' => false, // use database permanent connection (like server), may speed up your site
// multiworld system (only TFS 0.3) // multiworld system (only TFS 0.3)
'multiworld' => false, // use multiworld system? 'multiworld' => false, // use multiworld system?

View File

@ -9,7 +9,7 @@
*/ */
defined('MYAAC') or die('Direct access not allowed!'); defined('MYAAC') or die('Direct access not allowed!');
if(!isset($config['database_type'][0], $config['database_user'][0], $config['database_password'][0], $config['database_name'][0])) if(!isset($config['database_user'][0], $config['database_password'][0], $config['database_name'][0]))
{ {
if(isset($config['lua']['sqlType'])) {// tfs 0.3 if(isset($config['lua']['sqlType'])) {// tfs 0.3
if(isset($config['lua']['mysqlHost'])) {// tfs 0.2 if(isset($config['lua']['mysqlHost'])) {// tfs 0.2
@ -45,6 +45,9 @@ defined('MYAAC') or die('Direct access not allowed!');
$config['database_user'] = $config['lua']['mysqlUser']; $config['database_user'] = $config['lua']['mysqlUser'];
$config['database_password'] = $config['lua']['mysqlPass']; $config['database_password'] = $config['lua']['mysqlPass'];
$config['database_name'] = $config['lua']['mysqlDatabase']; $config['database_name'] = $config['lua']['mysqlDatabase'];
if(!isset($config['database_socket'][0])) {
$config['database_socket'] = isset($config['lua']['mysqlSock']) ? trim($config['lua']['mysqlSock']) : '';
}
$config['database_encryption'] = 'sha1'; $config['database_encryption'] = 'sha1';
} }
else if(isset($config['lua']['database_type'])) // otserv else if(isset($config['lua']['database_type'])) // otserv
@ -80,13 +83,19 @@ defined('MYAAC') or die('Direct access not allowed!');
$config['database_log'] = false; $config['database_log'] = false;
} }
if(!isset($config['database_socket'])) {
$config['database_socket'] = '';
}
try { try {
$ots->connect(array( $ots->connect(array(
'host' => $config['database_host'], 'host' => $config['database_host'],
'user' => $config['database_user'], 'user' => $config['database_user'],
'password' => $config['database_password'], 'password' => $config['database_password'],
'database' => $config['database_name'], 'database' => $config['database_name'],
'log' => $config['database_log'] 'log' => $config['database_log'],
'socket' => @$config['database_socket'],
'persistent' => @$config['database_persistent']
) )
); );
@ -96,7 +105,7 @@ defined('MYAAC') or die('Direct access not allowed!');
if(isset($cache) && $cache->enabled()) { if(isset($cache) && $cache->enabled()) {
$cache->delete('config_lua'); $cache->delete('config_lua');
} }
if(defined('MYAAC_INSTALL')) { if(defined('MYAAC_INSTALL')) {
return; // installer will take care of this return; // installer will take care of this
} }

View File

@ -64,16 +64,6 @@ class OTS_DB_MySQL extends OTS_Base_DB
$params['port'] = $host[1]; $params['port'] = $host[1];
} }
if( isset($params['host']) )
{
$dns[] = 'host=' . $params['host'];
}
if( isset($params['port']) )
{
$dns[] = 'port=' . $params['port'];
}
if( isset($params['database']) ) if( isset($params['database']) )
{ {
$dns[] = 'dbname=' . $params['database']; $dns[] = 'dbname=' . $params['database'];
@ -99,6 +89,10 @@ class OTS_DB_MySQL extends OTS_Base_DB
$this->logged = true; $this->logged = true;
} }
if( !isset($params['persistent']) ) {
$params['persistent'] = false;
}
global $cache, $config; global $cache, $config;
if(isset($cache) && $cache->enabled()) { if(isset($cache) && $cache->enabled()) {
$tmp = null; $tmp = null;
@ -123,7 +117,27 @@ class OTS_DB_MySQL extends OTS_Base_DB
} }
} }
parent::__construct('mysql:' . implode(';', $dns), $user, $password); if(isset($params['socket'][0])) {
$dns[] = 'unix_socket=' . $params['socket'];
parent::__construct('mysql:' . implode(';', $dns), $user, $password, array(
PDO::ATTR_PERSISTENT => $params['persistent']
));
return;
}
if( isset($params['host']) ) {
$dns[] = 'host=' . $params['host'];
}
if( isset($params['port']) ) {
$dns[] = 'port=' . $params['port'];
}
parent::__construct('mysql:' . implode(';', $dns), $user, $password, array(
PDO::ATTR_PERSISTENT => $params['persistent']
));
} }
public function __destruct() public function __destruct()