Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
revert(Config): Add back Dynamic Config provider by Redis
Browse files Browse the repository at this point in the history
It is necessary when our project run at php-fpm model to set our dynamic
config in redis by not in swoole_table
  • Loading branch information
Rhilip committed Feb 6, 2019
1 parent a24638b commit 043ad5c
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 16 deletions.
2 changes: 1 addition & 1 deletion apps/config/http_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
],

'config' => [
'class' => Rid\Config\Config::class,
'class' => Rid\Config\ConfigByRedis::class,
],

'swiftmailer' => [
Expand Down
5 changes: 4 additions & 1 deletion apps/config/http_coroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,15 @@
// 连接池
'redis.connectionPool' => [
// 类路径
'class' => mix\pool\ConnectionPool::class,
'class' => Rid\pool\ConnectionPool::class,
// 最小连接数
'min' => 5,
// 最大连接数
'max' => 50,
],

'config' => [
'class' => Rid\Config\ConfigBySwoole::class,
]
],
]);
4 changes: 4 additions & 0 deletions apps/config/http_permanent.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@
'redis' => [
'class' => Rid\Redis\Persistent\RedisConnection::class,
],

'config' => [
'class' => Rid\Config\ConfigBySwoole::class,
]
],
]);
2 changes: 1 addition & 1 deletion framework/Base/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @property \Rid\Http\Cookie $cookie
* @property \Rid\Database\PDOConnection $pdo
* @property \Rid\Redis\RedisConnection $redis
* @property \Rid\Config\Config $config
* @property \Rid\Config\ConfigBySwoole|\Rid\Config\ConfigByRedis $config
* @property \Rid\Mailer\Mailer $swiftmailer
* @property \Rid\Pool\ConnectionPool $connectionPool
* @property \Rid\User\User $user
Expand Down
85 changes: 85 additions & 0 deletions framework/Config/ConfigByRedis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Created by PhpStorm.
* User: Rhilip
* Date: 2019/2/6
* Time: 20:25
*/

namespace Rid\Config;

use Rid\Base\Component;
use Rid\Exceptions\ConfigException;

class ConfigByRedis extends Component implements DynamicConfigInterface
{

/** Config key prefix in Cache
* @var string
*/
public $cacheField = "CONFIG:site_config";

public $cacheExpire = 86400;

public function __construct(array $config = [])
{
parent::__construct($config);

if (!app()->redis->exists($this->cacheField)) {
$configs_pdo = app()->pdo->createCommand("SELECT `name`,`value` FROM `site_config`")->queryAll();
$configs = array_column($configs_pdo, 'value', 'name');
app()->redis->hMset($this->cacheField, $configs);
app()->redis->expire($this->cacheField, $this->cacheExpire);
}
}

public function get(string $name, bool $throw = true)
{
// First Check config stored in RedisConnection Cache, If it exist , then just return the cached key
$setting = app()->redis->hget($this->cacheField, $name);
if (!is_null($setting)) return $setting;
// Get config From Database
$setting = app()->pdo->createCommand("SELECT `value` from `site_config` WHERE `name` = :name")
->bindParams(["name" => $name])->queryScalar();

// In this case (Load config From Database Failed) , A Exception should throw
if ($setting === false)
throw new ConfigException(sprintf("Dynamic Setting \"%s\" couldn't be found.", $name));

// Cache it in RedisConnection and return
app()->redis->hset($this->cacheField, $name, $setting);
return $setting;
}

public function getAll()
{
return app()->redis->hgetall($this->cacheField);
}

public function getSection($prefix = null)
{
return array_filter($this->getAll(), function ($k) use ($prefix) {
return strpos($k, $prefix) === 0;
}, ARRAY_FILTER_USE_KEY);
}

public function set(string $name, $value)
{
app()->pdo->createCommand("UPDATE `site_config` SET `value` = :val WHERE `name` = :name")->bindParams([
"val" => $value, "name" => $name
])->execute();
return $this->flush($name);
}

public function setMultiple(array $config_array)
{
foreach ($config_array as $key => $value)
$this->set($key, $value);
}

public function flush($name)
{
app()->redis->hdel($this->cacheField, $name);
return $this->get($name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Rid\Base\Component;
use Rid\Exceptions\ConfigException;

class Config extends Component
class ConfigBySwoole extends Component implements DynamicConfigInterface
{
/** @var \swoole_table */
private $cacheTable;
Expand All @@ -32,7 +32,7 @@ public function __construct(array $config = [])
}
}

public function get(string $name, $throw = true)
public function get(string $name,bool $throw = true)
{
$setting = $this->cacheTable->get($name, $this->valueField);
// First Check config stored in RedisConnection Cache, If it exist , then just return the cached key
Expand All @@ -41,7 +41,8 @@ public function get(string $name, $throw = true)
$setting = app()->pdo->createCommand("SELECT `value` from `site_config` WHERE `name` = :name")
->bindParams(["name" => $name])->queryScalar();
// In this case (Load config From Database Failed) , A Exception should throw
if ($setting === false && $throw) throw $this->createNotFoundException($name);
if ($setting === false && $throw)
throw new ConfigException(sprintf("Dynamic Setting \"%s\" couldn't be found.", $name));

$this->cacheTable->set($name, [$this->valueField => $setting]);
}
Expand Down Expand Up @@ -83,14 +84,4 @@ public function setMultiple(array $config_array)
foreach ($config_array as $key => $value)
$this->set($key, $value);
}

/**
* @param string $name Name of the setting.
* @return ConfigException
*/
protected function createNotFoundException($name)
{
return new ConfigException(sprintf("Dynamic Setting \"%s\" couldn't be found.", $name));
}

}
49 changes: 49 additions & 0 deletions framework/Config/DynamicConfigInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Created by PhpStorm.
* User: Rhilip
* Date: 2019/2/6
* Time: 20:18
*/

namespace Rid\Config;


interface DynamicConfigInterface
{
/**
* @param string $name
* @param bool $throw
* @return mixed
*/
public function get(string $name, bool $throw = true);

/**
* @return array
*/
public function getAll();

/**
* @param null $prefix
* @return array
*/
public function getSection($prefix = null);

/**
* @param string $name
* @param $value
* @return mixed
*/
public function set(string $name, $value);

/**
* @param array $config_array
*/
public function setMultiple(array $config_array);

/**
* @param $name
* @return mixed
*/
public function flush($name);
}

0 comments on commit 043ad5c

Please sign in to comment.