Add caching to appconfig

This commit is contained in:
Robin Appelman 2014-02-07 14:03:39 +01:00
parent b537d90e58
commit cd3ef0bb9d
2 changed files with 48 additions and 16 deletions

View File

@ -47,6 +47,10 @@ class AppConfig implements \OCP\IAppConfig {
*/ */
protected $conn; protected $conn;
private $cache = array();
private $appsLoaded = array();
/** /**
* @param \OC\DB\Connection $conn * @param \OC\DB\Connection $conn
*/ */
@ -54,6 +58,32 @@ class AppConfig implements \OCP\IAppConfig {
$this->conn = $conn; $this->conn = $conn;
} }
/**
* @param string $app
* @return string[]
*/
private function getAppCache($app) {
if (!isset($this->cache[$app])) {
$this->cache[$app] = array();
}
return $this->cache[$app];
}
private function getAppValues($app) {
$appCache = $this->getAppCache($app);
if (array_search($app, $this->appsLoaded) === false) {
$query = 'SELECT `configvalue`, `configkey` FROM `*PREFIX*appconfig`'
. ' WHERE `appid` = ?';
$result = $this->conn->executeQuery($query, array($app));
while ($row = $result->fetch()) {
$appCache[$row['configkey']] = $row['configvalue'];
}
$this->appsLoaded[] = $app;
}
$this->cache[$app] = $appCache;
return $appCache;
}
/** /**
* @brief Get all apps using the config * @brief Get all apps using the config
* @return array with app ids * @return array with app ids
@ -81,15 +111,8 @@ class AppConfig implements \OCP\IAppConfig {
* not returned. * not returned.
*/ */
public function getKeys($app) { public function getKeys($app) {
$query = 'SELECT `configkey` FROM `*PREFIX*appconfig` WHERE `appid` = ?'; $values = $this->getAppValues($app);
$result = $this->conn->executeQuery($query, array($app)); return array_keys($values);
$keys = array();
while ($key = $result->fetchColumn()) {
$keys[] = $key;
}
return $keys;
} }
/** /**
@ -103,11 +126,9 @@ class AppConfig implements \OCP\IAppConfig {
* not exist the default value will be returned * not exist the default value will be returned
*/ */
public function getValue($app, $key, $default = null) { public function getValue($app, $key, $default = null) {
$query = 'SELECT `configvalue` FROM `*PREFIX*appconfig`' $values = $this->getAppValues($app);
. ' WHERE `appid` = ? AND `configkey` = ?'; if (isset($values[$key])) {
$row = $this->conn->fetchAssoc($query, array($app, $key)); return $values[$key];
if ($row) {
return $row['configvalue'];
} else { } else {
return $default; return $default;
} }
@ -120,8 +141,8 @@ class AppConfig implements \OCP\IAppConfig {
* @return bool * @return bool
*/ */
public function hasKey($app, $key) { public function hasKey($app, $key) {
$exists = $this->getKeys($app); $values = $this->getAppValues($app);
return in_array($key, $exists); return isset($values[$key]);
} }
/** /**
@ -151,6 +172,10 @@ class AppConfig implements \OCP\IAppConfig {
); );
$this->conn->update('*PREFIX*appconfig', $data, $where); $this->conn->update('*PREFIX*appconfig', $data, $where);
} }
if (!isset($this->cache[$app])) {
$this->cache[$app] = array();
}
$this->cache[$app][$key] = $value;
} }
/** /**
@ -167,6 +192,9 @@ class AppConfig implements \OCP\IAppConfig {
'configkey' => $key, 'configkey' => $key,
); );
$this->conn->delete('*PREFIX*appconfig', $where); $this->conn->delete('*PREFIX*appconfig', $where);
if (isset($this->cache[$app]) and isset($this->cache[$app][$key])) {
unset($this->cache[$app][$key]);
}
} }
/** /**
@ -181,6 +209,7 @@ class AppConfig implements \OCP\IAppConfig {
'appid' => $app, 'appid' => $app,
); );
$this->conn->delete('*PREFIX*appconfig', $where); $this->conn->delete('*PREFIX*appconfig', $where);
unset($this->cache[$app]);
} }
/** /**

View File

@ -31,6 +31,9 @@ class OC_DB_StatementWrapper {
/** /**
* make execute return the result instead of a bool * make execute return the result instead of a bool
*
* @param array $input
* @return \OC_DB_StatementWrapper | int
*/ */
public function execute($input=array()) { public function execute($input=array()) {
if(OC_Config::getValue( "log_query", false)) { if(OC_Config::getValue( "log_query", false)) {