Move *ConfigValue functions to App

This commit is contained in:
Hypolite Petovan 2018-03-06 20:04:04 -05:00
parent 047f949967
commit 87f2d18554
6 changed files with 150 additions and 172 deletions

View file

@ -944,4 +944,116 @@ class App
return true; return true;
} }
/**
* @param string $cat Config category
* @param string $k Config key
* @param mixed $default Default value if it isn't set
*/
public function getConfigValue($cat, $k, $default = null)
{
$return = $default;
if ($cat === 'config') {
if (isset($this->config[$k])) {
$return = $this->config[$k];
}
} else {
if (isset($this->config[$cat][$k])) {
$return = $this->config[$cat][$k];
}
}
return $return;
}
/**
* Sets a value in the config cache. Accepts raw output from the config table
*
* @param string $cat Config category
* @param string $k Config key
* @param mixed $v Value to set
*/
public function setConfigValue($cat, $k, $v)
{
// Only arrays are serialized in database, so we have to unserialize sparingly
$value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v;
if ($cat === 'config') {
$this->config[$k] = $value;
} else {
$this->config[$cat][$k] = $value;
}
}
/**
* Deletes a value from the config cache
*
* @param string $cat Config category
* @param string $k Config key
*/
public function deleteConfigValue($cat, $k)
{
if ($cat === 'config') {
if (isset($this->config[$k])) {
unset($this->config[$k]);
}
} else {
if (isset($this->config[$cat][$k])) {
unset($this->config[$cat][$k]);
}
}
}
/**
* Retrieves a value from the user config cache
*
* @param int $uid User Id
* @param string $cat Config category
* @param string $k Config key
* @param mixed $default Default value if key isn't set
*/
public function getPConfigValue($uid, $cat, $k, $default = null)
{
$return = $default;
if (isset($this->config[$uid][$cat][$k])) {
$return = $this->config[$uid][$cat][$k];
}
return $return;
}
/**
* Sets a value in the user config cache
*
* Accepts raw output from the pconfig table
*
* @param int $uid User Id
* @param string $cat Config category
* @param string $k Config key
* @param mixed $v Value to set
*/
public function setPConfigValue($uid, $cat, $k, $v)
{
// Only arrays are serialized in database, so we have to unserialize sparingly
$value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v;
$this->config[$uid][$cat][$k] = $value;
}
/**
* Deletes a value from the user config cache
*
* @param int $uid User Id
* @param string $cat Config category
* @param string $k Config key
*/
public function deletePConfigValue($uid, $cat, $k)
{
if (isset($this->config[$uid][$cat][$k])) {
unset($this->config[$uid][$cat][$k]);
}
}
} }

View file

