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)) {
71 $value = $this->toConfigValue($config['v']);
73 // just return it in case it is set
85 public function set($cat, $key, $value)
87 if (!$this->isConnected()) {
91 // We store our setting values in a string variable.
92 // So we have to do the conversion here so that the compare below works.
93 // The exception are array values.
94 $dbvalue = (!is_array($value) ? (string)$value : $value);
96 $stored = $this->get($cat, $key, false);
98 if (!isset($this->in_db[$cat])) {
99 $this->in_db[$cat] = [];
101 if (!isset($this->in_db[$cat][$key])) {
102 $this->in_db[$cat][$key] = false;
105 if (isset($stored) && ($stored === $dbvalue) && $this->in_db[$cat][$key]) {
109 $dbvalue = $this->toDbValue($dbvalue);
111 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
113 $this->in_db[$cat][$key] = $result;
121 public function delete($cat, $key)
123 if (!$this->isConnected()) {
127 if (isset($this->cache[$cat][$key])) {
128 unset($this->in_db[$cat][$key]);
131 $result = DBA::delete('config', ['cat' => $cat, 'k' => $key]);
139 public function isLoaded($cat, $key)
141 if (!$this->isConnected()) {
145 return (isset($this->in_db[$cat][$key])) && $this->in_db[$cat][$key];