Refactor ConfigMockTrait to mocked ConfigCache
This commit is contained in:
@@ -22,7 +22,7 @@ use Friendica\Core\Config\IConfigCache;
|
||||
class Config
|
||||
{
|
||||
/**
|
||||
* @var Config\IConfigAdapter
|
||||
* @var Config\IConfigAdapter|null
|
||||
*/
|
||||
private static $adapter;
|
||||
|
||||
@@ -62,7 +62,7 @@ class Config
|
||||
*/
|
||||
public static function load($family = "config")
|
||||
{
|
||||
if (!isset(self::$adapter)) {
|
||||
if (!isset(self::$adapter) || !self::$adapter->isConnected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ class Config
|
||||
*/
|
||||
public static function get($family, $key, $default_value = null, $refresh = false)
|
||||
{
|
||||
if (!isset(self::$adapter)) {
|
||||
if (!isset(self::$adapter) || !self::$adapter->isConnected()) {
|
||||
return self::$cache->get($family, $key, $default_value);
|
||||
}
|
||||
|
||||
@@ -108,9 +108,8 @@ class Config
|
||||
*/
|
||||
public static function set($family, $key, $value)
|
||||
{
|
||||
if (!isset(self::$adapter)) {
|
||||
self::$cache->set($family, $key, $value);
|
||||
return true;
|
||||
if (!isset(self::$adapter) || !self::$adapter->isConnected()) {
|
||||
return self::$cache->set($family, $key, $value);
|
||||
}
|
||||
|
||||
return self::$adapter->set($family, $key, $value);
|
||||
@@ -129,7 +128,7 @@ class Config
|
||||
*/
|
||||
public static function delete($family, $key)
|
||||
{
|
||||
if (!isset(self::$adapter)) {
|
||||
if (!isset(self::$adapter) || !self::$adapter->isConnected()) {
|
||||
self::$cache->delete($family, $key);
|
||||
}
|
||||
|
||||
|
||||
13
src/Core/Config/AbstractDbaConfigAdapter.php
Normal file
13
src/Core/Config/AbstractDbaConfigAdapter.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Friendica\Core\Config;
|
||||
|
||||
use Friendica\Database\DBA;
|
||||
|
||||
abstract class AbstractDbaConfigAdapter
|
||||
{
|
||||
public function isConnected()
|
||||
{
|
||||
return DBA::connected();
|
||||
}
|
||||
}
|
||||
@@ -94,6 +94,8 @@ class ConfigCache implements IConfigCache, IPConfigCache
|
||||
|
||||
$this->config[$cat][$key] = $value;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -54,4 +54,11 @@ interface IConfigAdapter
|
||||
* @return mixed
|
||||
*/
|
||||
public function delete($cat, $k);
|
||||
|
||||
/**
|
||||
* Checks, if the current adapter is connected to the backend
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isConnected();
|
||||
}
|
||||
|
||||
@@ -22,6 +22,8 @@ interface IConfigCache
|
||||
* @param string $cat Config category
|
||||
* @param string $key Config key
|
||||
* @param mixed $value Value to set
|
||||
*
|
||||
* @return bool True, if the value is set
|
||||
*/
|
||||
function set($cat, $key, $value);
|
||||
|
||||
|
||||
@@ -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;
|
||||
@@ -33,6 +33,10 @@ class JITConfigAdapter implements IConfigAdapter
|
||||
*/
|
||||
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') {
|
||||
@@ -58,6 +62,10 @@ class JITConfigAdapter implements IConfigAdapter
|
||||
*/
|
||||
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])) {
|
||||
@@ -103,6 +111,10 @@ class JITConfigAdapter implements IConfigAdapter
|
||||
*/
|
||||
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.
|
||||
@@ -143,6 +155,10 @@ class JITConfigAdapter implements IConfigAdapter
|
||||
*/
|
||||
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]);
|
||||
|
||||
@@ -12,7 +12,7 @@ use Friendica\Database\DBA;
|
||||
*
|
||||
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
||||
*/
|
||||
class PreloadConfigAdapter implements IConfigAdapter
|
||||
class PreloadConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
|
||||
{
|
||||
private $config_loaded = false;
|
||||
|
||||
@@ -35,6 +35,10 @@ class PreloadConfigAdapter implements IConfigAdapter
|
||||
*/
|
||||
public function load($family = 'config')
|
||||
{
|
||||
if (!$this->isConnected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->config_loaded) {
|
||||
return;
|
||||
}
|
||||
@@ -53,6 +57,10 @@ class PreloadConfigAdapter implements IConfigAdapter
|
||||
*/
|
||||
public function get($cat, $k, $default_value = null, $refresh = false)
|
||||
{
|
||||
if (!$this->isConnected()) {
|
||||
return $default_value;
|
||||
}
|
||||
|
||||
if ($refresh) {
|
||||
$config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
|
||||
if (DBA::isResult($config)) {
|
||||
@@ -70,6 +78,10 @@ class PreloadConfigAdapter implements IConfigAdapter
|
||||
*/
|
||||
public function set($cat, $k, $value)
|
||||
{
|
||||
if (!$this->isConnected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// We store our setting values as strings.
|
||||
// So we have to do the conversion here so that the compare below works.
|
||||
// The exception are array values.
|
||||
@@ -97,6 +109,10 @@ class PreloadConfigAdapter implements IConfigAdapter
|
||||
*/
|
||||
public function delete($cat, $k)
|
||||
{
|
||||
if (!$this->isConnected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->configCache->delete($cat, $k);
|
||||
|
||||
$result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);
|
||||
|
||||
Reference in New Issue
Block a user