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

@ -64,12 +64,12 @@ class Cache
if ($class_name && class_exists($class_name) && $class_name::isSupported()) if ($class_name && class_exists($class_name) && $class_name::isSupported())
{ {
self::$_driver = new $class_name($config); self::$_driver = $class_name::getInstance($config);
self::$_driver_name = strtolower($driver_name); self::$_driver_name = strtolower($driver_name);
} }
else else
{ {
self::$_driver = new Drivers\Cache\Dummy(array()); self::$_driver = Drivers\Cache\Dummy::getInstance(array());
self::$_driver_name = 'dummy'; self::$_driver_name = 'dummy';
} }
@ -133,7 +133,7 @@ class Cache
$class_name = '\\Rhymix\\Framework\\Drivers\\Cache\\' . $name; $class_name = '\\Rhymix\\Framework\\Drivers\\Cache\\' . $name;
if (class_exists($class_name) && $class_name::isSupported() && $class_name::validateSettings($config)) if (class_exists($class_name) && $class_name::isSupported() && $class_name::validateSettings($config))
{ {
return new $class_name($config); return $class_name::getInstance($config);
} }
else else
{ {

View file

@ -12,15 +12,32 @@ class APC implements \Rhymix\Framework\Drivers\CacheInterface
*/ */
public $prefix = true; public $prefix = true;
/**
* The singleton instance is stored here.
*/
protected static $_instance = null;
/**
* Direct invocation of the constructor is not permitted.
*/
protected function __construct()
{
}
/** /**
* Create a new instance of the current cache driver, using the given settings. * Create a new instance of the current cache driver, using the given settings.
* *
* @param array $config * @param array $config
* @return void * @return void
*/ */
public function __construct(array $config) public static function getInstance(array $config)
{ {
if (self::$_instance === null)
{
self::$_instance = new self();
}
return self::$_instance;
} }
/** /**
@ -30,7 +47,7 @@ class APC implements \Rhymix\Framework\Drivers\CacheInterface
* *
* @return bool * @return bool
*/ */
public function isSupported() public static function isSupported()
{ {
return function_exists('apc_exists'); return function_exists('apc_exists');
} }

View file

@ -10,7 +10,12 @@ class Dummy extends File implements \Rhymix\Framework\Drivers\CacheInterface
/** /**
* Set this flag to false to disable cache prefixes. * Set this flag to false to disable cache prefixes.
*/ */
public $prefix = true; public $prefix = false;
/**
* The singleton instance is stored here.
*/
protected static $_instance = null;
/** /**
* Dummy data is stored here. * Dummy data is stored here.

View file

@ -14,18 +14,20 @@ class File implements \Rhymix\Framework\Drivers\CacheInterface
*/ */
public $prefix = false; public $prefix = false;
/**
* The singleton instance is stored here.
*/
protected static $_instance = null;
/** /**
* The cache directory. * The cache directory.
*/ */
protected $_dir; protected $_dir;
/** /**
* Create a new instance of the current cache driver, using the given settings. * Direct invocation of the constructor is not permitted.
*
* @param array $config
* @return void
*/ */
public function __construct(array $config) protected function __construct(array $config)
{ {
$this->_dir = \RX_BASEDIR . 'files/cache/store'; $this->_dir = \RX_BASEDIR . 'files/cache/store';
if (!Storage::isDirectory($this->_dir)) if (!Storage::isDirectory($this->_dir))
@ -34,6 +36,21 @@ class File 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 (static::$_instance === null)
{
static::$_instance = new static($config);
}
return static::$_instance;
}
/** /**
* Check if the current cache driver is supported on this server. * Check if the current cache driver is supported on this server.
* *
@ -41,7 +58,7 @@ class File implements \Rhymix\Framework\Drivers\CacheInterface
* *
* @return bool * @return bool
*/ */
public function isSupported() public static function isSupported()
{ {
return true; return true;
} }

View file

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

View file

@ -12,18 +12,20 @@ class Redis implements \Rhymix\Framework\Drivers\CacheInterface
*/ */
public $prefix = true; public $prefix = true;
/**
* The singleton instance is stored here.
*/
protected static $_instance = null;
/** /**
* The Redis connection is stored here. * The Redis connection is stored here.
*/ */
protected $_conn = null; protected $_conn = null;
/** /**
* Create a new instance of the current cache driver, using the given settings. * Direct invocation of the constructor is not permitted.
*
* @param array $config
* @return void
*/ */
public function __construct(array $config) protected function __construct(array $config)
{ {
try try
{ {
@ -53,6 +55,21 @@ class Redis 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. * Check if the current cache driver is supported on this server.
* *
@ -60,7 +77,7 @@ class Redis implements \Rhymix\Framework\Drivers\CacheInterface
* *
* @return bool * @return bool
*/ */
public function isSupported() public static function isSupported()
{ {
return class_exists('\\Redis', false); return class_exists('\\Redis', false);
} }

View file

@ -14,6 +14,11 @@ class SQLite implements \Rhymix\Framework\Drivers\CacheInterface
*/ */
public $prefix = false; public $prefix = false;
/**
* The singleton instance is stored here.
*/
protected static $_instance = null;
/** /**
* The database handle and prepared statements are stored here. * The database handle and prepared statements are stored here.
*/ */
@ -21,12 +26,9 @@ class SQLite implements \Rhymix\Framework\Drivers\CacheInterface
protected $_ps = array(); protected $_ps = array();
/** /**
* Create a new instance of the current cache driver, using the given settings. * Direct invocation of the constructor is not permitted.
*
* @param array $config
* @return void
*/ */
public function __construct(array $config) protected function __construct()
{ {
$dir = \RX_BASEDIR . 'files/cache/store'; $dir = \RX_BASEDIR . 'files/cache/store';
if (!Storage::isDirectory($dir)) if (!Storage::isDirectory($dir))
@ -63,6 +65,21 @@ class SQLite implements \Rhymix\Framework\Drivers\CacheInterface
$this->_dbh->exec('PRAGMA synchronous = OFF'); $this->_dbh->exec('PRAGMA synchronous = OFF');
} }
/**
* 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();
}
return self::$_instance;
}
/** /**
* Check if the current cache driver is supported on this server. * Check if the current cache driver is supported on this server.
* *
@ -70,9 +87,9 @@ class SQLite implements \Rhymix\Framework\Drivers\CacheInterface
* *
* @return bool * @return bool
*/ */
public function isSupported() public static function isSupported()
{ {
return class_exists('\\SQLite3', false) && config('crypto.authentication_key'); return class_exists('\\SQLite3', false) && config('crypto.authentication_key') !== null;
} }
/** /**

View file

@ -12,15 +12,32 @@ class WinCache implements \Rhymix\Framework\Drivers\CacheInterface
*/ */
public $prefix = true; public $prefix = true;
/**
* The singleton instance is stored here.
*/
protected static $_instance = null;
/**
* Direct invocation of the constructor is not permitted.
*/
protected function __construct()
{
}
/** /**
* Create a new instance of the current cache driver, using the given settings. * Create a new instance of the current cache driver, using the given settings.
* *
* @param array $config * @param array $config
* @return void * @return void
*/ */
public function __construct(array $config) public static function getInstance(array $config)
{ {
if (self::$_instance === null)
{
self::$_instance = new self();
}
return self::$_instance;
} }
/** /**
@ -30,7 +47,7 @@ class WinCache implements \Rhymix\Framework\Drivers\CacheInterface
* *
* @return bool * @return bool
*/ */
public function isSupported() public static function isSupported()
{ {
return function_exists('wincache_ucache_get'); return function_exists('wincache_ucache_get');
} }

View file

@ -12,15 +12,32 @@ class XCache implements \Rhymix\Framework\Drivers\CacheInterface
*/ */
public $prefix = true; public $prefix = true;
/**
* The singleton instance is stored here.
*/
protected static $_instance = null;
/**
* Direct invocation of the constructor is not permitted.
*/
protected function __construct()
{
}
/** /**
* Create a new instance of the current cache driver, using the given settings. * Create a new instance of the current cache driver, using the given settings.
* *
* @param array $config * @param array $config
* @return void * @return void
*/ */
public function __construct(array $config) public static function getInstance(array $config)
{ {
if (self::$_instance === null)
{
self::$_instance = new self();
}
return self::$_instance;
} }
/** /**
@ -30,7 +47,7 @@ class XCache implements \Rhymix\Framework\Drivers\CacheInterface
* *
* @return bool * @return bool
*/ */
public function isSupported() public static function isSupported()
{ {
return function_exists('xcache_get'); return function_exists('xcache_get');
} }

View file

@ -13,7 +13,7 @@ interface CacheInterface
* @param array $config * @param array $config
* @return void * @return void
*/ */
public function __construct(array $config); public static function getInstance(array $config);
/** /**
* Check if the current cache driver is supported on this server. * Check if the current cache driver is supported on this server.
@ -22,7 +22,7 @@ interface CacheInterface
* *
* @return bool * @return bool
*/ */
public function isSupported(); public static function isSupported();
/** /**
* Validate cache settings. * Validate cache settings.