Skip to content

Instantly share code, notes, and snippets.

@n1k0
Created August 7, 2010 14:10
Show Gist options
  • Save n1k0/512851 to your computer and use it in GitHub Desktop.
Save n1k0/512851 to your computer and use it in GitHub Desktop.
<?php
/**
* This Doctrine Cache Apc class adds a 'prefix' option in order to handle
* different APC environments or setups for storing query and cache results
*
* @author Nicolas Perriault
* @license WTFPL
*/
class Doctrine_Cache_Namespaced_Apc extends Doctrine_Cache_Apc
{
/**
* Computes cache key from an id and configured prefix
*
* @param string $id cache id
* @return string
*/
protected function computeId($id)
{
return $this->getOption('prefix').$id;
}
/**
* Fetch a cache record from this cache driver instance
*
* @param string $id cache id
* @param boolean $testCacheValidity if set to false, the cache validity won't be tested
* @return mixed Returns either the cached data or false
*/
protected function _doFetch($id, $testCacheValidity = true)
{
return parent::_doFetch($this->computeId($id), $testCacheValidity);
}
/**
* Test if a cache record exists for the passed id
*
* @param string $id cache id
* @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
*/
protected function _doContains($id)
{
return parent::_doContains($this->computeId($id));
}
/**
* Save a cache record directly. This method is implemented by the cache
* drivers and used in Doctrine_Cache_Driver::save()
*
* @param string $id cache id
* @param string $data data to cache
* @param int $lifeTime if != false, set a specific lifetime for this cache record (null => infinite lifeTime)
* @return boolean true if no problem
*/
protected function _doSave($id, $data, $lifeTime = false)
{
return parent::_doSave($this->computeId($id), $data, $lifeTime);
}
/**
* Remove a cache record directly. This method is implemented by the cache
* drivers and used in Doctrine_Cache_Driver::delete()
*
* @param string $id cache id
* @return boolean true if no problem
*/
protected function _doDelete($id)
{
return parent::_doDelete($this->computeId($id));
}
}
@n1k0
Copy link
Author

n1k0 commented Aug 7, 2010

Sample usage, in case it's not clear:

<?php
$cacheDriver = new Doctrine_Cache_Namespaced_Apc(array('prefix' => 'prod')); <- 'prod' should be dynamically set, obviously
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine_Core::ATTR_QUERY_CACHE, $cacheDriver);
$manager->setAttribute(Doctrine_Core::ATTR_QUERY_CACHE_LIFESPAN, 3600);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment