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)) {
38 $return[$key] = $config['v'];
39 $this->in_db[$cat][$key] = true;
43 return [$cat => $config];
49 public function get($cat, $key)
51 if (!$this->isConnected()) {
55 $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
56 if (DBA::isResult($config)) {
58 $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
60 $this->in_db[$cat][$key] = true;
64 $this->in_db[$cat][$key] = false;
72 public function set($cat, $key, $value)
74 if (!$this->isConnected()) {
78 // We store our setting values in a string variable.
79 // So we have to do the conversion here so that the compare below works.
80 // The exception are array values.
81 $dbvalue = (!is_array($value) ? (string)$value : $value);
83 $stored = $this->get($cat, $key);
85 if (!isset($this->in_db[$cat])) {
86 $this->in_db[$cat] = [];
88 if (!isset($this->in_db[$cat][$key])) {
89 $this->in_db[$cat][$key] = false;
92 if (($stored === $dbvalue) && $this->in_db[$cat][$key]) {
97 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
99 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
101 $this->in_db[$cat][$key] = $result;
109 public function delete($cat, $key)
111 if (!$this->isConnected()) {
115 if (isset($this->cache[$cat][$key])) {
116 unset($this->in_db[$cat][$key]);
119 $result = DBA::delete('config', ['cat' => $cat, 'k' => $key]);
127 public function isLoaded($cat, $key)
129 if (!$this->isConnected()) {
133 return (isset($this->in_db[$cat][$key])) && $this->in_db[$cat][$key];