Doctrine + Zend_Cache
Da ich mit dem in Doctrine verbauten Cache – Mechanismus nicht zufrieden (evtl. war er mir auch ein wenig zu umständlich
), habe ich mir eine eigene kleine Klasse gebaut, die das cachen alá Zend_Cache erlaubt.
class My_Doctrine_Table extends Doctrine_Table { /** * @var Zend_Cache_Core */ protected $_cache_result = null; /** * My_Doctrine_Table::__construct() * * @param string $name the name of the component * @param Doctrine_Connection $conn the connection associated with this table * @return void */ public function __construct($name, Doctrine_Connection $conn, $initDefinition = false) { parent::__construct ( $name, $conn, $initDefinition ); $frontend = 'Core'; $backend = 'File'; $frontendOptions = array ('automatic_cleaning_factor' => 1, 'lifetime' => 1800, 'automatic_serialization' => true ); $backendOptions = array ('cache_dir' => Zend_Registry::get ( 'config' )->framework->cache_dir ); $this->_cache_result = Zend_Cache::factory ( $frontend, $backend, $frontendOptions, $backendOptions ); } /** * My_Doctrine_Table::findAll() * * @param int $hydrationMode * @return mixed */ public function findAll($hydrationMode = null) { $cache_id = "findAllResult_{$this->getTableName()}_{$hydrationMode}"; if (! ($result = $this->_cache_result->load ( $cache_id, true ))) { $result = parent::findAll ( $hydrationMode ); $this->_cache_result->save ( $result, $cache_id, array ($this->getTableName () ) ); } return $result; } /** * My_Doctrine_Table::find() * * wir überschreiben, die find Methode * um den eigenen Cache Mechanismus zu nutzen * * @param mixed $id * @param int $hydrationMode * @return mixed */ public function find($id, $hydrationMode = null) { $cache_id = "findResult_{$this->getTableName()}_{$id}_{$hydrationMode}"; if (! ($result = $this->_cache_result->load ( $cache_id, true ))) { $result = parent::find ( $id, $hydrationMode ); $this->_cache_result->save ( $result, $cache_id, array ($this->getTableName () ) ); } return $result; } /** * My_Doctrine_Table::cleanCaches() * * löscht die caches zur aktuellen Tabelle * * @return void */ public function cleanCaches() { if ($this->_cache_pager instanceof Zend_Cache_Core) { $this->_cache_pager->clean ( Zend_Cache::CLEANING_MODE_MATCHING_TAG, array ($this->getTableName () ) ); } if ($this->_cache_result instanceof Zend_Cache_Core) { $this->_cache_result->clean ( Zend_Cache::CLEANING_MODE_MATCHING_TAG, array ($this->getTableName () ) ); } } /** * My_Doctrine_Table::findBy() * * wir überschreiben, die findBy Methode * um den eigenen Cache Mechanismus zu nutzen * * @param string $fieldName * @param mixed $value * @param int $hydrationMode * @return mixed */ protected function findBy($fieldName, $value, $hydrationMode = null) { $cache_id = "findBy{$fieldName}Result_{$this->getTableName()}_{$value}_{$hydrationMode}"; if (! ($result = $this->_cache_result->load ( $cache_id, true ))) { $result = parent::findBy ( $fieldName, $value, $hydrationMode ); $this->_cache_result->save ( $result, $cache_id, array ($this->getTableName () ) ); } return $result; } /** * My_Doctrine_Table::findOneBy() * * wir überschreiben, die findOneBy Methode * um den eigenen Cache Mechanismus zu nutzen * * @param mixed $fieldName * @param mixed $value * @param int $hydrationMode * @return mixed */ protected function findOneBy($fieldName, $value, $hydrationMode = null) { $cache_id = "findOneBy{$fieldName}Result_{$this->getTableName()}_{$value}_{$hydrationMode}"; if (! ($result = $this->_cache_result->load ( $cache_id, true ))) { $result = parent::findOneBy ( $fieldName, $value, $hydrationMode ); $this->_cache_result->save ( $result, $cache_id, array ($this->getTableName () ) ); } return $result; } }
Wie der Cache genutzt wird, sollte klar werden, wenn ihr euch die oberen find Methoden anschaut. Um den Cache eines Models jetzt wieder zu löschen, kann man folgendes in seinen Record einbauen:
/** * wird beim einfügen eines datensatzes ausgeführt * @see My_Doctrine_Table::cleanCaches() * @param Doctrine_Event $event */ public function postInsert(Doctrine_Event $event) { $table = $this->getTable (); $table->cleanCaches (); parent::postInsert ( $event ); }
Ich hoffe euch ist das ein wenig verstänlich rübergekommen, falls nicht, könnt ihr ja noch nen Comment hier lassen.
Greez and happy blogging!
Tags: Doctrine, Doctrine_Pager, PHP, Zend Framework, Zend_Cache

Articles, Snippets and Libraries for using Doctrine with the Zend Framework…
Since Doctrine will be integrated into the Zend Framework and Doctrine 2.0 in Zend Framework 2.0 (Both will need PHP 5.3), I thought it would be nice to have have some informations about how you can already use Doctrine in your Zend Framework Applic…