title: Where should I implement cache within Zend_Db?
body: <p>I'm looking to implement a cache within <code>Zend_Db</code>, there isn't any native method to provide a cache to <code>Zend_Db</code>, so I'm wondering where should I do it.</p>
  
  <p>I took a look to the <code>Zend_Db_Table_Abstract</code> (I'm extending it in a custom <code>App_Model_DbTable_Abstract</code>) and I found a protected method <code>_fetch()</code> which directly take a <code>Zend_Db_Table_Select</code> instance and looks like to be the last step before the adapter.</p>
  
  <p>I was thinking override this method, serialize the <code>$select</code> object, hash it, and finally cache it, and check against each $select object provided to return the cache or an up-to-date rowset.</p>
  
  <p>Is it a correct way to do?</p>
  
  <p>Here is what I just did:</p>
  
  <pre><code>class App_Model_DbTable_Abstract extends Zend_Db_Table_Abstract
  {
      protected function _fetch(Zend_Db_Table_Select $select)
      {
          $hashedQuery = sha1(serialize($select-&gt;__toString()));
          $cacheManager = Zend_Registry::get('Zend_Cache_Manager');
          $cache = $cacheManager-&gt;getCache('database');
          if (!($data = $cache-&gt;load($hashedQuery))) {
              $data = parent::_fetch($select);
              $cache-&gt;save($data, $hashedQuery);
          }
          return $data;
      }
  }
  </code></pre>