]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Config/JITConfigAdapter.php
Merge pull request #6612 from MrPetovan/bug/6605-cache-config-adapter-connection
[friendica.git] / src / Core / Config / JITConfigAdapter.php
index 256a2a956f4d1aded6c1ecf616054e2f9c9fa657..ecd88bb3d3d5d9f609dbb706d64a5f7eccfd1db3 100644 (file)
@@ -10,7 +10,7 @@ use Friendica\Database\DBA;
  *
  * @author Hypolite Petovan <hypolite@mrpetovan.com>
  */
-class JITConfigAdapter implements IConfigAdapter
+class JITConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
 {
        private $cache;
        private $in_db;
@@ -18,18 +18,26 @@ class JITConfigAdapter implements IConfigAdapter
        /**
         * @var IConfigCache The config cache of this driver
         */
-       private $config;
+       private $configCache;
 
        /**
-        * @param IConfigCache $config The config cache of this driver
+        * @param IConfigCache $configCache The config cache of this driver
         */
-       public function __construct($config)
+       public function __construct(IConfigCache $configCache)
        {
-               $this->config = $config;
+               $this->configCache = $configCache;
+               $this->connected = DBA::connected();
        }
 
+       /**
+        * {@inheritdoc}
+        */
        public function load($cat = "config")
        {
+               if (!$this->isConnected()) {
+                       return;
+               }
+
                // We don't preload "system" anymore.
                // This reduces the number of database reads a lot.
                if ($cat === 'system') {
@@ -40,7 +48,7 @@ class JITConfigAdapter implements IConfigAdapter
                while ($config = DBA::fetch($configs)) {
                        $k = $config['k'];
 
-                       $this->config->set($cat, $k, $config['v']);
+                       $this->configCache->set($cat, $k, $config['v']);
 
                        if ($cat !== 'config') {
                                $this->cache[$cat][$k] = $config['v'];
@@ -50,8 +58,15 @@ class JITConfigAdapter implements IConfigAdapter
                DBA::close($configs);
        }
 
+       /**
+        * {@inheritdoc}
+        */
        public function get($cat, $k, $default_value = null, $refresh = false)
        {
+               if (!$this->isConnected()) {
+                       return $default_value;
+               }
+
                if (!$refresh) {
                        // Do we have the cached value? Then return it
                        if (isset($this->cache[$cat][$k])) {
@@ -72,18 +87,18 @@ class JITConfigAdapter implements IConfigAdapter
                        $this->cache[$cat][$k] = $value;
                        $this->in_db[$cat][$k] = true;
                        return $value;
-               } elseif ($this->config->get($cat, $k) !== null) {
+               } elseif ($this->configCache->get($cat, $k) !== null) {
                        // Assign the value (mostly) from config/local.config.php file to the cache
-                       $this->cache[$cat][$k] = $this->config->get($cat, $k);
+                       $this->cache[$cat][$k] = $this->configCache->get($cat, $k);
                        $this->in_db[$cat][$k] = false;
 
-                       return $this->config->get($cat, $k);
-               } elseif ($this->config->get('config', $k) !== null) {
+                       return $this->configCache->get($cat, $k);
+               } elseif ($this->configCache->get('config', $k) !== null) {
                        // Assign the value (mostly) from config/local.config.php file to the cache
-                       $this->cache[$k] = $this->config->get('config', $k);
+                       $this->cache[$k] = $this->configCache->get('config', $k);
                        $this->in_db[$k] = false;
 
-                       return $this->config->get('config', $k);
+                       return $this->configCache->get('config', $k);
                }
 
                $this->cache[$cat][$k] = '!<unset>!';
@@ -92,8 +107,15 @@ class JITConfigAdapter implements IConfigAdapter
                return $default_value;
        }
 
+       /**
+        * {@inheritdoc}
+        */
        public function set($cat, $k, $value)
        {
+               if (!$this->isConnected()) {
+                       return false;
+               }
+
                // We store our setting values in a string variable.
                // So we have to do the conversion here so that the compare below works.
                // The exception are array values.
@@ -112,7 +134,7 @@ class JITConfigAdapter implements IConfigAdapter
                        return true;
                }
 
-               $this->config->set($cat, $k, $value);
+               $this->configCache->set($cat, $k, $value);
 
                // Assign the just added value to the cache
                $this->cache[$cat][$k] = $dbvalue;
@@ -129,8 +151,15 @@ class JITConfigAdapter implements IConfigAdapter
                return $result;
        }
 
+       /**
+        * {@inheritdoc}
+        */
        public function delete($cat, $k)
        {
+               if (!$this->isConnected()) {
+                       return false;
+               }
+
                if (isset($this->cache[$cat][$k])) {
                        unset($this->cache[$cat][$k]);
                        unset($this->in_db[$cat][$k]);