add cache for single users

This commit is contained in:
adrien 2014-03-06 17:57:09 +01:00
parent 256dca935b
commit 08a46e3080
1 changed files with 45 additions and 33 deletions

View File

@ -42,7 +42,9 @@ class OC_User_Database extends OC_User_Backend {
/** /**
* @var PasswordHash * @var PasswordHash
*/ */
static private $hasher = null; private static $hasher = null;
protected static $cache = array();
private function getHasher() { private function getHasher() {
if (!self::$hasher) { if (!self::$hasher) {
@ -135,11 +137,9 @@ class OC_User_Database extends OC_User_Backend {
* @return string display name * @return string display name
*/ */
public function getDisplayName($uid) { public function getDisplayName($uid) {
$query = OC_DB::prepare('SELECT `displayname` FROM `*PREFIX*users` WHERE `uid` = ?'); $this->loadUser($uid);
$result = $query->execute(array($uid))->fetchAll(); if (!empty(self::$cache['uid']['displayname'])) {
$displayName = trim($result[0]['displayname'], ' '); return self::$cache['uid']['displayname'];
if (!empty($displayName)) {
return $displayName;
} else { } else {
return $uid; return $uid;
} }
@ -183,23 +183,41 @@ class OC_User_Database extends OC_User_Backend {
$storedHash = $row['password']; $storedHash = $row['password'];
if ($storedHash[0] == '$') { //the new phpass based hashing if ($storedHash[0] == '$') { //the new phpass based hashing
$hasher = $this->getHasher(); $hasher = $this->getHasher();
if ($hasher->CheckPassword($password . OC_Config::getValue('passwordsalt', ''), $storedHash)) { if ($hasher->CheckPassword($password . OC_Config::getValue('passwordsalt', ''), $storedHash))
return $row['uid']; return $row['uid'];
} else {
return false; //old sha1 based hashing
} } elseif (sha1($password) == $storedHash) {
} else { //old sha1 based hashing //upgrade to new hashing
if (sha1($password) == $storedHash) { $this->setPassword($row['uid'], $password);
//upgrade to new hashing return $row['uid'];
$this->setPassword($row['uid'], $password);
return $row['uid'];
} else {
return false;
}
} }
} else {
return false;
} }
return false;
}
/**
* @brief Load an user in the cache
* @returns boolean
*/
protected function loadUser($uid) {
if (empty(self::$cache[$uid])) {
$query = OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)');
$result = $query->execute(array($uid));
if (OC_DB::isError($result)) {
OC_Log::write('core', OC_DB::getErrorMessage($result), OC_Log::ERROR);
return false;
}
while ($row = $result->fetchRow()) {
self::$cache[$uid]['uid'] = $row['uid'];
self::$cache[$uid]['displayname'] = $row['displayname'];
}
}
return true;
} }
/** /**
@ -224,26 +242,20 @@ class OC_User_Database extends OC_User_Backend {
* @return boolean * @return boolean
*/ */
public function userExists($uid) { public function userExists($uid) {
$query = OC_DB::prepare('SELECT COUNT(*) FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)'); $this->loadUser($uid);
$result = $query->execute(array($uid)); return empty(self::$cache[$uid]) ? false : true;
if (OC_DB::isError($result)) {
OC_Log::write('core', OC_DB::getErrorMessage($result), OC_Log::ERROR);
return false;
}
return $result->fetchOne() > 0;
} }
/** /**
* @brief get the user's home directory * @brief get the user's home directory
* @param string $uid the username * @param string $uid the username
* @return string|false * @return boolean
*/ */
public function getHome($uid) { public function getHome($uid) {
if ($this->userExists($uid)) { if ($this->userExists($uid))
return OC_Config::getValue("datadirectory", OC::$SERVERROOT . "/data") . '/' . $uid; return OC_Config::getValue("datadirectory", OC::$SERVERROOT . "/data") . '/' . $uid;
} else {
return false; return false;
}
} }
/** /**
@ -256,7 +268,7 @@ class OC_User_Database extends OC_User_Backend {
/** /**
* counts the users in the database * counts the users in the database
* *
* @return false|string | bool * @return int | bool
*/ */
public function countUsers() { public function countUsers() {
$query = OC_DB::prepare('SELECT COUNT(*) FROM `*PREFIX*users`'); $query = OC_DB::prepare('SELECT COUNT(*) FROM `*PREFIX*users`');