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 = $config['v'];
39 if (isset($value) && $value !== '') {
40 $return[$key] = $value;
41 $this->in_db[$cat][$key] = true;
46 return [$cat => $config];
52 public function get($cat, $key)
54 if (!$this->isConnected()) {
58 $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
59 if (DBA::isResult($config)) {
61 $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
63 if (isset($value) && $value !== '') {
64 $this->in_db[$cat][$key] = true;
69 $this->in_db[$cat][$key] = false;
76 public function set($cat, $key, $value)
78 if (!$this->isConnected()) {
82 // We store our setting values in a string variable.
83 // So we have to do the conversion here so that the compare below works.
84 // The exception are array values.
85 $dbvalue = (!is_array($value) ? (string)$value : $value);
87 $stored = $this->get($cat, $key);
89 if (!isset($this->in_db[$cat])) {
90 $this->in_db[$cat] = [];
92 if (!isset($this->in_db[$cat][$key])) {
93 $this->in_db[$cat][$key] = false;
96 if (($stored === $dbvalue) && $this->in_db[$cat][$key]) {
100 // manage array value
101 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
103 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
105 $this->in_db[$cat][$key] = $result;
113 public function delete($cat, $key)
115 if (!$this->isConnected()) {
119 if (isset($this->cache[$cat][$key])) {
120 unset($this->in_db[$cat][$key]);
123 $result = DBA::delete('config', ['cat' => $cat, 'k' => $key]);
131 public function isLoaded($cat, $key)
133 if (!$this->isConnected()) {
137 return (isset($this->in_db[$cat][$key])) && $this->in_db[$cat][$key];