2

I'm a beginner in Zend and I'm getting some problems while trying to cache the database query results.

I've read a lot of forums and docs like this http://framework.zend.com/manual/1.12/en/zend.db.table.html (example 31), but all of them generally say to set the $defaultMetadataCache in the construct method.

I did it, but it seems that the cache is not read because I didn't get a better performance.

Here is my construct of file ./library/Zend/Db/Table/Abstract.php

public function __construct($config = array())
    {
        /**
         * Allow a scalar argument to be the Adapter object or Registry key.
         */
        if (!is_array($config)) {
            $config = array(self::ADAPTER => $config);
        }

        if ($config) {
            $this->setOptions($config);
        }

        $config_temp = new Zend_Config_Ini(APPLICATION_PATH . '/configs/config.ini', APPLICATION_ENV);

        $cache_lifetime = (isset($config_temp->cache->lifetime) && $config_temp->cache->lifetime) ? $config_temp->cache->lifetime : 300;
        $cache_dir = (isset($config_temp->cache->dir) && $config_temp->cache->dir) ? $config_temp->cache->dir : '/tmp/hookit/cache/';

        if(!is_dir($cache_dir))
            mkdir ($cache_dir, 0755, true);

        $frontendOptions = array('lifetime' => $cache_lifetime, 'automatic_serialization' => true);
        $backendOptions = array('cache_dir' => $cache_dir);

        $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);

        if(!self::getDefaultMetadataCache()){
            self::setDefaultMetadataCache($cache);
        }

        $this->_setup();
        $this->init();
    }

1 Answer 1

1

The $defaultMetadataCache parameter allows to cache only table's metadata (not the results of certain query).

It is usefull because

By default, Zend_Db_Table_Abstract queries the underlying database for table metadata whenever that data is needed to perform table operations.

But as soon as this caching option is just for table metadata it seems that you should not see such significant boost in performace as if you were caching all queries to database.

Also you use File as Zend_Cache backend. That means that you store your cached data in regular OS file which is merely the same that to store data in database which also uses files to store the data.

The better approach (in order to improve performance/decrease latency) is to store cached data in RAM and to cache all queries (not just table's metadata).

Sign up to request clarification or add additional context in comments.

2 Comments

Is the some way to do it automatically in Zend or I need to implement it?
There is no general technique provided by ZF - you need to implement it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.