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;
private $cache = array();
private $appsLoaded = array();
/**
* @param \OC\DB\Connection $conn
*/
@ -54,6 +58,32 @@ class AppConfig implements \OCP\IAppConfig {
$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
* @return array with app ids
@ -81,15 +111,8 @@ class AppConfig implements \OCP\IAppConfig {
* not returned.
*/
public function getKeys($app) {
$query = 'SELECT `configkey` FROM `*PREFIX*appconfig` WHERE `appid` = ?';
$result = $this->conn->executeQuery($query, array($app));
$keys = array();
while ($key = $result->fetchColumn()) {
$keys[] = $key;
}
return $keys;
$values = $this->getAppValues($app);
return array_keys($values);
}
/**
@ -103,11 +126,9 @@ class AppConfig implements \OCP\IAppConfig {
* not exist the default value will be returned
*/
public function getValue($app, $key, $default = null) {
$query = 'SELECT `configvalue` FROM `*PREFIX*appconfig`'
. ' WHERE `appid` = ? AND `configkey` = ?';
$row = $this->conn->fetchAssoc($query, array($app, $key));
if ($row) {
return $row['configvalue'];
$values = $this->getAppValues($app);
if (isset($values[$key])) {
return $values[$key];
} else {
return $default;
}
@ -120,8 +141,8 @@ class AppConfig implements \OCP\IAppConfig {
* @return bool
*/
public function hasKey($app, $key) {
$exists = $this->getKeys($app);
return in_array($key, $exists);
$values = $this->getAppValues($app);
return isset($values[$key]);
}
/**
@ -151,6 +172,10 @@ class AppConfig implements \OCP\IAppConfig {
);
$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,
);
$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,
);
$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
*
* @param array $input
* @return \OC_DB_StatementWrapper | int
*/
public function execute($input=array()) {
if(OC_Config::getValue( "log_query", false)) {