1
0
Fork 0

Use APCu (if available) to cache username lookups

master release/2.0.0
Anonymous Contributor 2023-09-10 02:41:36 +02:00
parent c416ec0992
commit b18b960fb0
1 changed files with 14 additions and 8 deletions

View File

@ -10,11 +10,12 @@ class OpenSim
{ {
private PDO $pdo; private PDO $pdo;
private $cache = array(); private bool $apcu;
public function __construct(PDO $pdo) public function __construct(PDO $pdo)
{ {
$this->pdo = $pdo; $this->pdo = $pdo;
$this->apcu = function_exists('apcu_fetch') && apcu_enabled();
} }
private function getUserNameFromGridData($userID, $table, $row): ?string private function getUserNameFromGridData($userID, $table, $row): ?string
@ -29,9 +30,10 @@ class OpenSim
$dbUserID = $userData[0]; $dbUserID = $userData[0];
$dbUserName = $userData[2]; $dbUserName = $userData[2];
$this->cache['USERNAME'][$userID] = $dbUserName;
if ($dbUserID == $userID) { if ($dbUserID == $userID) {
if ($this->apcu) {
apcu_store('os_username_'.$userID, $dbUserName, 600);
}
return $dbUserName; return $dbUserName;
} }
} }
@ -46,16 +48,20 @@ class OpenSim
return "Unknown User"; return "Unknown User";
} }
if (isset($this->cache['USERNAME'][$userID])) { if ($this->apcu && apcu_exists('os_username_'.$userID)) {
return $this->cache['USERNAME'][$userID]; return apcu_fetch('os_username_'.$userID);
} }
$statementUser = $this->pdo->prepare('SELECT FirstName,LastName FROM UserAccounts WHERE PrincipalID = ?'); $statementUser = $this->pdo->prepare('SELECT FirstName,LastName FROM UserAccounts WHERE PrincipalID = ?');
$statementUser->execute(array($userID)); $statementUser->execute(array($userID));
if ($rowUser = $statementUser->fetch()) { if ($rowUser = $statementUser->fetch()) {
$this->cache['USERNAME'][$userID] = $rowUser['FirstName'].' '.$rowUser['LastName']; $name = $rowUser['FirstName'].' '.$rowUser['LastName'];
return $this->cache['USERNAME'][$userID]; if ($this->apcu) {
apcu_store('os_username_'.$userID, $name, 300);
}
return $name;
} }
$res = $this->getUserNameFromGridData($userID, 'GridUser', 'UserID'); $res = $this->getUserNameFromGridData($userID, 'GridUser', 'UserID');