Where should I implement the cache in Zend_Db?

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;
    }
}
+3
2

, zf create db-table <name> , Zend_Db_Table_Abstract, .

, db ZF, , dbtable. : , , , :

  • , ,
  • X ( dbtable, , XML , JSON ..)

, , dbtable, . http://www.slideshare.net/weierophinney/playdoh-modelling-your-objects-1766001 ( # 35) .

: dbtable , , db.

+3
    public function indexAction()
        {
            // action body
            $this->_helper->layout->setLayout('layout');

            $db = new Zend_Db_Adapter_Pdo_Mysql(array('host' => 'localhost',
                                  'username' => 'root',
                                  'password' => '',
                                      'dbname' => 'zendtest'));
            $sql  = "SELECT SQL_CALC_FOUND_ROWS "
                      . "       register.firstname, "
                      . "       register.lastname, "
                      . "       register.username, "
                      . "       register.password, "
                      . "       register.email, "
                      . "       register.city, "
                      . "       register.state, "
                      . "       register.contactno "     
                      . "  FROM register register "
                      . "  WHERE register.id = ? ";                               

                $result = $db->fetchall($sql,1);    

                        $result1 = "";
               $cache = Zend_Registry::get('cache');

                if(!$result1 = $cache->load('mydata2')) {
                    echo 'caching the data…..';
                    $cache->save($result, 'mydata2');
                   } else {
                    echo 'retrieving cache data…….';
                    Zend_Debug::dump($result1);
                  }
}

, , zend DB, ... db. .

+3

Source: https://habr.com/ru/post/1768803/


All Articles