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 extends AbstractDbaConfigAdapter 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;
29 $this->connected = DBA::connected();
35 public function load($cat = "config")
37 if (!$this->isConnected()) {
41 // We don't preload "system" anymore.
42 // This reduces the number of database reads a lot.
43 if ($cat === 'system') {
47 $configs = DBA::select('config', ['v', 'k'], ['cat' => $cat]);
48 while ($config = DBA::fetch($configs)) {
51 $this->configCache->set($cat, $k, $config['v']);
53 if ($cat !== 'config') {
54 $this->cache[$cat][$k] = $config['v'];
55 $this->in_db[$cat][$k] = true;
64 public function get($cat, $k, $default_value = null, $refresh = false)
66 if (!$this->isConnected()) {
67 return $default_value;
71 // Do we have the cached value? Then return it
72 if (isset($this->cache[$cat][$k])) {
73 if ($this->cache[$cat][$k] === '!<unset>!') {
74 return $default_value;
76 return $this->cache[$cat][$k];
81 $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
82 if (DBA::isResult($config)) {
84 $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
86 // Assign the value from the database to the cache
87 $this->cache[$cat][$k] = $value;
88 $this->in_db[$cat][$k] = true;
90 } elseif ($this->configCache->get($cat, $k) !== null) {
91 // Assign the value (mostly) from config/local.config.php file to the cache
92 $this->cache[$cat][$k] = $this->configCache->get($cat, $k);
93 $this->in_db[$cat][$k] = false;
95 return $this->configCache->get($cat, $k);
96 } elseif ($this->configCache->get('config', $k) !== null) {
97 // Assign the value (mostly) from config/local.config.php file to the cache
98 $this->cache[$k] = $this->configCache->get('config', $k);
99 $this->in_db[$k] = false;
101 return $this->configCache->get('config', $k);
104 $this->cache[$cat][$k] = '!<unset>!';
105 $this->in_db[$cat][$k] = false;
107 return $default_value;
113 public function set($cat, $k, $value)
115 if (!$this->isConnected()) {
119 // We store our setting values in a string variable.
120 // So we have to do the conversion here so that the compare below works.
121 // The exception are array values.
122 $dbvalue = (!is_array($value) ? (string)$value : $value);
124 $stored = $this->get($cat, $k, null, true);
126 if (!isset($this->in_db[$cat])) {
127 $this->in_db[$cat] = [];
129 if (!isset($this->in_db[$cat][$k])) {
130 $this->in_db[$cat] = false;
133 if (($stored === $dbvalue) && $this->in_db[$cat][$k]) {
137 $this->configCache->set($cat, $k, $value);
139 // Assign the just added value to the cache
140 $this->cache[$cat][$k] = $dbvalue;
142 // manage array value
143 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
145 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
148 $this->in_db[$cat][$k] = true;
157 public function delete($cat, $k)
159 if (!$this->isConnected()) {
163 if (isset($this->cache[$cat][$k])) {
164 unset($this->cache[$cat][$k]);
165 unset($this->in_db[$cat][$k]);
168 $result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);