Skip to content

Commit

Permalink
Cache non existing DB user
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
rullzer committed Oct 10, 2016
1 parent 3f40bb6 commit 1273d82
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/private/User/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ public function createUser($uid, $password) {
$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;
}

Expand Down Expand Up @@ -234,7 +237,7 @@ public function checkPassword($uid, $password) {
* @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));

Expand All @@ -243,6 +246,8 @@ private function loadUser($uid) {
return false;
}

$this->cache[$uid] = false;

while ($row = $result->fetchRow()) {
$this->cache[$uid]['uid'] = $row['uid'];
$this->cache[$uid]['displayname'] = $row['displayname'];
Expand Down Expand Up @@ -284,7 +289,7 @@ public function getUsers($search = '', $limit = null, $offset = null) {
*/
public function userExists($uid) {
$this->loadUser($uid);
return !empty($this->cache[$uid]);
return $this->cache[$uid] !== false;
}

/**
Expand Down

0 comments on commit 1273d82

Please sign in to comment.