2 namespace Friendica\Core\Config\Adapter;
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
20 public function load($cat = "config")
24 if (!$this->isConnected()) {
28 // We don't preload "system" anymore.
29 // This reduces the number of database reads a lot.
30 if ($cat === 'system') {
34 $configs = DBA::select('config', ['v', 'k'], ['cat' => $cat]);
35 while ($config = DBA::fetch($configs)) {
37 $value = $this->toConfigValue($config['v']);
39 // The value was in the db, so don't check it again (unless you have to)
40 $this->in_db[$cat][$key] = true;
42 // just save it in case it is set
44 $return[$key] = $value;
49 return [$cat => $return];
55 * @param bool $mark if true, mark the selection of the current cat/key pair
57 public function get($cat, $key, $mark = true)
59 if (!$this->isConnected()) {
63 // The value got checked, so mark it to avoid checking it over and over again
65 $this->in_db[$cat][$key] = true;
68 $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
69 if (DBA::isResult($config)) {
70 $value = $this->toConfigValue($config['v']);
72 // just return it in case it is set
84 public function set($cat, $key, $value)
86 if (!$this->isConnected()) {
90 // We store our setting values in a string variable.
91 // So we have to do the conversion here so that the compare below works.
92 // The exception are array values.
93 $compare_value = (!is_array($value) ? (string)$value : $value);
94 $stored_value = $this->get($cat, $key, false);
96 if (!isset($this->in_db[$cat])) {
97 $this->in_db[$cat] = [];
99 if (!isset($this->in_db[$cat][$key])) {
100 $this->in_db[$cat][$key] = false;
103 if (isset($stored_value) && ($stored_value === $compare_value) && $this->in_db[$cat][$key]) {
107 $dbvalue = $this->toDbValue($value);
109 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
111 $this->in_db[$cat][$key] = $result;
119 public function delete($cat, $key)
121 if (!$this->isConnected()) {
125 if (isset($this->cache[$cat][$key])) {
126 unset($this->in_db[$cat][$key]);
129 $result = DBA::delete('config', ['cat' => $cat, 'k' => $key]);
137 public function isLoaded($cat, $key)
139 if (!$this->isConnected()) {
143 return (isset($this->in_db[$cat][$key])) && $this->in_db[$cat][$key];