Make all cache drivers singleton instances

This commit is contained in:
Kijin Sung 2016-04-24 16:30:20 +09:00
parent 9b71df6a01
commit 2227e0d278
10 changed files with 164 additions and 40 deletions

View file

@ -12,6 +12,11 @@ class Memcached implements \Rhymix\Framework\Drivers\CacheInterface
*/
public $prefix = true;
/**
* The singleton instance is stored here.
*/
protected static $_instance = null;
/**
* The Memcached connection is stored here.
*/
@ -19,12 +24,9 @@ class Memcached implements \Rhymix\Framework\Drivers\CacheInterface
protected $_ext = null;
/**
* Create a new instance of the current cache driver, using the given settings.
*
* @param array $config
* @return void
* Direct invocation of the constructor is not permitted.
*/
public function __construct(array $config)
protected function __construct(array $config)
{
if (class_exists('\\Memcached', false))
{
@ -51,6 +53,21 @@ class Memcached implements \Rhymix\Framework\Drivers\CacheInterface
}
}
/**
* Create a new instance of the current cache driver, using the given settings.
*
* @param array $config
* @return void
*/
public static function getInstance(array $config)
{
if (self::$_instance === null)
{
self::$_instance = new self($config);
}
return self::$_instance;
}
/**
* Check if the current cache driver is supported on this server.
*
@ -58,7 +75,7 @@ class Memcached implements \Rhymix\Framework\Drivers\CacheInterface
*
* @return bool
*/
public function isSupported()
public static function isSupported()
{
return class_exists('\\Memcached', false) || class_exists('\\Memcache', false);
}