friendica/src/Core/Config/JITPConfigAdapter.php

137 lines
3.2 KiB
PHP
Raw Normal View History

2018-03-03 18:09:12 +01:00
<?php
namespace Friendica\Core\Config;
use Friendica\Database\DBA;
2018-03-03 18:09:12 +01:00
/**
2018-03-07 02:04:04 +01:00
* JustInTime User Configuration Adapter
2018-03-03 18:09:12 +01:00
*
* Default PConfig Adapter. Provides the best performance for pages loading few configuration variables.
*
* @author Hypolite Petovan <hypolite@mrpetovan.com>
2018-03-03 18:09:12 +01:00
*/
class JITPConfigAdapter implements IPConfigAdapter
2018-03-03 18:09:12 +01:00
{
private $in_db;
2019-02-03 22:22:04 +01:00
/**
* The config cache of this adapter
* @var IPConfigCache
*/
private $configCache;
2019-02-03 22:22:04 +01:00
/**
* @param IPConfigCache $configCache The config cache of this adapter
2019-02-03 22:22:04 +01:00
*/
public function __construct(IPConfigCache $configCache)
2019-02-03 22:22:04 +01:00
{
$this->configCache = $configCache;
2019-02-03 22:22:04 +01:00
}
2019-02-05 23:42:49 +01:00
/**
* {@inheritdoc}
*/
2018-03-03 18:09:12 +01:00
public function load($uid, $cat)
{
$pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
2018-07-21 14:46:04 +02:00
if (DBA::isResult($pconfigs)) {
while ($pconfig = DBA::fetch($pconfigs)) {
2018-03-03 18:09:12 +01:00
$k = $pconfig['k'];
2018-03-07 02:04:04 +01:00
$this->configCache->setP($uid, $cat, $k, $pconfig['v']);
2018-03-07 02:04:04 +01:00
2018-03-03 18:09:12 +01:00
$this->in_db[$uid][$cat][$k] = true;
}
} else if ($cat != 'config') {
// Negative caching
$this->configCache->setP($uid, $cat, null, "!<unset>!");
2018-03-03 18:09:12 +01:00
}
DBA::close($pconfigs);
2018-03-03 18:09:12 +01:00
}
2019-02-05 23:42:49 +01:00
/**
* {@inheritdoc}
*/
2018-03-03 18:09:12 +01:00
public function get($uid, $cat, $k, $default_value = null, $refresh = false)
{
if (!$refresh) {
// Looking if the whole family isn't set
if ($this->configCache->getP($uid, $cat) !== null) {
if ($this->configCache->getP($uid, $cat) === '!<unset>!') {
2018-03-03 18:09:12 +01:00
return $default_value;
}
}
if ($this->configCache->getP($uid, $cat, $k) !== null) {
if ($this->configCache->getP($uid, $cat, $k) === '!<unset>!') {
2018-03-03 18:09:12 +01:00
return $default_value;
}
return $this->configCache->getP($uid, $cat, $k);
2018-03-03 18:09:12 +01:00
}
}
$pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
2018-07-21 14:46:04 +02:00
if (DBA::isResult($pconfig)) {
2018-03-03 18:09:12 +01:00
$val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
2018-03-07 02:04:04 +01:00
$this->configCache->setP($uid, $cat, $k, $val);
2018-03-07 02:04:04 +01:00
2018-03-03 18:09:12 +01:00
$this->in_db[$uid][$cat][$k] = true;
return $val;
} else {
$this->configCache->setP($uid, $cat, $k, '!<unset>!');
2018-03-07 02:04:04 +01:00
2018-03-03 18:09:12 +01:00
$this->in_db[$uid][$cat][$k] = false;
return $default_value;
}
}
2019-02-05 23:42:49 +01:00
/**
* {@inheritdoc}
*/
2018-03-03 18:09:12 +01:00
public function set($uid, $cat, $k, $value)
{
// We store our setting values in a string variable.
// So we have to do the conversion here so that the compare below works.
// The exception are array values.
$dbvalue = (!is_array($value) ? (string)$value : $value);
$stored = $this->get($uid, $cat, $k, null, true);
if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$k]) {
return true;
}
$this->configCache->setP($uid, $cat, $k, $value);
2018-03-03 18:09:12 +01:00
// manage array value
$dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
$result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
2018-03-03 18:09:12 +01:00
if ($result) {
$this->in_db[$uid][$cat][$k] = true;
}
return $result;
}
2019-02-05 23:42:49 +01:00
/**
* {@inheritdoc}
*/
2018-03-03 18:09:12 +01:00
public function delete($uid, $cat, $k)
{
$this->configCache->deleteP($uid, $cat, $k);
2018-03-03 18:09:12 +01:00
2018-03-07 02:04:04 +01:00
if (!empty($this->in_db[$uid][$cat][$k])) {
2018-03-03 18:09:12 +01:00
unset($this->in_db[$uid][$cat][$k]);
}
$result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
2018-03-03 18:09:12 +01:00
return $result;
}
}