Skip to content

Commit 2fb6113

Browse files
committed
Allow to return the cached user object without recreating database.
This increases performance.
1 parent 2a063ec commit 2fb6113

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

lib/User.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -919,25 +919,34 @@ public static function getLoggedInUser(&$database = null, &$log = null)
919919
{
920920
$loggedin_ID = self::getLoggedInID();
921921

922+
if(static::$loggedin_user instanceof User) //If we already have an cached user, then return this for perfomance
923+
{
924+
if (static::$loggedin_user->getID() != $loggedin_ID) {
925+
$var = null;
926+
if (is_null($database) || is_null($log)) {
927+
$database = new Database();
928+
$log = new Log($database);
929+
}
930+
static::$loggedin_user = new User($database, $var, $log, $loggedin_ID);
931+
}
932+
return static::$loggedin_user;
933+
}
934+
922935
if (is_null($database) || is_null($log)) {
923936
$database = new Database();
924937
$log = new Log($database);
925938
}
939+
926940
if (!is_object(static::$loggedin_user)) {
927941
if ($database->doesTableExist('users')) {
928942
$var = null;
929943
static::$loggedin_user = new User($database, $var, $log, $loggedin_ID);
944+
930945
} else {
931946
$var = null;
932947
//When no user table exists, create a fake user, with all needed permission
933948
return new User($database, $var, $log, 0, array("perms_system_database" => 21845));
934949
}
935-
} else { //A user is cached...
936-
//Check if the the cached user, is the one we want!
937-
if (static::$loggedin_user->getID() != $loggedin_ID) {
938-
$var = null;
939-
static::$loggedin_user = new User($database, $var, $log, $loggedin_ID);
940-
}
941950
}
942951

943952
return static::$loggedin_user;

0 commit comments

Comments
 (0)