close #48 add multi DB object instance caching support

This commit is contained in:
Exodus4D
2015-10-11 19:10:29 +02:00
parent 4770fc771d
commit f0ea85bcd0
8 changed files with 87 additions and 58 deletions

View File

@@ -71,16 +71,18 @@ class Route extends \Controller\AccessController {
$this->f3->exists($cacheKeyIdArray)
){
// get cached values
$this->nameArray = $this->f3->get($cacheKeyNamedArray);
$this->jumpArray = $this->f3->get($cacheKeyJumpArray);
$this->idArray = $this->f3->get($cacheKeyIdArray);
}else{
// nothing cached
$pfDB = $this->getDB('PF');
$query = "SELECT * FROM system_neighbour";
$rows = $this->f3->get('DB')->exec($query, null, $this->jumpDataCacheTime);
$rows = $pfDB->exec($query, null, $this->jumpDataCacheTime);
foreach($rows as $row){
$regionId = $row['regionId'];
@@ -202,8 +204,8 @@ class Route extends \Controller\AccessController {
*/
private function setupSystemJumpTable(){
// switch DB
$this->setDB('CCP');
$pfDB = $this->getDB('PF');
$ccpDB = $this->getDB('CCP');
$query = "SELECT
map_sys.solarSystemID system_id,
@@ -228,18 +230,17 @@ class Route extends \Controller\AccessController {
system_neighbours IS NOT NULL
";
$rows = $this->f3->get('DB')->exec($query);
$rows = $ccpDB->exec($query);
if(count($rows) > 0){
// switch DB back to pathfinder DB
$this->setDB('PF');
// clear cache table
$query = "TRUNCATE system_neighbour";
$this->f3->get('DB')->exec($query);
$pfDB->exec($query);
foreach($rows as $row){
$this->f3->get('DB')->exec("
$pfDB->exec("
INSERT INTO
system_neighbour(
regionId,

View File

@@ -99,24 +99,20 @@ class System extends \Controller\AccessController {
$systemModels = [];
// switch DB
$this->setDB('CCP');
$ccpDB = $this->getDB('CCP');
$this->whereQuery = "WHERE
map_sys." . $column . " IN (" . implode(',', $columnIDs) . ")";
$query = $this->_getQuery();
$rows = $this->f3->get('DB')->exec($query, null, 60 * 60 * 24);
$rows = $ccpDB->exec($query, null, 60 * 60 * 24);
// format result
$mapper = new Mapper\CcpSystemsMapper($rows);
$ccpSystemsData = $mapper->getData();
// switch DB
$this->setDB('PF');
foreach($ccpSystemsData as $ccpSystemData){
$system = Model\BasicModel::getNew('SystemModel');
$system->setData($ccpSystemData);
@@ -132,12 +128,11 @@ class System extends \Controller\AccessController {
*/
public function getSystems(){
// switch DB
$this->setDB('CCP');
$ccpDB = $this->getDB('CCP');
$query = $this->_getQuery();
$rows = $this->f3->get('DB')->exec($query, null, 60 * 60 * 24);
$rows = $ccpDB->exec($query, null, 60 * 60 * 24);
// format result
$mapper = new Mapper\CcpSystemsMapper($rows);
@@ -152,9 +147,7 @@ class System extends \Controller\AccessController {
*/
public function search($f3, $params){
// switch DB
\DB\Database::instance();
$this->setDB('CCP');
$ccpDB = $this->getDB('CCP');
$searchToken = '';
// check for search parameter
@@ -167,7 +160,7 @@ class System extends \Controller\AccessController {
$query = $this->_getQuery();
$rows = $f3->get('DB')->exec($query);
$rows = $ccpDB->exec($query);
// format result
$mapper = new Mapper\CcpSystemsMapper($rows);

View File

@@ -65,9 +65,10 @@ class Controller {
/**
* set change the DB connection
* @param string $database
* @return mixed|void
*/
protected function setDB($database = 'PF'){
DB\Database::instance()->setDB($database);
protected function getDB($database = 'PF'){
return DB\Database::instance()->getDB($database);
}
/**

View File

@@ -36,17 +36,14 @@ class CcpSystemsUpdate {
*/
private function prepareSystemLogTables(){
$f3 = \Base::instance();
// get information for all systems from CCP DB
$systemController = new Controller\Api\System();
$systemsData = $systemController->getSystems();
// switch DB back to pathfinder
DB\Database::instance()->setDB('PF');
$pfDB = DB\Database::instance()->getDB('PF');
// insert systems into each log table if not exist
$f3->get('DB')->begin();
$pfDB->begin();
foreach($this->logTables as $tableName){
// insert systems into jump log table
@@ -56,14 +53,14 @@ class CcpSystemsUpdate {
foreach($systemsData as $systemData){
// skip WH systems -> no jump data available
if($systemData['type']['name'] == 'k-space'){
$f3->get('DB')->exec($sqlInsertSystem, array(
$pfDB->exec($sqlInsertSystem, array(
':systemId' => $systemData['systemId']
), 0, false);
}
}
}
$f3->get('DB')->commit();
$pfDB->commit();
return $systemsData;
}
@@ -82,6 +79,7 @@ class CcpSystemsUpdate {
$time_end = microtime(true);
$execTimePrepareSystemLogTables = $time_end - $time_start;
$pfDB = DB\Database::instance()->getDB('PF');
// get current jump Data -------------------------------------------------------
$time_start = microtime(true);
@@ -141,7 +139,7 @@ class CcpSystemsUpdate {
// update system log tables -----------------------------------------------------
$time_start = microtime(true);
// make sure last update is (at least) 1h ago
$f3->get('DB')->begin();
$pfDB->begin();
foreach($this->logTables as $key => $tableName){
$sql = "UPDATE
@@ -187,7 +185,7 @@ class CcpSystemsUpdate {
$currentJumps = $jumpData[$systemData['systemId']];
}
$f3->get('DB')->exec($sql, array(
$pfDB->exec($sql, array(
':systemId' => $systemData['systemId'],
':value' => $currentJumps
), 0, false);
@@ -201,14 +199,14 @@ class CcpSystemsUpdate {
$currentKills = $currentKillData[$key];
}
$f3->get('DB')->exec($sql, array(
$pfDB->exec($sql, array(
':systemId' => $systemData['systemId'],
':value' => $currentKills
), 0, false);
}
}
}
$f3->get('DB')->commit();
$pfDB->commit();
$time_end = microtime(true);
$execTimeUpdateTables = $time_end - $time_start;

View File

@@ -21,8 +21,6 @@ class CharacterUpdate {
*/
function deleteLogData($f3){
DB\Database::instance()->setDB('PF');
$characterLogModel = Model\BasicModel::getNew('CharacterLogModel');
// find "old" character logs

View File

@@ -24,7 +24,7 @@ class MapUpdate {
*/
function deactivateMapData($f3){
DB\Database::instance()->setDB('PF');
$pfDB = DB\Database::instance()->getDB('PF');
$sqlDeactivateExpiredMaps = "UPDATE map SET
active = 0
@@ -35,8 +35,8 @@ class MapUpdate {
$privateMapLifetime = (int)$f3->get('PATHFINDER.MAP.PRIVATE.LIFETIME');
$f3->get('DB')->exec($sqlDeactivateExpiredMaps, ['lifetime' => $privateMapLifetime]);
$deactivatedMapsCount = $f3->get('DB')->count();
$pfDB->exec($sqlDeactivateExpiredMaps, ['lifetime' => $privateMapLifetime]);
$deactivatedMapsCount = $pfDB->count();
// Log ------------------------
$log = Controller\LogController::getLogger('cron_' . __FUNCTION__);
@@ -50,7 +50,7 @@ class MapUpdate {
*/
function deleteMapData($f3){
DB\Database::instance()->setDB('PF');
$pfDB = DB\Database::instance()->getDB('PF');
$sqlDeleteDisabledMaps = "DELETE FROM
map
@@ -58,9 +58,9 @@ class MapUpdate {
map.active = 0 AND
TIMESTAMPDIFF(DAY, map.updated, NOW() ) > :deletion_time";
$f3->get('DB')->exec($sqlDeleteDisabledMaps, ['deletion_time' => self::DAYS_UNTIL_MAP_DELETION]);
$pfDB->exec($sqlDeleteDisabledMaps, ['deletion_time' => self::DAYS_UNTIL_MAP_DELETION]);
$deletedMapsCount = $f3->get('DB')->count();
$deletedMapsCount = $pfDB->count();
// Log ------------------------
$log = Controller\LogController::getLogger('cron_' . __FUNCTION__);

View File

@@ -16,17 +16,20 @@ class Database extends \Prefab {
function __construct($database = 'PF'){
// set database
$this->setDB($database);
$this->setDB($database);
}
/**
* set database
* @param string $database
* @return SQL
*/
public function setDB($database = 'PF'){
$f3 = \Base::instance();
// "Hive" Key for DB storage
$dbHiveKey = $this->getDbHiveKey($database);
if($database === 'CCP'){
// CCP DB
$dns = Controller\Controller::getEnvironmentData('DB_CCP_DNS');
@@ -34,33 +37,63 @@ class Database extends \Prefab {
$user = Controller\Controller::getEnvironmentData('DB_CCP_USER');
$password = Controller\Controller::getEnvironmentData('DB_CCP_PASS');
}else{
// Pathfinder DB
// Pathfinder(PF) DB
$dns = Controller\Controller::getEnvironmentData('DB_DNS');
$name = Controller\Controller::getEnvironmentData('DB_NAME');
$user = Controller\Controller::getEnvironmentData('DB_USER');
$password = Controller\Controller::getEnvironmentData('DB_PASS');
}
// check for DB switch. If current DB equal new DB -> no switch needed
// check if DB connection already exists
if(
!$f3->exists('DB') ||
$name !== $f3->get('DB')->name()
!$f3->exists( $dbHiveKey ) ||
$name !== $f3->get( $dbHiveKey )->name()
){
$db = $this->connect($dns, $name, $user, $password);
$f3->set('DB', $db);
// set DB timezone to UTC +00:00 (eve server time)
$f3->get('DB')->exec('SET @@session.time_zone = "+00:00";');
$db->exec('SET @@session.time_zone = "+00:00";');
// disable innoDB schema (relevant vor MySql 5.5)
// not necessary for MySql > v.5.6
//$f3->get('DB')->exec('SET GLOBAL innodb_stats_on_metadata = OFF;');
}
// $db->exec('SET GLOBAL innodb_stats_on_metadata = OFF;');
// store DB object
$f3->set($dbHiveKey, $db);
return $db;
}else{
return $f3->get( $dbHiveKey );
}
}
/**
* get database
* @param string $database
* @return mixed|void
*/
public function getDB($database = 'PF'){
$f3 = \Base::instance();
$dbHiveKey = $this->getDbHiveKey($database);
if( $f3->exists( $dbHiveKey ) ){
return $f3->get( $dbHiveKey );
}else{
return $this->setDB($database);
}
}
/**
* get a unique hive key for each DB connection
* @param $database
* @return string
*/
protected function getDbHiveKey($database){
return 'DB_' . $database;
}
/**
* connect to a database
* @param $dns

View File

@@ -11,10 +11,15 @@ namespace Model;
use DB\SQL\Schema;
use Exception;
use Controller;
use DB;
class BasicModel extends \DB\Cortex {
protected $db = 'DB';
/**
* Hive key with DB object
* @var string
*/
protected $db = 'DB_PF';
/**
* caching time of field schema - seconds
@@ -231,8 +236,9 @@ class BasicModel extends \DB\Cortex {
*/
protected function setUpdated(){
if($this->_id > 0){
$f3 = self::getF3();
$f3->get('DB')->exec(
$pfDB = DB\Database::instance()->getDB('PF');
$pfDB->exec(
["UPDATE " . $this->table . " SET updated=NOW() WHERE id=:id"],
[
[':id' => $this->_id]
@@ -386,7 +392,6 @@ class BasicModel extends \DB\Cortex {
}
}
}
/**
@@ -401,7 +406,7 @@ class BasicModel extends \DB\Cortex {
$model = '\\' . __NAMESPACE__ . '\\' . $model;
if(class_exists($model)){
$class = new $model( self::getF3()->get('DB'), null, null, $ttl );
$class = new $model( null, null, null, $ttl );
}else{
throw new \Exception('No model class found');
}