Files
spa-api/system/memcached.php

50 lines
1.1 KiB
PHP
Raw Normal View History

2011-06-08 23:45:08 -05:00
<?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()
{
return ( ! is_null(static::$instance)) ? static::$instance : static::$instance = static::connect(Config::get('cache.servers'));
2011-08-08 09:16:47 -05:00
}
2011-06-08 23:45:08 -05:00
2011-08-08 09:16:47 -05:00
/**
* Connect to the configured Memcached servers.
*
* @param array $servers
2011-08-08 09:16:47 -05:00
* @return Memcache
*/
private static function connect($servers)
2011-08-08 09:16:47 -05:00
{
if ( ! class_exists('Memcache'))
{
throw new \Exception('Attempting to use Memcached, but the Memcached PHP extension is not installed on this server.');
}
2011-06-08 23:45:08 -05:00
2011-08-08 09:16:47 -05:00
$memcache = new \Memcache;
2011-06-08 23:45:08 -05:00
foreach ($servers as $server)
2011-08-08 09:16:47 -05:00
{
$memcache->addServer($server['host'], $server['port'], true, $server['weight']);
}
2011-06-08 23:45:08 -05:00
2011-08-08 09:16:47 -05:00
if ($memcache->getVersion() === false)
{
throw new \Exception('Memcached is configured. However, no connections could be made. Please verify your memcached configuration.');
2011-06-08 23:45:08 -05:00
}
2011-08-08 09:16:47 -05:00
return $memcache;
2011-06-08 23:45:08 -05:00
}
}