2 namespace Friendica\Core\Config\Adapter;
4 use Friendica\Core\Logger;
5 use Friendica\Database\DBA;
8 * JustInTime Configuration Adapter
10 * Default Config Adapter. Provides the best performance for pages loading few configuration variables.
12 * @author Hypolite Petovan <hypolite@mrpetovan.com>
14 class JITConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
21 public function load($cat = "config")
25 if (!$this->isConnected()) {
29 // We don't preload "system" anymore.
30 // This reduces the number of database reads a lot.
31 if ($cat === 'system') {
35 $configs = DBA::select('config', ['v', 'k'], ['cat' => $cat]);
36 while ($config = DBA::fetch($configs)) {
39 $return[$key] = $config['v'];
40 $this->in_db[$cat][$key] = true;
44 return [$cat => $config];
50 public function get($cat, $key)
52 if (!$this->isConnected()) {
56 $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
57 if (DBA::isResult($config)) {
59 $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
61 if ($key === 'last_worker_execution') {
62 Logger::alert('catchmeifyou', ['store' => $value, 'in_db' => $this->in_db[$cat][$key]]);
65 if (isset($value) && $value !== '') {
66 $this->in_db[$cat][$key] = true;
69 $this->in_db[$cat][$key] = false;
74 $this->in_db[$cat][$key] = false;
82 public function set($cat, $key, $value)
84 if (!$this->isConnected()) {
88 // We store our setting values in a string variable.
89 // So we have to do the conversion here so that the compare below works.
90 // The exception are array values.
91 $dbvalue = (!is_array($value) ? (string)$value : $value);
93 $stored = $this->get($cat, $key);
95 if (!isset($this->in_db[$cat])) {
96 $this->in_db[$cat] = [];
98 if (!isset($this->in_db[$cat][$key])) {
99 $this->in_db[$cat][$key] = false;
102 if ($key === 'last_worker_execution') {
103 Logger::alert('catchmeifyou', ['db' => $dbvalue, 'store' => $stored, 'in_db' => $this->in_db[$cat][$key]]);
106 if (($stored === $dbvalue) && $this->in_db[$cat][$key]) {
110 // manage array value
111 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
113 $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
115 $this->in_db[$cat][$key] = $result;
123 public function delete($cat, $key)
125 if (!$this->isConnected()) {
129 if (isset($this->cache[$cat][$key])) {
130 unset($this->in_db[$cat][$key]);
133 $result = DBA::delete('config', ['cat' => $cat, 'k' => $key]);
141 public function isLoaded($cat, $key)
143 if (!$this->isConnected()) {
147 return (isset($this->in_db[$cat][$key])) && $this->in_db[$cat][$key];