Adapter für Doctrine_Cache zu Zend_Cache
Dieser Adapter erlaubt es eine schon vorhandene Instanz eines Zend_Cache_Core-Objects als Cache für Doctrine und dessen Query und Result-Cache zu nutzen. So ist es mit Doctrine möglich auch Backends zu nutzen die Doctrine nicht bietet (ZendCache, ZendServer oder den TwoLevel-Cache mit verschiedenen Adaptern) und man spart sie die Resourcen für eine Extra-Instanz von Doctrine_Cache.
Ausserdem ermöglicht der Adapter ein automatisches Prefixing aller Einträge um ein einfaches löschen aller mit Doctrine assoziierten Cache-Einträge auch ohne Tags zu ermöglichen.
<?php
/**
* An Adapter for using a Zend_Cache_Core-Instance as Query or Result-Cache
* for Doctrine
*
* Offers an additional Prefix for its entries for usage within prefix-based
* Cache-Structure (for example when using one Zend_Cache_Core for a complete
* system)
*
* @uses Doctrine_Cache_Interface
* @author Benjamin Steininger
* @license New BSD License
* @category Robo47
* @package Robo47_Cache
* @todo Add support for Tags to automatically tag all Entry made with a
* set of Tags provided by the constructor
*/
class Robo47_Cache_DoctrineAdapter implements Doctrine_Cache_Interface
{
/**
* @var Zend_Cache_Core
*/
protected $_cache = null;
/**
* @param string
*/
protected $_prefix = '';
/**
* @param Zend_Cache_Core $cache
* @param string $prefix
*/
public function __construct(Zend_Cache_Core $cache, $prefix = '')
{
$this->_cache = $cache;
$this->_prefix = $prefix;
}
/**
* Test if a cache is available for the given id and (if yes) return it
* (false else)
*
* Note : return value is always "string" (unserialization is done by the
* core not by the backend)
*
* @param string $id cache id
* @param boolean $testCacheValidity if set to false, the cache
* validity won't be tested
* @return string cached datas (or false)
*/
public function fetch($id, $testCacheValidity = true)
{
$id = $this->_prefix . $id;
return $this->_cache->load($id, $testCacheValidity);
}
/**
* Test if a cache is available or not (for the given id)
*
* @param string $id cache id
* @return mixed false (a cache is not available) or "last modified"
* timestamp (int) of the available cache record
*/
public function contains($id)
{
$id = $this->_prefix . $id;
return $this->_cache->test($id);
}
/**
* Save some string datas into a cache record
*
* Note : $data is always saved as a string
*
* @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
*/
public function save($id, $data, $lifeTime = false)
{
$id = $this->_prefix . $id;
return $this->_cache->save($data, $id, array(), $lifeTime);
}
/**
* Remove a cache record
*
* @param string $id cache id
* @return boolean true if no problem
*/
public function delete($id)
{
$id = $this->_prefix . $id;
return $this->_cache->remove($id);
}
}
Trackbacks (0)
Trackbackurl: http://www.robo47.net/trackback/snippet/32Es sind keine Trackbacks vorhanden.
You liked it ? Link it on your homepage or blog:



Benjamin Steininger ist Webentwickler bei
Kommentare (0)
Es sind noch keine Kommentare vorhanden.
Die Kommentare zu diesem Beitrag sind gesperrt.