@ -29,9 +29,7 @@ class Config extends BaseObject
public static function init() public static function init()
{ {
$a = self::getApp(); if (self::getApp()->getConfigValue('system', 'config_adapter') == 'preload') {
if (isset($a->config['system']['config_adapter']) && $a->config['system']['config_adapter'] == 'preload') {
self::$adapter = new Config\PreloadConfigAdapter(); self::$adapter = new Config\PreloadConfigAdapter();
} else { } else {
self::$adapter = new Config\JITConfigAdapter(); self::$adapter = new Config\JITConfigAdapter();

View file

@ -8,7 +8,7 @@ use Friendica\Database\DBM;
require_once 'include/dba.php'; require_once 'include/dba.php';
/** /**
* JustInTime ConfigAdapter * JustInTime Configuration Adapter
* *
* Default Config Adapter. Provides the best performance for pages loading few configuration variables. * Default Config Adapter. Provides the best performance for pages loading few configuration variables.
* *
@ -27,17 +27,15 @@ class JITConfigAdapter extends BaseObject implements IConfigAdapter
return; return;
} }
$a = self::getApp();
$configs = dba::select('config', ['v', 'k'], ['cat' => $cat]); $configs = dba::select('config', ['v', 'k'], ['cat' => $cat]);
while ($config = dba::fetch($configs)) { while ($config = dba::fetch($configs)) {
$k = $config['k']; $k = $config['k'];
if ($cat === 'config') {
$a->config[$k] = $config['v']; self::getApp()->setConfigValue($cat, $k, $config['v']);
} else {
$a->config[$cat][$k] = $config['v']; if ($cat !== 'config') {
self::$cache[$cat][$k] = $config['v']; $this->cache[$cat][$k] = $config['v'];
self::$in_db[$cat][$k] = true; $this->in_db[$cat][$k] = true;
} }
} }
dba::close($configs); dba::close($configs);
@ -96,11 +94,7 @@ class JITConfigAdapter extends BaseObject implements IConfigAdapter
return true; return true;
} }
if ($cat === 'config') { self::getApp()->setConfigValue($cat, $k, $value);
$a->config[$k] = $dbvalue;
} elseif ($cat != 'system') {
$a->config[$cat][$k] = $dbvalue;
}
// Assign the just added value to the cache // Assign the just added value to the cache
$this->cache[$cat][$k] = $dbvalue; $this->cache[$cat][$k] = $dbvalue;

View file

@ -8,7 +8,7 @@ use Friendica\Database\DBM;
require_once 'include/dba.php'; require_once 'include/dba.php';
/** /**
* JustInTime PConfigAdapter * JustInTime User Configuration Adapter
* *
* Default PConfig Adapter. Provides the best performance for pages loading few configuration variables. * Default PConfig Adapter. Provides the best performance for pages loading few configuration variables.
* *
@ -26,7 +26,9 @@ class JITPConfigAdapter extends BaseObject implements IPConfigAdapter
if (DBM::is_result($pconfigs)) { if (DBM::is_result($pconfigs)) {
while ($pconfig = dba::fetch($pconfigs)) { while ($pconfig = dba::fetch($pconfigs)) {
$k = $pconfig['k']; $k = $pconfig['k'];
$a->config[$uid][$cat][$k] = $pconfig['v'];
self::getApp()->setPConfigValue($uid, $cat, $k, $pconfig['v']);
$this->in_db[$uid][$cat][$k] = true; $this->in_db[$uid][$cat][$k] = true;
} }
} else if ($cat != 'config') { } else if ($cat != 'config') {
@ -59,12 +61,15 @@ class JITPConfigAdapter extends BaseObject implements IPConfigAdapter
$pconfig = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]); $pconfig = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
if (DBM::is_result($pconfig)) { if (DBM::is_result($pconfig)) {
$val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']); $val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
$a->config[$uid][$cat][$k] = $val;
self::getApp()->setPConfigValue($uid, $cat, $k, $val);
$this->in_db[$uid][$cat][$k] = true; $this->in_db[$uid][$cat][$k] = true;
return $val; return $val;
} else { } else {
$a->config[$uid][$cat][$k] = '!<unset>!'; self::getApp()->setPConfigValue($uid, $cat, $k, '!<unset>!');
$this->in_db[$uid][$cat][$k] = false; $this->in_db[$uid][$cat][$k] = false;
return $default_value; return $default_value;
@ -73,8 +78,6 @@ class JITPConfigAdapter extends BaseObject implements IPConfigAdapter
public function set($uid, $cat, $k, $value) public function set($uid, $cat, $k, $value)
{ {
$a = self::getApp();
// We store our setting values in a string variable. // We store our setting values in a string variable.
// So we have to do the conversion here so that the compare below works. // So we have to do the conversion here so that the compare below works.
// The exception are array values. // The exception are array values.
@ -86,7 +89,7 @@ class JITPConfigAdapter extends BaseObject implements IPConfigAdapter
return true; return true;
} }
$a->config[$uid][$cat][$k] = $dbvalue; self::getApp()->setPConfigValue($uid, $cat, $k, $value);
// manage array value // manage array value
$dbvalue = (is_array($value) ? serialize($value) : $dbvalue); $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
@ -103,10 +106,9 @@ class JITPConfigAdapter extends BaseObject implements IPConfigAdapter
public function delete($uid, $cat, $k) public function delete($uid, $cat, $k)
{ {
$a = self::getApp(); self::getApp()->deletePConfigValue($uid, $cat, $k);
if (!empty($a->config[$uid][$cat][$k])) { if (!empty($this->in_db[$uid][$cat][$k])) {
unset($a->config[$uid][$cat][$k]);
unset($this->in_db[$uid][$cat][$k]); unset($this->in_db[$uid][$cat][$k]);
} }

View file

@ -11,9 +11,9 @@ use Friendica\Database\DBM;
require_once 'include/dba.php'; require_once 'include/dba.php';
/** /**
* Preload ConfigAdapter * Preload Configuration Adapter
* *
* Minimize the number of database queries to retrieve configuration values at the cost of memory. * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
* *
* @author Hypolite Petovan <mrpetovan@gmail.com> * @author Hypolite Petovan <mrpetovan@gmail.com>
*/ */
@ -34,7 +34,7 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter
$configs = dba::select('config', ['cat', 'v', 'k']); $configs = dba::select('config', ['cat', 'v', 'k']);
while ($config = dba::fetch($configs)) { while ($config = dba::fetch($configs)) {
$this->setPreloadedValue($config['cat'], $config['k'], $config['v']); self::getApp()->setConfigValue($config['cat'], $config['k'], $config['v']);
} }
dba::close($configs); dba::close($configs);
@ -46,11 +46,11 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter
if ($refresh) { if ($refresh) {
$config = dba::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]); $config = dba::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
if (DBM::is_result($config)) { if (DBM::is_result($config)) {
$this->setPreloadedValue($cat, $k, $config['v']); self::getApp()->setConfigValue($cat, $k, $config['v']);
} }
} }
$return = $this->getPreloadedValue($cat, $k, $default_value); $return = self::getApp()->getConfigValue($cat, $k, $default_value);
return $return; return $return;
} }
@ -62,11 +62,11 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter
// The exception are array values. // The exception are array values.
$compare_value = !is_array($value) ? (string)$value : $value; $compare_value = !is_array($value) ? (string)$value : $value;
if ($this->getPreloadedValue($cat, $k) === $compare_value) { if (self::getApp()->getConfigValue($cat, $k) === $compare_value) {
return true; return true;
} }
$this->setPreloadedValue($cat, $k, $value); self::getApp()->setConfigValue($cat, $k, $value);
// manage array value // manage array value
$dbvalue = is_array($value) ? serialize($value) : $value; $dbvalue = is_array($value) ? serialize($value) : $value;
@ -81,80 +81,10 @@ class PreloadConfigAdapter extends BaseObject implements IConfigAdapter
public function delete($cat, $k) public function delete($cat, $k)
{ {
$this->deletePreloadedValue($cat, $k); self::getApp()->deleteConfigValue($cat, $k);
$result = dba::delete('config', ['cat' => $cat, 'k' => $k]); $result = dba::delete('config', ['cat' => $cat, 'k' => $k]);
return $result; return $result;
} }
/**
* Retrieves a preloaded value from the App config cache
*
* @param string $cat
* @param string $k
* @param mixed $default
*/
private function getPreloadedValue($cat, $k, $default = null)
{
$a = self::getApp();
$return = $default;
if ($cat === 'config') {
if (isset($a->config[$k])) {
$return = $a->config[$k];
}
} else {
if (isset($a->config[$cat][$k])) {
$return = $a->config[$cat][$k];
}
}
return $return;
}
/**
* Sets a preloaded value in the App config cache
*
* Accepts raw output from the config table
*
* @param string $cat
* @param string $k
* @param mixed $v
*/
private function setPreloadedValue($cat, $k, $v)
{
$a = self::getApp();
// Only arrays are serialized in database, so we have to unserialize sparingly
$value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v;
if ($cat === 'config') {
$a->config[$k] = $value;
} else {
$a->config[$cat][$k] = $value;
}
}
/**
* Deletes a preloaded value from the App config cache
*
* @param string $cat
* @param string $k
*/
private function deletePreloadedValue($cat, $k)
{
$a = self::getApp();
if ($cat === 'config') {
if (isset($a->config[$k])) {
unset($a->config[$k]);
}
} else {
if (isset($a->config[$cat][$k])) {
unset($a->config[$cat][$k]);
}
}
}
} }

