refactor the memcached class.
This commit is contained in:
parent
07d6ed5703
commit
cb7a59711a
|
@ -16,29 +16,34 @@ class Memcached {
|
||||||
*/
|
*/
|
||||||
public static function instance()
|
public static function instance()
|
||||||
{
|
{
|
||||||
if (is_null(static::$instance))
|
return ( ! is_null(static::$instance)) ? static::$instance : static::$instance = static::connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connect to the configured Memcached servers.
|
||||||
|
*
|
||||||
|
* @return Memcache
|
||||||
|
*/
|
||||||
|
private static function connect()
|
||||||
|
{
|
||||||
|
if ( ! class_exists('Memcache'))
|
||||||
{
|
{
|
||||||
if ( ! class_exists('Memcache'))
|
throw new \Exception('Attempting to use Memcached, but the Memcached PHP extension is not installed on this server.');
|
||||||
{
|
|
||||||
throw new \Exception('Attempting to use Memcached, but the Memcached PHP extension is not installed on this server.');
|
|
||||||
}
|
|
||||||
|
|
||||||
$memcache = new \Memcache;
|
|
||||||
|
|
||||||
foreach (Config::get('cache.servers') as $server)
|
|
||||||
{
|
|
||||||
$memcache->addServer($server['host'], $server['port'], true, $server['weight']);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($memcache->getVersion() === false)
|
|
||||||
{
|
|
||||||
throw new \Exception('Memcached is configured. However, no connections could be made. Please verify your memcached configuration.');
|
|
||||||
}
|
|
||||||
|
|
||||||
static::$instance = $memcache;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return static::$instance;
|
$memcache = new \Memcache;
|
||||||
|
|
||||||
|
foreach (Config::get('cache.servers') as $server)
|
||||||
|
{
|
||||||
|
$memcache->addServer($server['host'], $server['port'], true, $server['weight']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($memcache->getVersion() === false)
|
||||||
|
{
|
||||||
|
throw new \Exception('Memcached is configured. However, no connections could be made. Please verify your memcached configuration.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $memcache;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue