2 namespace Friendica\Core\Config;
4 use Friendica\Database\DBA;
7 * JustInTime Configuration Adapter
9 * Default Config Adapter. Provides the best performance for pages loading few configuration variables.
11 * @author Hypolite Petovan <hypolite@mrpetovan.com>
13 class JITConfigAdapter implements IConfigAdapter
19 * @var IConfigCache The config cache of this driver
24 * @param IConfigCache $configCache The config cache of this driver
26 public function __construct(IConfigCache $configCache)
28 $this->configCache = $configCache;
34 public function load($cat = "config")
36 // We don't preload "system" anymore.
37 // This reduces the number of database reads a lot.
38 if ($cat === 'system') {
42 $configs = DBA::select('config', ['v', 'k'], ['cat' => $cat]);
43 while ($config = DBA::fetch($configs)) {
46 $this->configCache->set($cat, $k, $config['v']);
48 if ($cat !== 'config') {
49 $this->cache[$cat][$k] = $config['v'];
50 $this->in_db[$cat][$k] = true;
59 public function get($cat, $k, $default_value = null, $refresh = false)
62 // Do we have the cached value? Then return it
63 if (isset($this->cache[$cat][$k])) {
64 if ($this->cache[$cat][$k] === '!<unset>!') {
65 return $default_value;
67 return $this->cache[$cat][$k];
72 $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
73 if (DBA::isResult($config)) {
75 $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
77 // Assign the value from the database to the cache
78 $this->cache[$cat][$k] = $value;
79 $this->in_db[$cat][$k] = true;
81 } elseif ($this->configCache->get($cat, $k) !== null) {
82 // Assign the value (mostly) from config/local.config.php file to the cache
83 $this->cache[$cat][$k] = $this->configCache->get($cat, $k);
84 $this->in_db[$cat][$k] = false;
86 return $this->configCache->get($cat, $k);
87 } elseif ($this->configCache->get('config', $k) !== null) {
88 // Assign the value (mostly) from config/local.config.php file to the cache
89 $this->cache[$k] = $this->configCache->get('config', $k);
90 $this->in_db[$k] = false;
92 return $this->configCache->get('config', $k);
95 $this->cache[$cat][$k] = '!<unset>!';
96 $this->in_db[$cat][$k] = false;
98 return $default_value;
104 public function set($cat, $k, $value)
106 // We store our setting values in a string variable.
107 // So we have to do the conversion here so that the compare below works.
108 // The exception are array values.
109 $dbvalue = (!is_array($value) ? (string)$value : $value);
111 $stored = $this->get($cat, $k, null, true);
113 if (!isset($this->in_db[$cat])) {
114 $this->in_db[$cat] = [];
116 if (!isset($this->in_db[$cat][$k])) {
117 $this->in_db[$cat] = false;
120 if (($stored === $dbvalue) && $this->in_db[$cat][$k]) {
124 $this->configCache->set($cat, $k, $value);
126 // Assign the just added value to the cache
127 $this->cache[$cat][$k] = $dbvalue;
129 // manage array value
130 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
132 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
135 $this->in_db[$cat][$k] = true;
144 public function delete($cat, $k)
146 if (isset($this->cache[$cat][$k])) {
147 unset($this->cache[$cat][$k]);
148 unset($this->in_db[$cat][$k]);
151 $result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);