memcached.php 902 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
<?php namespace System;

class Memcached {

	/**
	 * The Memcache instance.
	 *
	 * @var Memcache
	 */
	private static $instance = null;

	/**
	 * Get the singleton Memcache instance.
	 *
	 * @return Memcache
	 */
	public static function instance()
	{
		if (is_null(static::$instance))
		{
			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;
		}

		return static::$instance;
	}

}