This commit is contained in:
2020-09-03 15:23:11 +08:00
parent 5a038fb0b6
commit 5b4a1a5cd0
5 changed files with 86 additions and 42 deletions
+4 -2
View File
@@ -15,6 +15,7 @@ use HttpServer\Http\Response;
use HttpServer\Route\Router; use HttpServer\Route\Router;
use HttpServer\Server; use HttpServer\Server;
use Snowflake\Annotation\Annotation; use Snowflake\Annotation\Annotation;
use Snowflake\Cache\Memcached;
use Snowflake\Cache\Redis; use Snowflake\Cache\Redis;
use Snowflake\Config; use Snowflake\Config;
use Snowflake\Di\Service; use Snowflake\Di\Service;
@@ -24,7 +25,7 @@ use Snowflake\Exception\ComponentException;
use Snowflake\Exception\InitException; use Snowflake\Exception\InitException;
use Snowflake\Jwt\Jwt; use Snowflake\Jwt\Jwt;
use Snowflake\Pool\Connection; use Snowflake\Pool\Connection;
use Snowflake\Pool\RedisClient; use Snowflake\Pool\Redis as SRedis;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Snowflake\Event; use Snowflake\Event;
use Snowflake\Pool\Pool as SPool; use Snowflake\Pool\Pool as SPool;
@@ -41,6 +42,7 @@ use Database\DatabasesProviders;
* @property Server $server * @property Server $server
* @property DatabasesProviders $db * @property DatabasesProviders $db
* @property Connection $connections * @property Connection $connections
* @property Memcached $memcached
* @property Logger $logger * @property Logger $logger
* @property Jwt $jwt * @property Jwt $jwt
*/ */
@@ -335,7 +337,7 @@ abstract class BaseApplication extends Service
'event' => ['class' => Event::class], 'event' => ['class' => Event::class],
'annotation' => ['class' => Annotation::class], 'annotation' => ['class' => Annotation::class],
'connections' => ['class' => Connection::class], 'connections' => ['class' => Connection::class],
'redis_connections' => ['class' => RedisClient::class], 'redis_connections' => ['class' => SRedis::class],
'pool' => ['class' => SPool::class], 'pool' => ['class' => SPool::class],
'response' => ['class' => Response::class], 'response' => ['class' => Response::class],
'request' => ['class' => Request::class], 'request' => ['class' => Request::class],
+20 -26
View File
@@ -22,7 +22,16 @@ class Config extends Component
const ERROR_MESSAGE = 'The not find :key in app configs.'; const ERROR_MESSAGE = 'The not find :key in app configs.';
public $data; private $data;
/**
* @return mixed
*/
public function getData()
{
return $this->data;
}
/** /**
* @param $key * @param $key
@@ -33,37 +42,22 @@ class Config extends Component
*/ */
public static function get($key, $try = FALSE, $default = null) public static function get($key, $try = FALSE, $default = null)
{ {
$config = Snowflake::app()->config; $explode = explode('.', $key);
if (strpos($key, '.') === false) { $instance = Snowflake::app()->getConfig()->getData();
if (isset($config->data[$key])) { foreach ($explode as $index => $value) {
return $config->data[$key]; if (!isset($instance[$value]) && $try) {
} else if ($default !== null) {
return $default;
} else if ($try) {
throw new ConfigException(str_replace(':key', $key, self::ERROR_MESSAGE)); throw new ConfigException(str_replace(':key', $key, self::ERROR_MESSAGE));
} }
return NULL; $instance = $instance[$value];
if (!is_array($instance) && $index + 1 < count($explode)) {
throw new ConfigException(str_replace(':key', $key, self::ERROR_MESSAGE));
} }
$keys = explode('.', $key); }
if (empty($instance)) {
$parent = $config->data[array_shift($keys)] ?? null;
if (empty($parent)) {
return $default; return $default;
} }
if (empty($keys) || !is_array($parent)) { return $instance;
return $parent;
}
foreach ($keys as $key) {
if (!isset($parent[$key])) {
return $parent;
}
$parent = $parent[$key];
if (empty($keys) || !is_array($parent)) {
return $parent;
}
}
return $parent;
} }
/** /**
+43 -6
View File
@@ -5,6 +5,10 @@ namespace Snowflake\Cache;
use Exception; use Exception;
use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Component;
use Snowflake\Config;
use Snowflake\Event;
use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake;
/** /**
* Class Memcached * Class Memcached
@@ -22,17 +26,50 @@ class Memcached extends Component implements ICache
public $timeout = 60; public $timeout = 60;
/** /**
* @throws Exception * @throws Exception
*/ */
public function init() public function init()
{ {
$this->_memcached = new \Memcached(); $event = Snowflake::app()->event;
$isConnected = $this->_memcached->addServer( $event->on(Event::RELEASE_ALL, [$this, 'destroy']);
env('cache.memcached.host', $this->host), $event->on(Event::EVENT_AFTER_REQUEST, [$this, 'release']);
env('cache.memcached.port', $this->port),
env('cache.memcached.timeout', $this->timeout) $id = Config::get('id', false, 'system');
); $this->_memcached = new \Memcached($id);
$this->addServer();
}
/**
* @return \Memcached
* @throws Exception
*/
public function getConnect()
{
return $this->_memcached;
}
/**
* @throws ConfigException
* @throws Exception
*/
private function addServer()
{
$array = [];
$memcached = Config::get('cache.memcached');
if (isset($memcached[0])) {
foreach ($memcached as $value) {
$array[] = [$value['host'], $value['port'], $value['weight']];
}
$isConnected = $this->_memcached->addServers($array);
} else {
$array[] = Config::get('cache.memcached.host', true);
$array[] = Config::get('cache.memcached.port', true);
$array[] = Config::get('cache.memcached.weight', true);
$isConnected = $this->_memcached->addServer(...$array);
}
if (!$isConnected) { if (!$isConnected) {
throw new Exception('Cache Memcache Host 127.0.0.1 Connect Fail.'); throw new Exception('Cache Memcache Host 127.0.0.1 Connect Fail.');
} }
+12 -1
View File
@@ -4,6 +4,7 @@
namespace Snowflake\Pool; namespace Snowflake\Pool;
use Snowflake\Cache\Memcached;
use Snowflake\Snowflake; use Snowflake\Snowflake;
/** /**
@@ -11,12 +12,13 @@ use Snowflake\Snowflake;
* @package Snowflake\Pool * @package Snowflake\Pool
* @property $redis * @property $redis
* @property $db * @property $db
* @property $memcached
*/ */
class Pool extends \Snowflake\Abstracts\Pool class Pool extends \Snowflake\Abstracts\Pool
{ {
/** /**
* @return RedisClient * @return Redis
*/ */
public function getRedis() public function getRedis()
{ {
@@ -31,4 +33,13 @@ class Pool extends \Snowflake\Abstracts\Pool
return Snowflake::app()->connections; return Snowflake::app()->connections;
} }
/**
* @return Memcached
*/
public function getMemcached()
{
return Snowflake::app()->memcached;
}
} }
@@ -5,7 +5,7 @@ namespace Snowflake\Pool;
use HttpServer\Http\Context; use HttpServer\Http\Context;
use Redis; use Redis as SRedis;
use RedisException; use RedisException;
use Swoole\Coroutine; use Swoole\Coroutine;
use Exception; use Exception;
@@ -14,7 +14,7 @@ use Exception;
* Class RedisClient * Class RedisClient
* @package Snowflake\Snowflake\Pool * @package Snowflake\Snowflake\Pool
*/ */
class RedisClient extends Pool class Redis extends Pool
{ {
/** /**
@@ -81,12 +81,12 @@ class RedisClient extends Pool
/** /**
* @param array $config * @param array $config
* @return Redis * @return SRedis
* @throws Exception * @throws Exception
*/ */
private function createConnect(array $config) private function createConnect(array $config)
{ {
$redis = new Redis(); $redis = new SRedis();
if (!$redis->connect($config['host'], $config['port'], $config['timeout'])) { if (!$redis->connect($config['host'], $config['port'], $config['timeout'])) {
throw new Exception('The Redis Connect Fail.'); throw new Exception('The Redis Connect Fail.');
} }
@@ -97,8 +97,8 @@ class RedisClient extends Pool
$config['read_timeout'] = 10; $config['read_timeout'] = 10;
} }
$redis->select($config['databases']); $redis->select($config['databases']);
$redis->setOption(Redis::OPT_READ_TIMEOUT, -1); $redis->setOption(SRedis::OPT_READ_TIMEOUT, -1);
$redis->setOption(Redis::OPT_PREFIX, $config['prefix']); $redis->setOption(SRedis::OPT_PREFIX, $config['prefix']);
return $redis; return $redis;
} }
@@ -154,7 +154,7 @@ class RedisClient extends Pool
if ($time + 60 * 10 < time()) { if ($time + 60 * 10 < time()) {
return false; return false;
} }
if (!($client instanceof Redis)) { if (!($client instanceof SRedis)) {
return false; return false;
} }
if (!$client->isConnected() || !$client->ping('connect.')) { if (!$client->isConnected() || !$client->ping('connect.')) {