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

@ -49,7 +49,7 @@ class CacheHandler extends Handler
*/
public function isSupport()
{
return $this->_always_use_file || !(Rhymix\Framework\Cache::getCacheDriver() instanceof Rhymix\Framework\Drivers\Cache\Dummy);
return $this->_always_use_file || (Rhymix\Framework\Cache::getDriverName() !== 'dummy');
}
/**

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;
}

View file

@ -83,10 +83,9 @@ class adminAdminController extends admin
}
// remove object cache
$cache_driver = Rhymix\Framework\Cache::getCacheDriver();
if (!($cache_driver instanceof Rhymix\Framework\Drivers\Cache\File))
if (!in_array(Rhymix\Framework\Cache::getDriverName(), array('file', 'sqlite', 'dummy')))
{
$cache_driver->clear();
Rhymix\Framework\Cache::clearAll();
}
// remove old cache dir
@ -679,7 +678,7 @@ class adminAdminController extends admin
{
$cache_servers = array();
}
if (!Rhymix\Framework\Cache::getCacheDriver($vars->object_cache_type, $cache_servers))
if (!Rhymix\Framework\Cache::getDriverInstance($vars->object_cache_type, $cache_servers))
{
return new Object(-1, 'msg_cache_handler_not_supported');
}

View file

@ -38,18 +38,27 @@ class CacheTest extends \Codeception\TestCase\Test
$this->assertContains('sqlite', $drivers);
}
public function testGetCacheDriver()
public function testGetDriverName()
{
$driver = Rhymix\Framework\Cache::getCacheDriver('dummy');
$driver = Rhymix\Framework\Cache::init(array('type' => 'dummy'));
$this->assertEquals('dummy', Rhymix\Framework\Cache::getDriverName());
$driver = Rhymix\Framework\Cache::init(array('type' => 'sqlite'));
$this->assertEquals('sqlite', Rhymix\Framework\Cache::getDriverName());
}
public function testGetDriverInstance()
{
$driver = Rhymix\Framework\Cache::getDriverInstance('dummy');
$this->assertTrue($driver instanceof Rhymix\Framework\Drivers\Cache\Dummy);
$driver = Rhymix\Framework\Cache::getCacheDriver();
$driver = Rhymix\Framework\Cache::getDriverInstance();
$this->assertTrue($driver instanceof Rhymix\Framework\Drivers\Cache\File);
}
public function testGetCachePrefix()
public function testGetPrefix()
{
$prefix = Rhymix\Framework\Cache::getCachePrefix();
$prefix = Rhymix\Framework\Cache::getPrefix();
$this->assertEquals(\RX_VERSION . ':', $prefix);
}
@ -99,12 +108,12 @@ class CacheTest extends \Codeception\TestCase\Test
Rhymix\Framework\Cache::init(array('type' => 'sqlite'));
Rhymix\Framework\Cache::set('foo', 'foo');
Rhymix\Framework\Cache::set('bar', 42);
$prefix = Rhymix\Framework\Cache::getCachePrefix();
$prefix = Rhymix\Framework\Cache::getPrefix();
$this->assertEquals(1, Rhymix\Framework\Cache::getCacheDriver()->incr($prefix . 'foo', 1));
$this->assertEquals(45, Rhymix\Framework\Cache::getCacheDriver()->incr($prefix . 'bar', 3));
$this->assertEquals(-1, Rhymix\Framework\Cache::getCacheDriver()->decr($prefix . 'foo', 2));
$this->assertEquals(49, Rhymix\Framework\Cache::getCacheDriver()->decr($prefix . 'bar', -4));
$this->assertEquals(1, Rhymix\Framework\Cache::getDriverInstance()->incr($prefix . 'foo', 1));
$this->assertEquals(45, Rhymix\Framework\Cache::getDriverInstance()->incr($prefix . 'bar', 3));
$this->assertEquals(-1, Rhymix\Framework\Cache::getDriverInstance()->decr($prefix . 'foo', 2));
$this->assertEquals(49, Rhymix\Framework\Cache::getDriverInstance()->decr($prefix . 'bar', -4));
}
public function testClearAll()
@ -118,25 +127,25 @@ class CacheTest extends \Codeception\TestCase\Test
public function testCacheGroups()
{
Rhymix\Framework\Cache::init(array('type' => 'sqlite'));
$prefix = Rhymix\Framework\Cache::getCachePrefix();
$prefix = Rhymix\Framework\Cache::getPrefix();
$this->assertTrue(Rhymix\Framework\Cache::set('foobar:subkey:1234', 'rhymix'));
$this->assertTrue(Rhymix\Framework\Cache::exists('foobar:subkey:1234'));
$this->assertEquals('rhymix', Rhymix\Framework\Cache::get('foobar:subkey:1234'));
$this->assertEquals('rhymix', Rhymix\Framework\Cache::getCacheDriver()->get($prefix . 'foobar#0:subkey:1234'));
$this->assertEquals('rhymix', Rhymix\Framework\Cache::getDriverInstance()->get($prefix . 'foobar#0:subkey:1234'));
$this->assertEquals(0, Rhymix\Framework\Cache::getGroupVersion('foobar'));
$this->assertTrue(Rhymix\Framework\Cache::clearGroup('foobar'));
$this->assertFalse(Rhymix\Framework\Cache::exists('foobar:subkey:1234'));
$this->assertTrue(Rhymix\Framework\Cache::set('foobar:subkey:1234', 'rhymix'));
$this->assertEquals('rhymix', Rhymix\Framework\Cache::getCacheDriver()->get($prefix . 'foobar#1:subkey:1234'));
$this->assertEquals('rhymix', Rhymix\Framework\Cache::getDriverInstance()->get($prefix . 'foobar#1:subkey:1234'));
$this->assertEquals(1, Rhymix\Framework\Cache::getGroupVersion('foobar'));
}
public function testGetRealKey()
{
Rhymix\Framework\Cache::init(array('type' => 'sqlite'));
$prefix = Rhymix\Framework\Cache::getCachePrefix();
$prefix = Rhymix\Framework\Cache::getPrefix();
$this->assertEquals($prefix . 'foo', Rhymix\Framework\Cache::getRealKey('foo'));
$this->assertEquals($prefix . 'bar#0:2016', Rhymix\Framework\Cache::getRealKey('bar:2016'));