Allow connection to Memcached and Redis via unix socket

This commit is contained in:
Kijin Sung 2017-09-11 17:33:19 +09:00
parent 363f2feeac
commit 1042479d5b
4 changed files with 93 additions and 34 deletions

View file

@ -44,6 +44,12 @@ class Memcached implements \Rhymix\Framework\Drivers\CacheInterface
}
foreach ($config as $url)
{
if (starts_with('/', $url))
{
$this->_conn->addServer($url, 0);
}
else
{
$info = parse_url($url);
if (isset($info['host']) && isset($info['port']))
@ -52,6 +58,7 @@ class Memcached implements \Rhymix\Framework\Drivers\CacheInterface
}
}
}
}
/**
* Create a new instance of the current cache driver, using the given settings.
@ -106,6 +113,12 @@ class Memcached implements \Rhymix\Framework\Drivers\CacheInterface
}
foreach ($config as $url)
{
if (starts_with('/', $url))
{
$conn->addServer($url, 0);
}
else
{
$info = parse_url($url);
if (isset($info['host']) && isset($info['port']))
@ -113,6 +126,7 @@ class Memcached implements \Rhymix\Framework\Drivers\CacheInterface
$conn->addServer($info['host'], $info['port']);
}
}
}
for ($i = 0; $i < 5; $i++)
{

View file

@ -31,6 +31,15 @@ class Redis implements \Rhymix\Framework\Drivers\CacheInterface
{
$this->_conn = null;
foreach ($config as $url)
{
if (preg_match('!^(/.+)(#[0-9]+)?$!', $url, $matches))
{
$this->_conn = new \Redis;
$this->_conn->connect($matches[1]);
$this->_conn->select($matches[2] ? substr($matches[2], 1) : 0);
break;
}
else
{
$info = parse_url($url);
if (isset($info['host']) && isset($info['port']))
@ -41,7 +50,11 @@ class Redis implements \Rhymix\Framework\Drivers\CacheInterface
{
$this->_conn->auth(isset($info['user']) ? $info['user'] : $info['pass']);
}
if(isset($info['path']) && $dbnum = intval(substr($info['path'], 1)))
if(isset($info['fragment']) && $dbnum = intval($info['fragment']))
{
$this->_conn->select($dbnum);
}
elseif(isset($info['path']) && $dbnum = intval(substr($info['path'], 1)))
{
$this->_conn->select($dbnum);
}
@ -49,6 +62,7 @@ class Redis implements \Rhymix\Framework\Drivers\CacheInterface
}
}
}
}
catch (\RedisException $e)
{
$this->_conn = null;
@ -96,6 +110,15 @@ class Redis implements \Rhymix\Framework\Drivers\CacheInterface
{
$conn = new \Redis;
foreach ($config as $url)
{
if (preg_match('!^(/.+)(#[0-9]+)?$!', $url, $matches))
{
$conn = new \Redis;
$conn->connect($matches[1]);
$conn->select($matches[2] ? substr($matches[2], 1) : 0);
return true;
}
else
{
$info = parse_url($url);
if (isset($info['host']) && isset($info['port']))
@ -105,13 +128,18 @@ class Redis implements \Rhymix\Framework\Drivers\CacheInterface
{
$conn->auth(isset($info['user']) ? $info['user'] : $info['pass']);
}
if(isset($info['path']) && $dbnum = intval(substr($info['path'], 1)))
if(isset($info['fragment']) && $dbnum = intval($info['fragment']))
{
$conn->select($dbnum);
}
elseif(isset($info['path']) && $dbnum = intval(substr($info['path'], 1)))
{
$conn->select($dbnum);
}
return true;
}
}
}
return false;
}
catch (\RedisException $e)

View file

@ -724,11 +724,19 @@ class adminAdminController extends admin
if ($vars->object_cache_type)
{
if ($vars->object_cache_type === 'memcached' || $vars->object_cache_type === 'redis')
{
if (starts_with('/', $vars->object_cache_host))
{
$cache_servers = array($vars->object_cache_host);
}
else
{
$cache_servers = array($vars->object_cache_type . '://' . $vars->object_cache_host . ':' . intval($vars->object_cache_port));
}
if ($vars->object_cache_type === 'redis')
{
$cache_servers[0] .= '/' . intval($vars->object_cache_dbnum);
$cache_servers[0] .= '#' . intval($vars->object_cache_dbnum);
}
}
else

View file

@ -518,12 +518,21 @@ class adminAdminView extends admin
Context::set('cache_default_ttl', $cache_default_ttl);
if ($cache_servers)
{
if (preg_match('!^(/.+)(#[0-9]+)?$!', array_first($cache_servers), $matches))
{
Context::set('object_cache_host', $matches[1]);
Context::set('object_cache_port', 0);
Context::set('object_cache_dbnum', $matches[2] ? substr($matches[2], 1) : 0);
}
else
{
Context::set('object_cache_host', parse_url(array_first($cache_servers), PHP_URL_HOST) ?: null);
Context::set('object_cache_port', parse_url(array_first($cache_servers), PHP_URL_PORT) ?: null);
$cache_dbnum = preg_replace('/[^\d]/', '', parse_url(array_first($cache_servers), PHP_URL_PATH));
$cache_dbnum = preg_replace('/[^\d]/', '', parse_url(array_first($cache_servers), PHP_URL_FRAGMENT) ?: parse_url(array_first($cache_servers), PHP_URL_PATH));
Context::set('object_cache_dbnum', $cache_dbnum === '' ? 1 : intval($cache_dbnum));
}
}
else
{
Context::set('object_cache_host', null);