From 91180bfef487ae5e9fcf7890b9b28746c02839cd Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sun, 1 Jun 2014 14:04:17 +0200 Subject: [PATCH 1/4] Add caching to AppConfig->getApps --- lib/private/appconfig.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/private/appconfig.php b/lib/private/appconfig.php index e2a961b1d6d..d4ad20816eb 100644 --- a/lib/private/appconfig.php +++ b/lib/private/appconfig.php @@ -51,6 +51,11 @@ class AppConfig implements \OCP\IAppConfig { private $appsLoaded = array(); + /** + * @var string[] + */ + private $apps = null; + /** * @param \OC\DB\Connection $conn */ @@ -90,12 +95,16 @@ class AppConfig implements \OCP\IAppConfig { /** * Get all apps using the config + * * @return array an array of app ids * * This function returns a list of all apps that have at least one * entry in the appconfig table. */ public function getApps() { + if (is_array($this->apps)) { + return $this->apps; + } $query = 'SELECT DISTINCT `appid` FROM `*PREFIX*appconfig` ORDER BY `appid`'; $result = $this->conn->executeQuery($query); @@ -103,11 +112,13 @@ class AppConfig implements \OCP\IAppConfig { while ($appid = $result->fetchColumn()) { $apps[] = $appid; } + $this->apps = $apps; return $apps; } /** * Get the available keys for an app + * * @param string $app the app we are looking for * @return array an array of key names * @@ -123,6 +134,7 @@ class AppConfig implements \OCP\IAppConfig { /** * Gets the config value + * * @param string $app app * @param string $key key * @param string $default = null, default value if the key does not exist @@ -142,6 +154,7 @@ class AppConfig implements \OCP\IAppConfig { /** * check if a key is set in the appconfig + * * @param string $app * @param string $key * @return bool @@ -153,6 +166,7 @@ class AppConfig implements \OCP\IAppConfig { /** * sets a value in the appconfig + * * @param string $app app * @param string $key key * @param string $value value @@ -181,11 +195,15 @@ class AppConfig implements \OCP\IAppConfig { if (!isset($this->cache[$app])) { $this->cache[$app] = array(); } + if (is_array($this->apps) and array_search($app, $this->apps) === false) { + $this->apps[] = $app; + } $this->cache[$app][$key] = $value; } /** * Deletes a key + * * @param string $app app * @param string $key key * @return boolean|null @@ -203,6 +221,7 @@ class AppConfig implements \OCP\IAppConfig { /** * Remove app from appconfig + * * @param string $app app * @return boolean|null * @@ -214,6 +233,10 @@ class AppConfig implements \OCP\IAppConfig { ); $this->conn->delete('*PREFIX*appconfig', $where); unset($this->cache[$app]); + if (is_array($this->apps) and $i = array_search($app, $this->apps) !== false) { + unset($this->apps[$i]); + } + $this->apps = null; } /** From a4949f4b31e86650ac2ccfe547fa64494b08dbff Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sun, 1 Jun 2014 14:14:30 +0200 Subject: [PATCH 2/4] Simplify AppConfig->getValues() --- lib/private/appconfig.php | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/lib/private/appconfig.php b/lib/private/appconfig.php index d4ad20816eb..196d04aa9a5 100644 --- a/lib/private/appconfig.php +++ b/lib/private/appconfig.php @@ -251,28 +251,18 @@ class AppConfig implements \OCP\IAppConfig { return false; } - $fields = '`configvalue`'; - $where = 'WHERE'; - $params = array(); if ($app !== false) { - $fields .= ', `configkey`'; - $where .= ' `appid` = ?'; - $params[] = $app; - $key = 'configkey'; + return $this->getAppValues($app); } else { - $fields .= ', `appid`'; - $where .= ' `configkey` = ?'; - $params[] = $key; - $key = 'appid'; - } - $query = 'SELECT ' . $fields . ' FROM `*PREFIX*appconfig` ' . $where; - $result = $this->conn->executeQuery($query, $params); + $query = 'SELECT `configvalue`, `appid` FROM `*PREFIX*appconfig` WHERE `configkey` = ?'; + $result = $this->conn->executeQuery($query, array($key)); - $values = array(); - while ($row = $result->fetch((\PDO::FETCH_ASSOC))) { - $values[$row[$key]] = $row['configvalue']; - } + $values = array(); + while ($row = $result->fetch((\PDO::FETCH_ASSOC))) { + $values[$row['appid']] = $row['configvalue']; + } - return $values; + return $values; + } } } From bff9e11ff996a623a61fe2c86a37d3a2d83e61bb Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 3 Jun 2014 12:38:15 +0200 Subject: [PATCH 3/4] Remove duplicate cache cleanup --- lib/private/appconfig.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/private/appconfig.php b/lib/private/appconfig.php index 196d04aa9a5..cfb2addc4c3 100644 --- a/lib/private/appconfig.php +++ b/lib/private/appconfig.php @@ -236,7 +236,6 @@ class AppConfig implements \OCP\IAppConfig { if (is_array($this->apps) and $i = array_search($app, $this->apps) !== false) { unset($this->apps[$i]); } - $this->apps = null; } /** From 6ed319486387e6b7a80c64409527c7d454d3509d Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 3 Jun 2014 12:40:23 +0200 Subject: [PATCH 4/4] Use a map to prevent having to use search --- lib/private/appconfig.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/private/appconfig.php b/lib/private/appconfig.php index cfb2addc4c3..f20c4a08426 100644 --- a/lib/private/appconfig.php +++ b/lib/private/appconfig.php @@ -196,7 +196,7 @@ class AppConfig implements \OCP\IAppConfig { $this->cache[$app] = array(); } if (is_array($this->apps) and array_search($app, $this->apps) === false) { - $this->apps[] = $app; + $this->apps[$app] = $app; } $this->cache[$app][$key] = $value; } @@ -233,9 +233,7 @@ class AppConfig implements \OCP\IAppConfig { ); $this->conn->delete('*PREFIX*appconfig', $where); unset($this->cache[$app]); - if (is_array($this->apps) and $i = array_search($app, $this->apps) !== false) { - unset($this->apps[$i]); - } + unset($this->apps[$app]); } /**