Add Cache::getDriverName() and clean up confusing method names

This commit is contained in:
Kijin Sung 2016-04-24 16:02:54 +09:00
parent aaba4e306d
commit 9b71df6a01
4 changed files with 47 additions and 23 deletions

View file

@ -11,6 +11,7 @@ class Cache
* The currently enabled cache driver.
*/
protected static $_driver = null;
protected static $_driver_name = null;
/**
* The cache prefix.
@ -42,6 +43,7 @@ class Cache
if (isset($config['type']))
{
$driver_name = $config['type'];
$class_name = '\\Rhymix\\Framework\\Drivers\\Cache\\' . $config['type'];
if (isset($config['ttl']))
{
@ -51,20 +53,24 @@ class Cache
}
elseif (preg_match('/^(apc|dummy|file|memcache|redis|sqlite|wincache|xcache)/', strval(array_first($config)), $matches))
{
$class_name = '\\Rhymix\\Framework\\Drivers\\Cache\\' . $matches[1] . ($matches[1] === 'memcache' ? 'd' : '');
$driver_name = $matches[1] . ($matches[1] === 'memcache' ? 'd' : '');
$class_name = '\\Rhymix\\Framework\\Drivers\\Cache\\' . $driver_name;
}
else
{
$driver_name = null;
$class_name = null;
}
if (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_name = strtolower($driver_name);
}
else
{
self::$_driver = new Drivers\Cache\Dummy(array());
self::$_driver_name = 'dummy';
}
if (self::$_driver->prefix)
@ -99,6 +105,16 @@ class Cache
return $result;
}
/**
* Get the name of the currently enabled cache driver.
*
* @return string|null
*/
public static function getDriverName()
{
return self::$_driver_name;
}
/**
* Get the currently enabled cache driver, or a named driver with the given settings.
*
@ -106,7 +122,7 @@ class Cache
* @param array $config (optional)
* @return object|null
*/
public static function getCacheDriver($name = null, array $config = [])
public static function getDriverInstance($name = null, array $config = [])
{
if ($name === null)
{
@ -131,7 +147,7 @@ class Cache
*
* @return object|null
*/
public static function getCachePrefix()
public static function getPrefix()
{
return self::$_prefix;
}