title: How to inject caching into Zend_Db
body: <p>I'm looking at ways to reduce the number of queries that are being performed on my MySQL server. </p>
  
  <p>The application and database servers are on separate machines, so it seems caching previous query results on the application server is not a bad option to pursue. Obviously there are other strategies like reducing the number of queries and optimizing the query statements, however I don't think I should discount query caching as an option.</p>
  
  <p>I was initially thinking that I could easily turn on some form of query caching in the <code>Zend_Db</code> object, however the closest I've come to seeing something crosscut is the <code>Zend_Db_Profiler</code>.</p>
  
  <p>One approach I was considering was to create a proxy for my <code>Zend_Db_Adapter</code> object. Something to the effect of:</p>
  
  <pre><code>/**
   * Initialize the database.
   *
   * @return void
   */
  protected function _initDb() {
      // Instantiate a connection to the database
      $config = Zend_Registry::get('config');
      $db = new MyProject_Db_Proxy (
                  Zend_Db::factory($config-&gt;resources-&gt;db-&gt;adapter, 
                                   $config-&gt;resources-&gt;db-&gt;params)
                );
  
      // Store the database object in the registry
      Zend_Registry::set('db', $db);
  
      Zend_Db_Table::setDefaultAdapter('db');
  }
  </code></pre>
  
  <p>I've looked through the various <code>Zend_Db</code> interfaces and classes and it looks possible that I could inject caching into the <code>Zend_Db_Adapter_Abstract::query()</code> method via the proxy. Something to the effect of:</p>
  
  <pre><code>public MyProject_Db_Proxy() {
  
    private $_adapter = null;
    ...
    public function query($sql, $bind = array()) {
      // Generate the cache key by joining and hashing $sql and $bind
      // Check to see if key is in cache, return if found
      // Execute query
      $stmt = this-&gt;_adapter-&gt;query($sql, $bind);
      // Cache result
    }
    ...
  }
  </code></pre>
  
  <p>My question is: <strong>Is there a better/easier way to inject caching into Zend_Db?</strong></p>