refactor the memcached class.

This commit is contained in:
Taylor Otwell 2011-08-08 09:16:47 -05:00
parent 07d6ed5703
commit cb7a59711a
1 changed files with 25 additions and 20 deletions

View File

@ -16,29 +16,34 @@ class Memcached {
*/
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.');
}
$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;
throw new \Exception('Attempting to use Memcached, but the Memcached PHP extension is not installed on this server.');
}
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;
}
}