Prefixing group names with the cache enging prefix to avoid possible conflicts with shared servers

This commit is contained in:
Jose Lorenzo Rodriguez 2012-03-26 23:02:26 -04:30
parent 969b682ece
commit 957322ec82
2 changed files with 39 additions and 9 deletions

View file

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

View file

@ -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'));