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;
30 $this->connected = DBA::connected();
37 public function load($family = 'config')
39 if (!$this->isConnected()) {
43 if ($this->config_loaded) {
47 $configs = DBA::select('config', ['cat', 'v', 'k']);
48 while ($config = DBA::fetch($configs)) {
49 $this->configCache->set($config['cat'], $config['k'], $config['v']);
53 $this->config_loaded = true;
59 public function get($cat, $k, $default_value = null, $refresh = false)
61 if (!$this->isConnected()) {
62 return $default_value;
66 $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
67 if (DBA::isResult($config)) {
68 $this->configCache->set($cat, $k, $config['v']);
72 $return = $this->configCache->get($cat, $k, $default_value);
80 public function set($cat, $k, $value)
82 if (!$this->isConnected()) {
86 // We store our setting values as strings.
87 // So we have to do the conversion here so that the compare below works.
88 // The exception are array values.
89 $compare_value = !is_array($value) ? (string)$value : $value;
91 if ($this->configCache->get($cat, $k) === $compare_value) {
95 $this->configCache->set($cat, $k, $value);
98 $dbvalue = is_array($value) ? serialize($value) : $value;
100 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
102 throw new Exception('Unable to store config value in [' . $cat . '][' . $k . ']');
111 public function delete($cat, $k)
113 if (!$this->isConnected()) {
117 $this->configCache->delete($cat, $k);
119 $result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);