Files
kiri-core/System/Pool/Redis.php
T

193 lines
4.5 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
namespace Snowflake\Pool;
use HttpServer\Http\Context;
2020-09-03 15:23:11 +08:00
use Redis as SRedis;
2020-08-31 01:27:08 +08:00
use RedisException;
2020-09-03 18:05:54 +08:00
use Snowflake\Exception\RedisConnectException;
2020-08-31 01:27:08 +08:00
use Swoole\Coroutine;
use Exception;
/**
* Class RedisClient
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\Pool
2020-08-31 01:27:08 +08:00
*/
2020-09-03 15:23:11 +08:00
class Redis extends Pool
2020-08-31 01:27:08 +08:00
{
/**
* @param $value
*/
public function setLength($value)
{
$this->max = $value;
}
/**
* @param array $config
* @param bool $isMaster
* @return mixed|null
* @throws Exception
*/
public function getConnection(array $config, $isMaster = false)
{
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
[$coroutineId, $coroutineName] = $this->getIndex('redis:' . $name, $isMaster);
if (Context::hasContext($coroutineName)) {
return Context::getContext($coroutineName);
} else if (!$this->hasItem($coroutineName)) {
2020-09-04 19:00:27 +08:00
$this->success('create redis client -> ' . $config['host'] . ':' . $this->size($coroutineName));
2020-08-31 01:27:08 +08:00
return $this->saveClient($coroutineName, $this->createConnect($config));
}
return $this->getByChannel($coroutineName, $config);
}
/**
* @param $coroutineName
* @param $config
* @return mixed
* @throws Exception
*/
public function getByChannel($coroutineName, $config)
{
2020-09-04 19:00:27 +08:00
$this->info('redis client has :' . $this->size($coroutineName));
2020-08-31 01:27:08 +08:00
if (!$this->hasItem($coroutineName)) {
2020-09-04 19:00:27 +08:00
$this->success('create redis client -> ' . $config['host'] . ':' . $this->size($coroutineName));
2020-08-31 01:27:08 +08:00
return $this->saveClient($coroutineName, $this->createConnect($config));
}
2020-09-05 01:29:46 +08:00
[$time, $client] = $this->get($coroutineName);
2020-08-31 01:27:08 +08:00
if ($client === null) {
return $this->getByChannel($coroutineName, $config);
}
return $this->saveClient($coroutineName, $client);
}
/**
* @param $coroutineName
* @param $client
* @return mixed
* @throws Exception
*/
private function saveClient($coroutineName, $client)
{
return Context::setContext($coroutineName, $client);
}
/**
* @param array $config
2020-09-03 15:23:11 +08:00
* @return SRedis
2020-08-31 01:27:08 +08:00
* @throws Exception
*/
private function createConnect(array $config)
{
2020-09-03 15:23:11 +08:00
$redis = new SRedis();
2020-08-31 01:27:08 +08:00
if (!$redis->connect($config['host'], $config['port'], $config['timeout'])) {
2020-09-03 18:05:54 +08:00
throw new RedisConnectException(sprintf('The Redis Connect %s::%d Fail.', $config['host'], $config['port']));
2020-08-31 01:27:08 +08:00
}
if (empty($config['auth']) || !$redis->auth($config['auth'])) {
2020-09-03 18:05:54 +08:00
throw new RedisConnectException(sprintf('Redis Error: %s, Host %s, Auth %s', $redis->getLastError(), $config['host'], $config['auth']));
2020-08-31 01:27:08 +08:00
}
if (!isset($config['read_timeout'])) {
$config['read_timeout'] = 10;
}
$redis->select($config['databases']);
2020-09-03 15:23:11 +08:00
$redis->setOption(SRedis::OPT_READ_TIMEOUT, -1);
$redis->setOption(SRedis::OPT_PREFIX, $config['prefix']);
2020-08-31 01:27:08 +08:00
return $redis;
}
/**
* @param array $config
* @param bool $isMaster
*/
public function release(array $config, $isMaster = false)
{
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
[$coroutineId, $coroutineName] = $this->getIndex('redis:' . $name, $isMaster);
if (!Context::hasContext($coroutineName)) {
return;
}
$client = Context::getContext($coroutineName);
$this->push($coroutineName, $client);
$this->remove($coroutineName);
}
/**
* @param array $config
* @param bool $isMaster
*/
public function destroy(array $config, $isMaster = false)
{
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
[$coroutineId, $coroutineName] = $this->getIndex('redis:' . $name, $isMaster);
if (!Context::hasContext($coroutineName)) {
return;
}
$this->remove($coroutineName);
$this->clean($coroutineName);
}
/**
* @param $coroutineName
*/
public function remove($coroutineName)
{
Context::deleteId($coroutineName);
}
/**
2020-09-04 19:10:08 +08:00
* @param $name
2020-08-31 01:27:08 +08:00
* @param $time
* @param $client
* @return bool|mixed
2020-09-04 19:10:08 +08:00
* @throws Exception
2020-08-31 01:27:08 +08:00
*/
2020-09-04 19:10:08 +08:00
public function checkCanUse($name, $time, $client)
2020-08-31 01:27:08 +08:00
{
2020-09-04 19:10:08 +08:00
try {
if ($time + 60 * 10 < time()) {
return $result = false;
}
if (!($client instanceof SRedis)) {
return $result = false;
}
if (!$client->isConnected() || !$client->ping('connect.')) {
return $result = false;
}
return $result = true;
} catch (Exception $exception) {
return $result = false;
} finally {
if (!$result) {
$this->desc($name);
}
2020-08-31 01:27:08 +08:00
}
}
public function desc($name)
{
// TODO: Implement desc() method.
}
/**
* @param $name
* @param false $isMaster
* @return array
*/
private function getIndex($name, $isMaster = false)
{
return [Coroutine::getCid(), $this->name($name, $isMaster)];
}
}