Skip to content

Commit

Permalink
支持redis断开重连
Browse files Browse the repository at this point in the history
  • Loading branch information
JanHuang committed Jun 15, 2018
1 parent 40b6e4f commit bf98a66
Showing 1 changed file with 58 additions and 22 deletions.
80 changes: 58 additions & 22 deletions src/Pool/CachePool.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
* @copyright 2016
*
* @see https://www.github.com/janhuang
* @see https://fastdlabs.com
* @see http://www.fast-d.cn/
*/

namespace FastD\Pool;

use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\RedisAdapter;

/**
Expand All @@ -27,6 +28,11 @@ class CachePool implements PoolInterface
*/
protected $config;

/**
* @var array
*/
protected $redises = [];

/**
* Cache constructor.
*
Expand All @@ -39,32 +45,62 @@ public function __construct(array $config)

/**
* @param $key
* @param $force
*
* @return AbstractAdapter
* @return FilesystemAdapter|RedisAdapter
*/
public function getCache($key, $force = false)
protected function connect($key)
{
if ($force || !isset($this->caches[$key])) {
if (!isset($this->config[$key])) {
throw new \LogicException(sprintf('No set %s cache', $key));
}
$config = $this->config[$key];
switch ($config['adapter']) {
case RedisAdapter::class:
$this->caches[$key] = new RedisAdapter(
RedisAdapter::createConnection($config['params']['dsn']),
if (!isset($this->config[$key])) {
throw new \LogicException(sprintf('No set %s cache', $key));
}
$config = $this->config[$key];
switch ($config['adapter']) {
case RedisAdapter::class:
$connect = null;
try {
$connect = RedisAdapter::createConnection($config['params']['dsn']);
$cache = new RedisAdapter(
$connect,
isset($config['params']['namespace']) ? $config['params']['namespace'] : '',
isset($config['params']['lifetime']) ? $config['params']['lifetime'] : ''
);
} catch (\Exception $e) {
$cache = new FilesystemAdapter('', 0, '/tmp/cache');
}

break;
default:
$this->caches[$key] = new $config['adapter'](
isset($config['params']['namespace']) ? $config['params']['namespace'] : '',
isset($config['params']['lifetime']) ? $config['params']['lifetime'] : '',
isset($config['params']['directory']) ? $config['params']['directory'] : app()->getPath().'/runtime/cache'
);
$this->redises[$key] = [
'connect' => $connect,
'driver' => RedisAdapter::class,
];

break;
default:
$cache = new $config['adapter'](
isset($config['params']['namespace']) ? $config['params']['namespace'] : '',
isset($config['params']['lifetime']) ? $config['params']['lifetime'] : '',
isset($config['params']['directory']) ? $config['params']['directory'] : app()->getPath().'/runtime/cache'
);
}

return $cache;
}

/**
* @param $key
*
* @return AbstractAdapter
*/
public function getCache($key)
{
if (!isset($this->caches[$key])) {
$this->caches[$key] = $this->connect($key);
}

if (isset($this->redises[$key])) {
if (
null === $this->redises[$key]['connect']
|| false === $this->redises[$key]['connect']->ping()
) {
$this->caches[$key] = $this->connect($key);
}
}

Expand All @@ -77,7 +113,7 @@ public function getCache($key, $force = false)
public function initPool()
{
foreach ($this->config as $name => $config) {
$this->getCache($name, true);
$this->getCache($name);
}
}
}

0 comments on commit bf98a66

Please sign in to comment.