Cache non existing DB user

We always query the database backend. Even if we use a different one
(ldap for example). Now we do this everytime we try to get a user object
so caching that a user is not in the DB safes some queries on each
request then (at least 2 what I found).

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2016-10-10 09:30:36 +02:00
parent 3f40bb69f8
commit 1273d82e8b
No known key found for this signature in database
GPG Key ID: 1E152838F164D13B
1 changed files with 7 additions and 2 deletions

View File

@ -94,6 +94,9 @@ class Database extends Backend implements IUserBackend {
$query = \OC_DB::prepare('INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )');
$result = $query->execute(array($uid, \OC::$server->getHasher()->hash($password)));
// Clear cache
unset($this->cache[$uid]);
return $result ? true : false;
}
@ -234,7 +237,7 @@ class Database extends Backend implements IUserBackend {
* @return boolean
*/
private function loadUser($uid) {
if (empty($this->cache[$uid])) {
if (!isset($this->cache[$uid])) {
$query = \OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)');
$result = $query->execute(array($uid));
@ -243,6 +246,8 @@ class Database extends Backend implements IUserBackend {
return false;
}
$this->cache[$uid] = false;
while ($row = $result->fetchRow()) {
$this->cache[$uid]['uid'] = $row['uid'];
$this->cache[$uid]['displayname'] = $row['displayname'];
@ -284,7 +289,7 @@ class Database extends Backend implements IUserBackend {
*/
public function userExists($uid) {
$this->loadUser($uid);
return !empty($this->cache[$uid]);
return $this->cache[$uid] !== false;
}
/**