diff --git a/lib/Cake/Cache/Cache.php b/lib/Cake/Cache/Cache.php index a7a01d0a1..d2286960e 100644 --- a/lib/Cake/Cache/Cache.php +++ b/lib/Cake/Cache/Cache.php @@ -616,4 +616,19 @@ class Cache { self::set(null, $config); return $success; } + +/** + * Fetch the engine attached to a specific configuration name. + * + * @param string $config Optional string configuration name to get an engine for. Defaults to 'default'. + * @return bool|CacheEngine False if the engine has not been initialized else the engine + */ + public static function engine($config = 'default') { + if (self::isInitialized($config)) { + return self::$_engines[$config]; + } + + return false; + } + } diff --git a/lib/Cake/Test/Case/Cache/CacheTest.php b/lib/Cake/Test/Case/Cache/CacheTest.php index 985adb4f6..b40c77e18 100644 --- a/lib/Cake/Test/Case/Cache/CacheTest.php +++ b/lib/Cake/Test/Case/Cache/CacheTest.php @@ -538,4 +538,64 @@ class CacheTest extends CakeTestCase { $result = Cache::add('test_add_key', 'test data 2', 'default'); $this->assertFalse($result); } + +/** + * Test engine method. + * + * Success, default engine. + * + * @return void + */ + public function testEngineSuccess() { + $actual = Cache::engine(); + $this->assertIsA($actual, 'CacheEngine'); + + $actual = Cache::engine('default'); + $this->assertIsA($actual, 'CacheEngine'); + } + +/** + * Test engine method. + * + * Success, memcached engine. + * + * @return void + */ + public function testEngineSuccessMemcached() { + $this->skipIf(!class_exists('Memcached'), 'Memcached is not installed or configured properly.'); + + // @codingStandardsIgnoreStart + $socket = @fsockopen('127.0.0.1', 11211, $errno, $errstr, 1); + // @codingStandardsIgnoreEnd + $this->skipIf(!$socket, 'Memcached is not running.'); + fclose($socket); + + Cache::config('memcached', array( + 'engine' => 'Memcached', + 'prefix' => 'cake_', + 'duration' => 3600 + )); + + $actual = Cache::engine('memcached'); + $this->assertTrue($actual->add('test_add_key', 'test data', 10)); + $this->assertFalse($actual->add('test_add_key', 'test data', 10)); + $this->assertTrue($actual->delete('test_add_key')); + } + +/** + * Test engine method. + * + * Failure. + * + * @return void + */ + public function testEngineFailure() { + $actual = Cache::engine('some_config_that_does_not_exist'); + $this->assertFalse($actual); + + Configure::write('Cache.disable', true); + $actual = Cache::engine(); + $this->assertFalse($actual); + } + }