diff --git a/app/main/controller/api/map.php b/app/main/controller/api/map.php index 21aaee76..7e178dde 100644 --- a/app/main/controller/api/map.php +++ b/app/main/controller/api/map.php @@ -20,6 +20,7 @@ use Model; class Map extends Controller\AccessController { // cache keys + const CACHE_KEY_INIT = 'CACHED_INIT'; const CACHE_KEY_MAP_DATA = 'CACHED.MAP_DATA.%s'; const CACHE_KEY_USER_DATA = 'CACHED.USER_DATA.%s_%s'; @@ -69,124 +70,127 @@ class Map extends Controller\AccessController { * @param \Base $f3 */ public function init(\Base $f3){ - // expire time in seconds - $expireTimeHead = 60 * 60 * 12; + $expireTimeCache = 60 * 60; $expireTimeSQL = 60 * 60 * 12; - $f3->expire($expireTimeHead); + if( !$f3->exists(self::CACHE_KEY_INIT, $return )){ + $return = (object) []; + $return->error = []; - $return = (object) []; - $return->error = []; + // static program data ---------------------------------------------------------------------------------------- + $return->timer = $f3->get('PATHFINDER.TIMER'); - // static program data ---------------------------------------------------------------------------------------- - $return->timer = $f3->get('PATHFINDER.TIMER'); + // get all available map types -------------------------------------------------------------------------------- + $mapType = Model\BasicModel::getNew('MapTypeModel'); + $rows = $mapType->find('active = 1', null, $expireTimeSQL); - // get all available map types -------------------------------------------------------------------------------- - $mapType = Model\BasicModel::getNew('MapTypeModel'); - $rows = $mapType->find('active = 1', null, $expireTimeSQL); + // default map type config + $mapsDefaultConfig = Config::getMapsDefaultConfig(); - // default map type config - $mapsDefaultConfig = Config::getMapsDefaultConfig(); + $mapTypeData = []; + foreach((array)$rows as $rowData){ + $data = [ + 'id' => $rowData->id, + 'label' => $rowData->label, + 'class' => $rowData->class, + 'classTab' => $rowData->classTab, + 'defaultConfig' => $mapsDefaultConfig[$rowData->name] + ]; + $mapTypeData[$rowData->name] = $data; - $mapTypeData = []; - foreach((array)$rows as $rowData){ - $data = [ - 'id' => $rowData->id, - 'label' => $rowData->label, - 'class' => $rowData->class, - 'classTab' => $rowData->classTab, - 'defaultConfig' => $mapsDefaultConfig[$rowData->name] + } + $return->mapTypes = $mapTypeData; + + // get all available map scopes ------------------------------------------------------------------------------- + $mapScope = Model\BasicModel::getNew('MapScopeModel'); + $rows = $mapScope->find('active = 1', null, $expireTimeSQL); + $mapScopeData = []; + foreach((array)$rows as $rowData){ + $data = [ + 'id' => $rowData->id, + 'label' => $rowData->label + ]; + $mapScopeData[$rowData->name] = $data; + } + $return->mapScopes = $mapScopeData; + + // get all available system status ---------------------------------------------------------------------------- + $systemStatus = Model\BasicModel::getNew('SystemStatusModel'); + $rows = $systemStatus->find('active = 1', null, $expireTimeSQL); + $systemScopeData = []; + foreach((array)$rows as $rowData){ + $data = [ + 'id' => $rowData->id, + 'label' => $rowData->label, + 'class' => $rowData->class + ]; + $systemScopeData[$rowData->name] = $data; + } + $return->systemStatus = $systemScopeData; + + // get all available system types ----------------------------------------------------------------------------- + $systemType = Model\BasicModel::getNew('SystemTypeModel'); + $rows = $systemType->find('active = 1', null, $expireTimeSQL); + $systemTypeData = []; + foreach((array)$rows as $rowData){ + $data = [ + 'id' => $rowData->id, + 'name' => $rowData->name + ]; + $systemTypeData[$rowData->name] = $data; + } + $return->systemType = $systemTypeData; + + // get available connection scopes ---------------------------------------------------------------------------- + $connectionScope = Model\BasicModel::getNew('ConnectionScopeModel'); + $rows = $connectionScope->find('active = 1', null, $expireTimeSQL); + $connectionScopeData = []; + foreach((array)$rows as $rowData){ + $data = [ + 'id' => $rowData->id, + 'label' => $rowData->label, + 'connectorDefinition' => $rowData->connectorDefinition + ]; + $connectionScopeData[$rowData->name] = $data; + } + $return->connectionScopes = $connectionScopeData; + + // get available character status ----------------------------------------------------------------------------- + $characterStatus = Model\BasicModel::getNew('CharacterStatusModel'); + $rows = $characterStatus->find('active = 1', null, $expireTimeSQL); + $characterStatusData = []; + foreach((array)$rows as $rowData){ + $data = [ + 'id' => $rowData->id, + 'name' => $rowData->name, + 'class' => $rowData->class + ]; + $characterStatusData[$rowData->name] = $data; + } + $return->characterStatus = $characterStatusData; + + // route search config ---------------------------------------------------------------------------------------- + $return->routeSearch = [ + 'defaultCount' => $this->getF3()->get('PATHFINDER.ROUTE.SEARCH_DEFAULT_COUNT'), + 'maxDefaultCount' => $this->getF3()->get('PATHFINDER.ROUTE.MAX_Default_COUNT'), + 'limit' => $this->getF3()->get('PATHFINDER.ROUTE.LIMIT'), ]; - $mapTypeData[$rowData->name] = $data; - } - $return->mapTypes = $mapTypeData; - - // get all available map scopes ------------------------------------------------------------------------------- - $mapScope = Model\BasicModel::getNew('MapScopeModel'); - $rows = $mapScope->find('active = 1', null, $expireTimeSQL); - $mapScopeData = []; - foreach((array)$rows as $rowData){ - $data = [ - 'id' => $rowData->id, - 'label' => $rowData->label + // get program routes ----------------------------------------------------------------------------------------- + $return->routes = [ + 'ssoLogin' => $this->getF3()->alias( 'sso', ['action' => 'requestAuthorization'] ) ]; - $mapScopeData[$rowData->name] = $data; - } - $return->mapScopes = $mapScopeData; - // get all available system status ---------------------------------------------------------------------------- - $systemStatus = Model\BasicModel::getNew('SystemStatusModel'); - $rows = $systemStatus->find('active = 1', null, $expireTimeSQL); - $systemScopeData = []; - foreach((array)$rows as $rowData){ - $data = [ - 'id' => $rowData->id, - 'label' => $rowData->label, - 'class' => $rowData->class + // get notification status ------------------------------------------------------------------------------------ + $return->notificationStatus = [ + 'rallySet' => (bool)Config::getNotificationMail('RALLY_SET') ]; - $systemScopeData[$rowData->name] = $data; + + $f3->set(self::CACHE_KEY_INIT, $return, $expireTimeCache ); } - $return->systemStatus = $systemScopeData; - // get all available system types ----------------------------------------------------------------------------- - $systemType = Model\BasicModel::getNew('SystemTypeModel'); - $rows = $systemType->find('active = 1', null, $expireTimeSQL); - $systemTypeData = []; - foreach((array)$rows as $rowData){ - $data = [ - 'id' => $rowData->id, - 'name' => $rowData->name - ]; - $systemTypeData[$rowData->name] = $data; - } - $return->systemType = $systemTypeData; - - // get available connection scopes ---------------------------------------------------------------------------- - $connectionScope = Model\BasicModel::getNew('ConnectionScopeModel'); - $rows = $connectionScope->find('active = 1', null, $expireTimeSQL); - $connectionScopeData = []; - foreach((array)$rows as $rowData){ - $data = [ - 'id' => $rowData->id, - 'label' => $rowData->label, - 'connectorDefinition' => $rowData->connectorDefinition - ]; - $connectionScopeData[$rowData->name] = $data; - } - $return->connectionScopes = $connectionScopeData; - - // get available character status ----------------------------------------------------------------------------- - $characterStatus = Model\BasicModel::getNew('CharacterStatusModel'); - $rows = $characterStatus->find('active = 1', null, $expireTimeSQL); - $characterStatusData = []; - foreach((array)$rows as $rowData){ - $data = [ - 'id' => $rowData->id, - 'name' => $rowData->name, - 'class' => $rowData->class - ]; - $characterStatusData[$rowData->name] = $data; - } - $return->characterStatus = $characterStatusData; - - // route search config ---------------------------------------------------------------------------------------- - $return->routeSearch = [ - 'defaultCount' => $this->getF3()->get('PATHFINDER.ROUTE.SEARCH_DEFAULT_COUNT'), - 'maxDefaultCount' => $this->getF3()->get('PATHFINDER.ROUTE.MAX_Default_COUNT'), - 'limit' => $this->getF3()->get('PATHFINDER.ROUTE.LIMIT'), - ]; - - // get program routes ----------------------------------------------------------------------------------------- - $return->routes = [ - 'ssoLogin' => $this->getF3()->alias( 'sso', ['action' => 'requestAuthorization'] ) - ]; - - // get notification status ------------------------------------------------------------------------------------ - $return->notificationStatus = [ - 'rallySet' => (bool)Config::getNotificationMail('RALLY_SET') - ]; + // Add data that should not be cached ========================================================================= // program mode (e.g. "maintenance") -------------------------------------------------------------------------- $return->programMode = [ @@ -645,17 +649,16 @@ class Map extends Controller\AccessController { $activeCharacter = $this->getCharacter(); - $return = (object) []; - $return->error = []; - - $cacheKey = $this->getMapDataCacheKey($activeCharacter); // if there is any system/connection change data submitted -> save new data if( !empty($mapData) || - !$f3->exists($cacheKey) + !$f3->exists($cacheKey, $return) ){ + $return = (object) []; + $return->error = []; + // get current map data =============================================================================== $maps = $activeCharacter->getMaps(); @@ -772,9 +775,6 @@ class Map extends Controller\AccessController { $responseTTL = (int)$f3->get('PATHFINDER.TIMER.UPDATE_SERVER_MAP.DELAY') / 1000; $f3->set($cacheKey, $return, $responseTTL); - }else{ - // get from cache - $return = $f3->get($cacheKey); } // if userData is requested -> add it as well @@ -807,7 +807,6 @@ class Map extends Controller\AccessController { */ public function updateUserData(\Base $f3){ $return = (object) []; - $return->error = []; $activeCharacter = $this->getCharacter(0); @@ -837,8 +836,12 @@ class Map extends Controller\AccessController { $map = $this->updateMapData($activeCharacter, $map); } + // get from cache + // this should happen if a user has multiple program instances running + // with the same main char $cacheKey = $this->getUserDataCacheKey($mapId, $requestSystemData->systemId); - if( !$f3->exists($cacheKey) ){ + if( !$f3->exists($cacheKey, $return) ){ + $return = (object) []; $return->mapUserData[] = $map->getUserData(); // request signature data for a system if user has map access! @@ -858,11 +861,6 @@ class Map extends Controller\AccessController { // cache response $f3->set($cacheKey, $return, $responseTTL); - }else{ - // get from cache - // this should happen if a user has multiple program instances running - // with the same main char - $return = $f3->get($cacheKey); } } } @@ -871,6 +869,9 @@ class Map extends Controller\AccessController { // even if they have multiple characters using the same map! $return->userData = $activeCharacter->getUser()->getData(); + // add error (if exists) + $return->error = []; + echo json_encode( $return ); } diff --git a/app/main/controller/api/route.php b/app/main/controller/api/route.php index b09ec224..6d9f5b46 100644 --- a/app/main/controller/api/route.php +++ b/app/main/controller/api/route.php @@ -82,16 +82,10 @@ class Route extends Controller\AccessController { $cacheKeyIdArray = $cacheKey . '.idArray'; if( - $f3->exists($cacheKeyNamedArray) && - $f3->exists($cacheKeyJumpArray) && - $f3->exists($cacheKeyIdArray) + !$f3->exists($cacheKeyNamedArray, $this->nameArray) || + !$f3->exists($cacheKeyJumpArray, $this->jumpArray) || + !$f3->exists($cacheKeyIdArray, $this->idArray) ){ - - // get cached values - $this->nameArray = $f3->get($cacheKeyNamedArray); - $this->jumpArray = $f3->get($cacheKeyJumpArray); - $this->idArray = $f3->get($cacheKeyIdArray); - }else{ // nothing cached $query = "SELECT * FROM system_neighbour"; $rows = $this->getDB()->exec($query, null, $this->staticJumpDataCacheTime); @@ -598,9 +592,9 @@ class Route extends Controller\AccessController { $filterData ); - if($f3->exists($cacheKey)){ + if($f3->exists($cacheKey, $cachedData)){ // get data from cache - $returnRoutData = $f3->get($cacheKey); + $returnRoutData = $cachedData; }else{ // max search depth for search $searchDepth = $f3->get('PATHFINDER.ROUTE.SEARCH_DEPTH'); diff --git a/app/main/controller/api/system.php b/app/main/controller/api/system.php index abf15e1a..bf9912d6 100644 --- a/app/main/controller/api/system.php +++ b/app/main/controller/api/system.php @@ -351,8 +351,8 @@ class System extends Controller\AccessController { } $cacheKey = 'CACHE_CONSTELLATION_SYSTEMS_' . self::formatHiveKey($constellationId); - if($f3->exists($cacheKey)){ - $return->systemData = $f3->get($cacheKey); + if($f3->exists($cacheKey, $cachedData)){ + $return->systemData = $cachedData; }else{ if($constellationId > 0){ $systemModels = $this->getSystemModelByIds([$constellationId], 'constellationID'); diff --git a/app/main/controller/controller.php b/app/main/controller/controller.php index 9fb13064..e0e0d152 100644 --- a/app/main/controller/controller.php +++ b/app/main/controller/controller.php @@ -477,28 +477,23 @@ class Controller { * @param \Base $f3 */ public function getEveServerStatus(\Base $f3){ - $return = (object) []; - $return->error = []; - // server status can be cached for some seconds $cacheKey = 'eve_server_status'; - if( !$f3->exists($cacheKey) ){ + if( !$f3->exists($cacheKey, $return) ){ + $return = (object) []; + $return->error = []; + $sso = new Sso(); $return->status = $sso->getCrestServerStatus(); if( !$return->status->crestOffline ){ $f3->set($cacheKey, $return, 60); } - }else{ - // get from cache - $return = $f3->get($cacheKey); } echo json_encode($return); } - - /** * get error object is a user is not found/logged of * @return \stdClass diff --git a/app/main/db/database.php b/app/main/db/database.php index 83d06f91..baf3433b 100644 --- a/app/main/db/database.php +++ b/app/main/db/database.php @@ -24,14 +24,13 @@ class Database extends \Prefab { * @return SQL */ public function setDB($database = 'PF'){ - $f3 = \Base::instance(); // "Hive" Key for DB storage $dbHiveKey = $this->getDbHiveKey($database); // check if DB connection already exists - if( !$f3->exists( $dbHiveKey ) ){ + if( !$f3->exists($dbHiveKey, $db) ){ if($database === 'CCP'){ // CCP DB $dns = Controller\Controller::getEnvironmentData('DB_CCP_DNS'); @@ -59,11 +58,9 @@ class Database extends \Prefab { // store DB object $f3->set($dbHiveKey, $db); } - - return $db; - }else{ - return $f3->get( $dbHiveKey ); } + + return $db; } /** @@ -72,15 +69,13 @@ class Database extends \Prefab { * @return SQL */ 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); + if( !$f3->exists($dbHiveKey, $db) ){ + $db = $this->setDB($database); } + + return $db; } /** diff --git a/app/main/lib/config.php b/app/main/lib/config.php index 1f32d284..32aafa2b 100644 --- a/app/main/lib/config.php +++ b/app/main/lib/config.php @@ -142,12 +142,8 @@ class Config extends \Prefab { * @return string|null */ static function getEnvironmentData($key){ - $f3 = \Base::instance(); $hiveKey = self::HIVE_KEY_ENVIRONMENT . '.' . $key; - $data = null; - if( $f3->exists($hiveKey) ){ - $data = $f3->get($hiveKey); - } + \Base::instance()->exists($hiveKey, $data); return $data; } @@ -161,8 +157,8 @@ class Config extends \Prefab { $f3 = \Base::instance(); $hiveKey = self::HIVE_KEY_PATHFINDER . '.NOTIFICATION.' . $key; $mail = false; - if( $f3->exists($hiveKey) ){ - $mail = $f3->get($hiveKey); + if( $f3->exists($hiveKey, $cachedMail) ){ + $mail = $cachedMail; } return $mail; diff --git a/app/main/model/basicmodel.php b/app/main/model/basicmodel.php index 89140aa7..be7277c7 100644 --- a/app/main/model/basicmodel.php +++ b/app/main/model/basicmodel.php @@ -367,12 +367,8 @@ abstract class BasicModel extends \DB\Cortex { // table cache exists // -> check cache for this row data $cacheKey = $this->getCacheKey($dataCacheKeyPrefix); - if( !is_null($cacheKey) ){ - $f3 = self::getF3(); - if( $f3->exists($cacheKey) ){ - $cacheData = $f3->get( $cacheKey ); - } + self::getF3()->exists($cacheKey, $cacheData); } return $cacheData;