Skip to content

Instantly share code, notes, and snippets.

@jakzal
Created April 27, 2011 15:51
Show Gist options
  • Save jakzal/944524 to your computer and use it in GitHub Desktop.
Save jakzal/944524 to your computer and use it in GitHub Desktop.
Namespaced Doctrine_Cache_Apc
<?php
/*
* (c) 2011 Goyello IT Services
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Namespaced version of Doctrine_Cache_Apc driver.
*
* @author Jakub Zalas
*/
class gyDoctrineCacheApc extends Doctrine_Cache_Apc
{
/**
* @var string
*/
private $namespace = null;
/**
* @param string $namespace
* @return null
*/
public function setNamespace($namespace)
{
$this->namespace = $namespace;
}
/**
* @param string $id
* @param boolean $testCacheValidity
* @return mixed
*/
protected function _doFetch($id, $testCacheValidity = true)
{
return parent::_doFetch($this->getNamespacedId($id), $testCacheValidity);
}
/**
* @param string $id
* @return mixed
*/
protected function _doContains($id)
{
return parent::_doContains($this->getNamespacedId($id));
}
/**
* @param string $id
* @param string $data
* @param int $lifeTime
* @return boolean
*/
protected function _doSave($id, $data, $lifeTime = false)
{
return parent::_doSave($this->getNamespacedId($id), $data, $lifeTime);
}
/**
* @param string
* @return boolean
*/
protected function _doDelete($id)
{
return parent::_doDelete($this->getNamespacedId($id));
}
/**
* @param string $id
* @return string
*/
protected function getNamespacedId($id)
{
return is_null($this->namespace) ? $id : $this->namespace . $id;
}
}
<?php
/*
* (c) 2011 GOYELLO IT Services
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require_once(dirname(__FILE__) . '/../../bootstrap/unit.php');
/**
* @author Jakub Zalas
*/
class gyDoctrineCacheApcTest extends gyDoctrineLimeTest
{
public function tearDown()
{
apc_clear_cache('user');
}
public function testThatNamespaceIsNotRequired()
{
$cache = new gyDoctrineCacheApc();
$cache->save('foo', 'bar');
$this->cmp_ok(true, '===', $cache->contains('foo'), 'Not namespaced cache contains "foo"');
$this->cmp_ok('bar', '===', $cache->fetch('foo'), 'Value of "foo" is "bar"');
}
public function testThatNamespacesAreIndependent()
{
$cache1 = new gyDoctrineCacheApc();
$cache1->save('foo', 'bar');
$cache2 = new gyDoctrineCacheApc();
$cache2->setNamespace('myspace');
$this->cmp_ok(true, '===', $cache1->contains('foo'), 'Not namespaced cache contains "foo"');
$this->cmp_ok('bar', '===', $cache1->fetch('foo'), 'Value of "foo" is "bar"');
$this->cmp_ok(false, '===', $cache2->contains('foo'), '"myspace" namespaced cache does not contain "foo"');
$this->cmp_ok(false, '===', $cache2->fetch('foo'), '"myspace" namespaced cache does not return value for "bar"');
}
public function testThatKeysAreNamespaced()
{
$cache1 = new gyDoctrineCacheApc();
$cache1->setNamespace('myspace');
$cache1->save('foo', 'bar');
$cache2 = new gyDoctrineCacheApc();
$cache2->setNamespace('myspace');
$this->cmp_ok(true, '===', $cache1->contains('foo'), 'First "myspace" namespaced cache contains "foo"');
$this->cmp_ok('bar', '===', $cache1->fetch('foo'), 'Value of "foo" is "bar"');
$this->cmp_ok(true, '===', $cache2->contains('foo'), 'Second "myspace" namespaced cache contains "foo"');
$this->cmp_ok('bar', '===', $cache2->fetch('foo'), 'Value of "foo" is "bar"');
}
public function testThatRemovingKeyFromOneNamespaceDoesNotInfluenceTheOther()
{
$cache1 = new gyDoctrineCacheApc();
$cache1->setNamespace('myspace1');
$cache1->save('foo', 'bar');
$cache2 = new gyDoctrineCacheApc();
$cache2->setNamespace('myspace2');
$cache2->save('foo', 'baz');
$cache1->delete('foo');
$this->cmp_ok(false, '===', $cache1->contains('foo'), '"myspace1" namespaced cache does not contain "foo"');
$this->cmp_ok(false, '===', $cache1->fetch('foo'), '"myspace1" namespaced cache does not return value for "bar"');
$this->cmp_ok(true, '===', $cache2->contains('foo'), '"myspace2" namespaced cache does not contain "foo"');
$this->cmp_ok('baz', '===', $cache2->fetch('foo'), '"myspace2" namespaced cache does not return value for "baz"');
}
public function testThatChangingTheNamespaceDoesNotMoveExistingKeys()
{
$cache = new gyDoctrineCacheApc();
$cache->save('foo', 'baz');
$cache->setNamespace('myspace');
$this->cmp_ok(false, '===', $cache->contains('foo'), '"myspace" namespaced cache does not contain "foo"');
$this->cmp_ok(false, '===', $cache->fetch('foo'), 'Value of "foo" is not defined');
}
}
$test = new gyDoctrineCacheApcTest(16);
$test->run();
class ProjectConfiguration extends sfProjectConfiguration
{
public function configureDoctrine(Doctrine_Manager $manager)
{
require_once dirname(__FILE__) . '/../lib/cache/gyDoctrineCacheApc.class.php';
$cache = new gyDoctrineCacheApc();
$cache->setNamespace(md5(sfConfig::get('sf_root_dir')));
$manager->setAttribute(Doctrine_Core::ATTR_QUERY_CACHE, $cacheDriver);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment