3 namespace Friendica\Core\Config\Adapter;
5 use Friendica\Database\DBA;
8 * Preload Configuration Adapter
10 * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
12 * @author Hypolite Petovan <hypolite@mrpetovan.com>
14 class PreloadConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
16 private $config_loaded = false;
21 public function load($cat = 'config')
25 if (!$this->isConnected()) {
29 if ($this->config_loaded) {
33 $configs = DBA::select('config', ['cat', 'v', 'k']);
34 while ($config = DBA::fetch($configs)) {
35 $value = $config['v'];
36 if (isset($value) && $value !== '') {
37 $return[$config['cat']][$config['k']] = $value;
42 $this->config_loaded = true;
50 public function get($cat, $key)
52 if (!$this->isConnected()) {
56 $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
57 if (DBA::isResult($config)) {
59 $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
61 if (isset($value) && $value !== '') {
72 public function set($cat, $key, $value)
74 if (!$this->isConnected()) {
78 // We store our setting values as strings.
79 // So we have to do the conversion here so that the compare below works.
80 // The exception are array values.
81 $compare_value = !is_array($value) ? (string)$value : $value;
83 if ($this->get($cat, $key) === $compare_value) {
88 $dbvalue = is_array($value) ? serialize($value) : $value;
90 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
98 public function delete($cat, $key)
100 if (!$this->isConnected()) {
104 $result = DBA::delete('config', ['cat' => $cat, 'k' => $key]);
112 public function isLoaded($cat, $key)
114 if (!$this->isConnected()) {
118 return $this->config_loaded;