3 namespace Friendica\Core\Config;
6 use Friendica\BaseObject;
7 use Friendica\Database\DBA;
10 * Preload User Configuration Adapter
12 * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
14 * @author Hypolite Petovan <hypolite@mrpetovan.com>
16 class PreloadPConfigAdapter extends BaseObject implements IPConfigAdapter
18 private $config_loaded = false;
20 public function __construct($uid)
22 $this->load($uid, 'config');
25 public function load($uid, $family)
27 if ($this->config_loaded) {
35 $pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
36 while ($pconfig = DBA::fetch($pconfigs)) {
37 self::getApp()->setPConfigValue($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
39 DBA::close($pconfigs);
41 $this->config_loaded = true;
44 public function get($uid, $cat, $k, $default_value = null, $refresh = false)
46 if (!$this->config_loaded) {
47 $this->load($uid, $cat);
51 $config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
52 if (DBA::isResult($config)) {
53 self::getApp()->setPConfigValue($uid, $cat, $k, $config['v']);
55 self::getApp()->deletePConfigValue($uid, $cat, $k);
59 $return = self::getApp()->getPConfigValue($uid, $cat, $k, $default_value);
64 public function set($uid, $cat, $k, $value)
66 if (!$this->config_loaded) {
67 $this->load($uid, $cat);
69 // We store our setting values as strings.
70 // So we have to do the conversion here so that the compare below works.
71 // The exception are array values.
72 $compare_value = !is_array($value) ? (string)$value : $value;
74 if (self::getApp()->getPConfigValue($uid, $cat, $k) === $compare_value) {
78 self::getApp()->setPConfigValue($uid, $cat, $k, $value);
81 $dbvalue = is_array($value) ? serialize($value) : $value;
83 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
85 throw new Exception('Unable to store config value in [' . $uid . '][' . $cat . '][' . $k . ']');
91 public function delete($uid, $cat, $k)
93 if (!$this->config_loaded) {
94 $this->load($uid, $cat);
97 self::getApp()->deletePConfigValue($uid, $cat, $k);
99 $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);