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;
39 $return[$config['cat']][$config['k']] = '!<unset>!';
44 $this->config_loaded = true;
52 public function get($cat, $key)
54 if (!$this->isConnected()) {
58 $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
59 if (DBA::isResult($config)) {
61 $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
63 if (isset($value) && $value !== '') {
74 public function set($cat, $key, $value)
76 if (!$this->isConnected()) {
80 // We store our setting values as strings.
81 // So we have to do the conversion here so that the compare below works.
82 // The exception are array values.
83 $compare_value = !is_array($value) ? (string)$value : $value;
85 if ($this->get($cat, $key) === $compare_value) {
90 $dbvalue = is_array($value) ? serialize($value) : $value;
92 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
100 public function delete($cat, $key)
102 if (!$this->isConnected()) {
106 $result = DBA::delete('config', ['cat' => $cat, 'k' => $key]);
114 public function isLoaded($cat, $key)
116 if (!$this->isConnected()) {
120 return $this->config_loaded;