3 namespace Friendica\Core\Config;
8 use Friendica\BaseObject;
9 use Friendica\Database\DBM;
11 require_once 'include/dba.php';
14 * Preload Configuration Adapter
16 * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
18 * @author Hypolite Petovan <mrpetovan@gmail.com>
20 class PreloadConfigAdapter extends BaseObject implements IConfigAdapter
22 private $config_loaded = false;
24 public function __construct()
29 public function load($family = 'config')
31 if ($this->config_loaded) {
35 $configs = dba::select('config', ['cat', 'v', 'k']);
36 while ($config = dba::fetch($configs)) {
37 self::getApp()->setConfigValue($config['cat'], $config['k'], $config['v']);
41 $this->config_loaded = true;
44 public function get($cat, $k, $default_value = null, $refresh = false)
47 $config = dba::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
48 if (DBM::is_result($config)) {
49 self::getApp()->setConfigValue($cat, $k, $config['v']);
53 $return = self::getApp()->getConfigValue($cat, $k, $default_value);
58 public function set($cat, $k, $value)
60 // We store our setting values as strings.
61 // So we have to do the conversion here so that the compare below works.
62 // The exception are array values.
63 $compare_value = !is_array($value) ? (string)$value : $value;
65 if (self::getApp()->getConfigValue($cat, $k) === $compare_value) {
69 self::getApp()->setConfigValue($cat, $k, $value);
72 $dbvalue = is_array($value) ? serialize($value) : $value;
74 $result = dba::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
76 throw new Exception('Unable to store config value in [' . $cat . '][' . $k . ']');
82 public function delete($cat, $k)
84 self::getApp()->deleteConfigValue($cat, $k);
86 $result = dba::delete('config', ['cat' => $cat, 'k' => $k]);