I want to implement a cache in Zend_Db, there is no built-in method to provide a cache Zend_Db, so I wonder where I should do this.
I looked at Zend_Db_Table_Abstract(I am expanding it in a custom one App_Model_DbTable_Abstract) and I found a protected method _fetch()that directly accepts the instance Zend_Db_Table_Selectand looks like the last step before the adapter.
I was thinking of overriding this method, serializing the object $select, hashing it and finally caching, and checking every $ select object provided to return the cache or the updated rowset.
Is this right to do?
Here is what I just did:
class App_Model_DbTable_Abstract extends Zend_Db_Table_Abstract
{
protected function _fetch(Zend_Db_Table_Select $select)
{
$hashedQuery = sha1(serialize($select->__toString()));
$cacheManager = Zend_Registry::get('Zend_Cache_Manager');
$cache = $cacheManager->getCache('database');
if (!($data = $cache->load($hashedQuery))) {
$data = parent::_fetch($select);
$cache->save($data, $hashedQuery);
}
return $data;
}
}