diff --git a/apps/config/http_base.php b/apps/config/http_base.php index d18c38e..79c6fb3 100644 --- a/apps/config/http_base.php +++ b/apps/config/http_base.php @@ -202,7 +202,7 @@ ], 'config' => [ - 'class' => Rid\Config\Config::class, + 'class' => Rid\Config\ConfigByRedis::class, ], 'swiftmailer' => [ diff --git a/apps/config/http_coroutine.php b/apps/config/http_coroutine.php index 6ee2179..23ee594 100644 --- a/apps/config/http_coroutine.php +++ b/apps/config/http_coroutine.php @@ -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, + ] ], ]); diff --git a/apps/config/http_permanent.php b/apps/config/http_permanent.php index 824171b..ad6176a 100644 --- a/apps/config/http_permanent.php +++ b/apps/config/http_permanent.php @@ -16,5 +16,9 @@ 'redis' => [ 'class' => Rid\Redis\Persistent\RedisConnection::class, ], + + 'config' => [ + 'class' => Rid\Config\ConfigBySwoole::class, + ] ], ]); diff --git a/framework/Base/Application.php b/framework/Base/Application.php index 7b43a3b..c6552d6 100644 --- a/framework/Base/Application.php +++ b/framework/Base/Application.php @@ -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 diff --git a/framework/Config/ConfigByRedis.php b/framework/Config/ConfigByRedis.php new file mode 100644 index 0000000..3695efc --- /dev/null +++ b/framework/Config/ConfigByRedis.php @@ -0,0 +1,85 @@ +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); + } +} diff --git a/framework/Config/Config.php b/framework/Config/ConfigBySwoole.php similarity index 84% rename from framework/Config/Config.php rename to framework/Config/ConfigBySwoole.php index 597af57..531e492 100644 --- a/framework/Config/Config.php +++ b/framework/Config/ConfigBySwoole.php @@ -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; @@ -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 @@ -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]); } @@ -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)); - } - } diff --git a/framework/Config/DynamicConfigInterface.php b/framework/Config/DynamicConfigInterface.php new file mode 100644 index 0000000..91d17aa --- /dev/null +++ b/framework/Config/DynamicConfigInterface.php @@ -0,0 +1,49 @@ +