Restore Preload config adapter features

- Limit to 1 DB query per adapter
This commit is contained in:
Hypolite Petovan 2019-06-24 21:33:47 -04:00
parent 26accbe3ef
commit 60cc05f19f
3 changed files with 39 additions and 56 deletions

View File

@ -13,12 +13,22 @@ use Friendica\Database\DBA;
*/
class PreloadConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
{
private $config_loaded = false;
/**
* @var array Contains the whole node config, loaded on instantiation
*/
private $config;
public function __construct()
{
parent::__construct();
$this->load();
}
/**
* {@inheritdoc}
*/
public function load($cat = 'config')
public function load($cat = null)
{
$return = [];
@ -26,8 +36,8 @@ class PreloadConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAd
return $return;
}
if ($this->config_loaded) {
return $return;
if ($this->isLoaded()) {
return $this->config;
}
$configs = DBA::select('config', ['cat', 'v', 'k']);
@ -39,7 +49,7 @@ class PreloadConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAd
}
DBA::close($configs);
$this->config_loaded = true;
$this->config = $return;
return $return;
}
@ -53,16 +63,7 @@ class PreloadConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAd
return null;
}
$config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
if (DBA::isResult($config)) {
$value = $this->toConfigValue($config['v']);
if (isset($value)) {
return $value;
}
}
return null;
return $this->config[$cat][$key] ?? null;
}
/**
@ -84,6 +85,8 @@ class PreloadConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAd
return true;
}
$this->config[$cat][$key] = $value;
$dbvalue = $this->toDbValue($value);
return DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
@ -98,18 +101,20 @@ class PreloadConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAd
return false;
}
unset($this->config[$cat][$key]);
return DBA::delete('config', ['cat' => $cat, 'k' => $key]);
}
/**
* {@inheritdoc}
*/
public function isLoaded($cat, $key)
public function isLoaded($cat = null, $key = null)
{
if (!$this->isConnected()) {
return false;
}
return $this->config_loaded;
return !empty($this->config);
}
}

View File

@ -14,28 +14,14 @@ use Friendica\Database\DBA;
class PreloadPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfigAdapter
{
/**
* @var array true if config for user is loaded
* @var array Contains the users' config from the DB
*/
private $config_loaded;
/**
* @param int $uid The UID of the current user
*/
public function __construct($uid = null)
{
parent::__construct();
$this->config_loaded = [];
if (isset($uid)) {
$this->load($uid, 'config');
}
}
private $config;
/**
* {@inheritdoc}
*/
public function load($uid, $cat)
public function load($uid, $cat = null)
{
$return = [];
@ -43,8 +29,8 @@ class PreloadPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfig
return $return;
}
if (!$this->isLoaded($uid, $cat, null)) {
return $return;
if ($this->isLoaded($uid)) {
return $this->config[$uid];
}
$pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
@ -56,7 +42,7 @@ class PreloadPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfig
}
DBA::close($pconfigs);
$this->config_loaded[$uid] = true;
$this->config[$uid] = $return;
return $return;
}
@ -71,18 +57,10 @@ class PreloadPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfig
}
if (!$this->isLoaded($uid, $cat, $key)) {
$this->load($uid, $cat);
$this->load($uid);
}
$config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
if (DBA::isResult($config)) {
$value = $this->toConfigValue($config['v']);
if (isset($value)) {
return $value;
}
}
return null;
return $this->config[$uid][$cat][$key] ?? null;
}
/**
@ -95,8 +73,9 @@ class PreloadPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfig
}
if (!$this->isLoaded($uid, $cat, $key)) {
$this->load($uid, $cat);
$this->load($uid);
}
// We store our setting values as strings.
// So we have to do the conversion here so that the compare below works.
// The exception are array values.
@ -107,6 +86,8 @@ class PreloadPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfig
return true;
}
$this->config[$uid][$cat][$key] = $value;
$dbvalue = $this->toDbValue($value);
return DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
@ -121,9 +102,7 @@ class PreloadPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfig
return false;
}
if (!$this->isLoaded($uid, $cat, $key)) {
$this->load($uid, $cat);
}
unset($this->config[$uid][$cat][$key]);
return DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
}
@ -131,12 +110,12 @@ class PreloadPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfig
/**
* {@inheritdoc}
*/
public function isLoaded($uid, $cat, $key)
public function isLoaded($uid, $cat = null, $key = null)
{
if (!$this->isConnected()) {
return false;
}
return isset($this->config_loaded[$uid]) && $this->config_loaded[$uid];
return !empty($this->config[$uid]);
}
}

View File

@ -46,14 +46,13 @@ class ConfigFactory
/**
* @param Cache\ConfigCache $configCache The config cache of this adapter
* @param int $uid The UID of the current user
*
* @return Config\PConfiguration
*/
public static function createPConfig(Cache\ConfigCache $configCache, $uid = null)
public static function createPConfig(Cache\ConfigCache $configCache)
{
if ($configCache->get('system', 'config_adapter') === 'preload') {
$configAdapter = new Adapter\PreloadPConfigAdapter($uid);
$configAdapter = new Adapter\PreloadPConfigAdapter();
} else {
$configAdapter = new Adapter\JITPConfigAdapter();
}