View file

@ -11,9 +11,9 @@ use Friendica\Database\DBM;
require_once 'include/dba.php'; require_once 'include/dba.php';
/** /**
* Preload PConfigAdapter * Preload User Configuration Adapter
* *
* Minimize the number of database queries to retrieve configuration values at the cost of memory. * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
* *
* @author Hypolite Petovan <mrpetovan@gmail.com> * @author Hypolite Petovan <mrpetovan@gmail.com>
*/ */
@ -34,7 +34,7 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
$pconfigs = dba::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]); $pconfigs = dba::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
while ($pconfig = dba::fetch($pconfigs)) { while ($pconfig = dba::fetch($pconfigs)) {
$this->setPreloadedValue($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']); self::getApp()->setPConfigValue($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
} }
dba::close($pconfigs); dba::close($pconfigs);
@ -46,13 +46,13 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
if ($refresh) { if ($refresh) {
$config = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]); $config = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
if (DBM::is_result($config)) { if (DBM::is_result($config)) {
$this->setPreloadedValue($uid, $cat, $k, $config['v']); self::getApp()->setPConfigValue($uid, $cat, $k, $config['v']);
} else { } else {
$this->deletePreloadedValue($uid, $cat, $k); self::getApp()->deletePConfigValue($uid, $cat, $k);
} }
} }
$return = $this->getPreloadedValue($uid, $cat, $k, $default_value); $return = self::getApp()->getPConfigValue($uid, $cat, $k, $default_value);
return $return; return $return;
} }
@ -64,11 +64,11 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
// The exception are array values. // The exception are array values.
$compare_value = !is_array($value) ? (string)$value : $value; $compare_value = !is_array($value) ? (string)$value : $value;
if ($this->getPreloadedValue($uid, $cat, $k) === $compare_value) { if (self::getApp()->getPConfigValue($uid, $cat, $k) === $compare_value) {
return true; return true;
} }
$this->setPreloadedValue($uid, $cat, $k, $value); self::getApp()->setPConfigValue($uid, $cat, $k, $value);
// manage array value // manage array value
$dbvalue = is_array($value) ? serialize($value) : $value; $dbvalue = is_array($value) ? serialize($value) : $value;
@ -83,68 +83,10 @@ class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
public function delete($uid, $cat, $k) public function delete($uid, $cat, $k)
{ {
$this->deletePreloadedValue($uid, $cat, $k); self::getApp()->deletePConfigValue($uid, $cat, $k);
$result = dba::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]); $result = dba::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
return $result; return $result;
} }
/**
* Retrieves a preloaded value from the App user config cache
*
* @param int $uid
* @param string $cat
* @param string $k
* @param mixed $default
*/
private function getPreloadedValue($uid, $cat, $k, $default = null)
{
$a = self::getApp();
$return = $default;
if (isset($a->config[$uid][$cat][$k])) {
$return = $a->config[$uid][$cat][$k];
}
return $return;
}
/**
* Sets a preloaded value in the App user config cache
*
* Accepts raw output from the pconfig table
*
* @param int $uid
* @param string $cat
* @param string $k
* @param mixed $v
*/
private function setPreloadedValue($uid, $cat, $k, $v)
{
$a = self::getApp();
// Only arrays are serialized in database, so we have to unserialize sparingly
$value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v;
$a->config[$uid][$cat][$k] = $value;
}
/**
* Deletes a preloaded value from the App user config cache
*
* @param int $uid
* @param string $cat
* @param string $k
*/
private function deletePreloadedValue($uid, $cat, $k)
{
$a = self::getApp();
if (isset($a->config[$uid][$cat][$k])) {
unset($a->config[$uid][$cat][$k]);
}
}
} }