From cb7a59711aebc7b9cf30695ccdd71c70468aa504 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 8 Aug 2011 09:16:47 -0500 Subject: [PATCH] refactor the memcached class. --- system/memcached.php | 45 ++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/system/memcached.php b/system/memcached.php index 2361ec8d..e42eaedb 100644 --- a/system/memcached.php +++ b/system/memcached.php @@ -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; } } \ No newline at end of file