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 = $this->toConfigValue($config['v']);
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)) {
58 $value = $this->toConfigValue($config['v']);
71 public function set($cat, $key, $value)
73 if (!$this->isConnected()) {
77 // We store our setting values as strings.
78 // So we have to do the conversion here so that the compare below works.
79 // The exception are array values.
80 $compare_value = !is_array($value) ? (string)$value : $value;
81 $stored_value = $this->get($cat, $key);
83 if (isset($stored_value) && $stored_value === $compare_value) {
87 $dbvalue = $this->toDbValue($value);
89 return DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
95 public function delete($cat, $key)
97 if (!$this->isConnected()) {
101 return DBA::delete('config', ['cat' => $cat, 'k' => $key]);
107 public function isLoaded($cat, $key)
109 if (!$this->isConnected()) {
113 return $this->config_loaded;