3 namespace Friendica\Core\Config;
6 use Friendica\Database\DBA;
9 * Preload User Configuration Adapter
11 * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
13 * @author Hypolite Petovan <hypolite@mrpetovan.com>
15 class PreloadPConfigAdapter implements IPConfigAdapter
17 private $config_loaded = false;
20 * The config cache of this adapter
26 * @param IPConfigCache $configCache The config cache of this adapter
27 * @param int $uid The UID of the current user
29 public function __construct(IPConfigCache $configCache, $uid = null)
31 $this->configCache = $configCache;
33 $this->load($uid, 'config');
40 public function load($uid, $family)
42 if ($this->config_loaded) {
50 $pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
51 while ($pconfig = DBA::fetch($pconfigs)) {
52 $this->configCache->setP($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
54 DBA::close($pconfigs);
56 $this->config_loaded = true;
62 public function get($uid, $cat, $k, $default_value = null, $refresh = false)
64 if (!$this->config_loaded) {
65 $this->load($uid, $cat);
69 $config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
70 if (DBA::isResult($config)) {
71 $this->configCache->setP($uid, $cat, $k, $config['v']);
73 $this->configCache->deleteP($uid, $cat, $k);
77 return $this->configCache->getP($uid, $cat, $k, $default_value);;
83 public function set($uid, $cat, $k, $value)
85 if (!$this->config_loaded) {
86 $this->load($uid, $cat);
88 // We store our setting values as strings.
89 // So we have to do the conversion here so that the compare below works.
90 // The exception are array values.
91 $compare_value = !is_array($value) ? (string)$value : $value;
93 if ($this->configCache->getP($uid, $cat, $k) === $compare_value) {
97 $this->configCache->setP($uid, $cat, $k, $value);
100 $dbvalue = is_array($value) ? serialize($value) : $value;
102 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
104 throw new Exception('Unable to store config value in [' . $uid . '][' . $cat . '][' . $k . ']');
113 public function delete($uid, $cat, $k)
115 if (!$this->config_loaded) {
116 $this->load($uid, $cat);
119 $this->configCache->deleteP($uid, $cat, $k);
121 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);