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