From 957322ec825492d94d348199391732bec03bef6b Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Mon, 26 Mar 2012 23:02:26 -0430 Subject: [PATCH] Prefixing group names with the cache enging prefix to avoid possible conflicts with shared servers --- lib/Cake/Cache/Engine/ApcEngine.php | 23 ++++++++++++++--- .../Test/Case/Cache/Engine/ApcEngineTest.php | 25 +++++++++++++++---- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/lib/Cake/Cache/Engine/ApcEngine.php b/lib/Cake/Cache/Engine/ApcEngine.php index e4b9e8af9..f55d35037 100644 --- a/lib/Cake/Cache/Engine/ApcEngine.php +++ b/lib/Cake/Cache/Engine/ApcEngine.php @@ -25,6 +25,14 @@ */ class ApcEngine extends CacheEngine { +/** + * Contains the compiled group names + * (prefixed witht the global configuration prefix) + * + * @var array + **/ + protected $_compiledGroupNames = array(); + /** * Initialize the Cache Engine * @@ -135,10 +143,17 @@ class ApcEngine extends CacheEngine { * @return array **/ public function groups() { - $groups = apc_fetch($this->settings['groups']); - - if (count($groups) !== count($this->settings['groups'])) { + $groups = $this->_compiledGroupNames; + if (empty($groups)) { foreach ($this->settings['groups'] as $group) { + $groups[] = $this->settings['prefix'] . $group; + } + $this->_compiledGroupNames = $groups; + } + + $groups = apc_fetch($groups); + if (count($groups) !== count($this->settings['groups'])) { + foreach ($this->_compiledGroupNames as $group) { if (!isset($groups[$group])) { apc_store($group, 1); $groups[$group] = 1; @@ -161,7 +176,7 @@ class ApcEngine extends CacheEngine { * @return boolean success **/ public function clearGroup($group) { - apc_inc($group, 1, $success); + apc_inc($this->settings['prefix'] . $group, 1, $success); return $success; } diff --git a/lib/Cake/Test/Case/Cache/Engine/ApcEngineTest.php b/lib/Cake/Test/Case/Cache/Engine/ApcEngineTest.php index 46e3cac80..e7d0f4713 100644 --- a/lib/Cake/Test/Case/Cache/Engine/ApcEngineTest.php +++ b/lib/Cake/Test/Case/Cache/Engine/ApcEngineTest.php @@ -208,16 +208,21 @@ class ApcEngineTest extends CakeTestCase { * @return void */ public function testGroupsReadWrite() { - Cache::config('apc_groups', array('engine' => 'Apc', 'duration' => 0, 'groups' => array('group_a', 'group_b'))); + Cache::config('apc_groups', array( + 'engine' => 'Apc', + 'duration' => 0, + 'groups' => array('group_a', 'group_b'), + 'prefix' => 'test_' + )); $this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups')); $this->assertEquals('value', Cache::read('test_groups', 'apc_groups')); - apc_inc('group_a'); + apc_inc('test_group_a'); $this->assertFalse(Cache::read('test_groups', 'apc_groups')); $this->assertTrue(Cache::write('test_groups', 'value2', 'apc_groups')); $this->assertEquals('value2', Cache::read('test_groups', 'apc_groups')); - apc_inc('group_b'); + apc_inc('test_group_b'); $this->assertFalse(Cache::read('test_groups', 'apc_groups')); $this->assertTrue(Cache::write('test_groups', 'value3', 'apc_groups')); $this->assertEquals('value3', Cache::read('test_groups', 'apc_groups')); @@ -229,7 +234,12 @@ class ApcEngineTest extends CakeTestCase { * @return void */ public function testGroupDelete() { - Cache::config('apc_groups', array('engine' => 'Apc', 'duration' => 0, 'groups' => array('group_a', 'group_b'))); + Cache::config('apc_groups', array( + 'engine' => 'Apc', + 'duration' => 0, + 'groups' => array('group_a', 'group_b'), + 'prefix' => 'test_' + )); $this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups')); $this->assertEquals('value', Cache::read('test_groups', 'apc_groups')); $this->assertTrue(Cache::delete('test_groups', 'apc_groups')); @@ -243,7 +253,12 @@ class ApcEngineTest extends CakeTestCase { * @return void **/ public function testGroupClear() { - Cache::config('apc_groups', array('engine' => 'Apc', 'duration' => 0, 'groups' => array('group_a', 'group_b'))); + Cache::config('apc_groups', array( + 'engine' => 'Apc', + 'duration' => 0, + 'groups' => array('group_a', 'group_b'), + 'prefix' => 'test_' + )); $this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups')); $this->assertTrue(Cache::clearGroup('group_a', 'apc_groups'));