Hi,
I'm looking to implement a cache within Zend_Db
, there isn't any native method to provide a cache to Zend_Db
, so I'm wondering where should I do it.
I took a look to the Zend_Db_Table_Abstract
(I'm extending it in a custom App_Model_DbTable_Abstract
) and I found a protected method _fetch()
which directly take a Zend_Db_Table_Select
instance and looks like to be the last step before the adapter.
I was thinking override this method, serialize the $select
object, hash it, and finally cache it, and check against each $select object provided to return the cache or an up-to-date rowset.
Is it a correct way 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;
}
}