Skip to content

Commit

Permalink
Prefixing group names with the cache enging prefix to avoid possible …
Browse files Browse the repository at this point in the history
…conflicts with shared servers
  • Loading branch information
lorenzo committed Mar 27, 2012
1 parent 969b682 commit 957322e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
21 changes: 18 additions & 3 deletions lib/Cake/Cache/Engine/ApcEngine.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -135,10 +143,17 @@ public function clear($check) {
* @return array
**/
public function groups() {
$groups = apc_fetch($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->settings['groups'] as $group) {
foreach ($this->_compiledGroupNames as $group) {
if (!isset($groups[$group])) {
apc_store($group, 1);
$groups[$group] = 1;
Expand All @@ -161,7 +176,7 @@ public function groups() {
* @return boolean success
**/
public function clearGroup($group) {
apc_inc($group, 1, $success);
apc_inc($this->settings['prefix'] . $group, 1, $success);
return $success;
}

Expand Down
25 changes: 20 additions & 5 deletions lib/Cake/Test/Case/Cache/Engine/ApcEngineTest.php
Expand Up @@ -208,16 +208,21 @@ public function testClear() {
* @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'));
Expand All @@ -229,7 +234,12 @@ public function testGroupsReadWrite() {
* @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'));
Expand All @@ -243,7 +253,12 @@ public function testGroupDelete() {
* @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'));
Expand Down

0 comments on commit 957322e

Please sign in to comment.