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 $return[$config['cat']][$config['k']] = $config['v'];
39 $this->config_loaded = true;
47 public function get($cat, $key)
49 if (!$this->isConnected()) {
53 $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
54 if (DBA::isResult($config)) {
56 $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
58 if (isset($value) && $value !== '') {
69 public function set($cat, $key, $value)
71 if (!$this->isConnected()) {
75 // We store our setting values as strings.
76 // So we have to do the conversion here so that the compare below works.
77 // The exception are array values.
78 $compare_value = !is_array($value) ? (string)$value : $value;
80 if ($this->get($cat, $key) === $compare_value) {
85 $dbvalue = is_array($value) ? serialize($value) : $value;
87 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
95 public function delete($cat, $key)
97 if (!$this->isConnected()) {
101 $result = DBA::delete('config', ['cat' => $cat, 'k' => $key]);
109 public function isLoaded($cat, $key)
111 if (!$this->isConnected()) {
115 return $this->config_loaded;