Commit cb7a5971 authored by Taylor Otwell's avatar Taylor Otwell

refactor the memcached class.

parent 07d6ed57
...@@ -16,29 +16,34 @@ class Memcached { ...@@ -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();
{ }
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; /**
* Connect to the configured Memcached servers.
*
* @return Memcache
*/
private static function connect()
{
if ( ! class_exists('Memcache'))
{
throw new \Exception('Attempting to use Memcached, but the Memcached PHP extension is not installed on this server.');
}
foreach (Config::get('cache.servers') as $server) $memcache = new \Memcache;
{
$memcache->addServer($server['host'], $server['port'], true, $server['weight']);
}
if ($memcache->getVersion() === false) foreach (Config::get('cache.servers') as $server)
{ {
throw new \Exception('Memcached is configured. However, no connections could be made. Please verify your memcached configuration.'); $memcache->addServer($server['host'], $server['port'], true, $server['weight']);
} }
static::$instance = $memcache; if ($memcache->getVersion() === false)
{
throw new \Exception('Memcached is configured. However, no connections could be made. Please verify your memcached configuration.');
} }
return static::$instance; return $memcache;
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment