Skip to content

Commit

Permalink
Prefixing group names in MemcacheEgine
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Mar 27, 2012
1 parent 957322e commit 98b14ff
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
22 changes: 19 additions & 3 deletions lib/Cake/Cache/Engine/MemcacheEngine.php
Expand Up @@ -27,6 +27,14 @@
*/
class MemcacheEngine extends CacheEngine {

/**
* Contains the compiled group names
* (prefixed witht the global configuration prefix)
*
* @var array
**/
protected $_compiledGroupNames = array();

/**
* Memcache wrapper.
*
Expand Down Expand Up @@ -243,9 +251,17 @@ public function connect($host, $port = 11211) {
* @return array
**/
public function groups() {
$groups = $this->_Memcache->get($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 = $this->_Memcache->get($groups);
if (count($groups) !== count($this->settings['groups'])) {
foreach ($this->_compiledGroupNames as $group) {
if (!isset($groups[$group])) {
$this->_Memcache->set($group, 1, false, 0);
$groups[$group] = 1;
Expand All @@ -269,6 +285,6 @@ public function groups() {
* @return boolean success
**/
public function clearGroup($group) {
return (bool) $this->_Memcache->increment($group);
return (bool) $this->_Memcache->increment($this->settings['prefix'] . $group);
}
}
5 changes: 3 additions & 2 deletions lib/Cake/Test/Case/Cache/Engine/MemcacheEngineTest.php
Expand Up @@ -413,12 +413,13 @@ public function testGroupReadWrite() {
Cache::config('memcache_groups', array(
'engine' => 'Memcache',
'duration' => 3600,
'groups' => array('group_a', 'group_b')
'groups' => array('group_a', 'group_b'),
'prefix' => 'test_'
));
Cache::config('memcache_helper', array(
'engine' => 'Memcache',
'duration' => 3600,
'prefix' => ''
'prefix' => 'test_'
));
$this->assertTrue(Cache::write('test_groups', 'value', 'memcache_groups'));
$this->assertEquals('value', Cache::read('test_groups', 'memcache_groups'));
Expand Down

0 comments on commit 98b14ff

Please sign in to comment.