3 namespace Friendica\Core\Config;
6 use Friendica\Database\DBA;
9 * Preload 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 PreloadConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
17 private $config_loaded = false;
20 * @var IConfigCache The config cache of this driver
25 * @param IConfigCache $configCache The config cache of this driver
27 public function __construct(IConfigCache $configCache)
29 $this->configCache = $configCache;
36 public function load($family = 'config')
38 if (!$this->isConnected()) {
42 if ($this->config_loaded) {
46 $configs = DBA::select('config', ['cat', 'v', 'k']);
47 while ($config = DBA::fetch($configs)) {
48 $this->configCache->set($config['cat'], $config['k'], $config['v']);
52 $this->config_loaded = true;
58 public function get($cat, $k, $default_value = null, $refresh = false)
60 if (!$this->isConnected()) {
61 return $default_value;
65 $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
66 if (DBA::isResult($config)) {
67 $this->configCache->set($cat, $k, $config['v']);
71 $return = $this->configCache->get($cat, $k, $default_value);
79 public function set($cat, $k, $value)
81 if (!$this->isConnected()) {
85 // We store our setting values as strings.
86 // So we have to do the conversion here so that the compare below works.
87 // The exception are array values.
88 $compare_value = !is_array($value) ? (string)$value : $value;
90 if ($this->configCache->get($cat, $k) === $compare_value) {
94 $this->configCache->set($cat, $k, $value);
97 $dbvalue = is_array($value) ? serialize($value) : $value;
99 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
101 throw new Exception('Unable to store config value in [' . $cat . '][' . $k . ']');
110 public function delete($cat, $k)
112 if (!$this->isConnected()) {
116 $this->configCache->delete($cat, $k);
118 $result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);