Zend how to use the cache component

Let's say you have a scenario like this: a simple blog homepage that loads both static content and dynamic content. Static content consists of images that rarely change. I also have dynamic content with dynamic content. Dynamic content consists of all your blog posts (text and image) and related user comments. Dynamic content changes periodically from every hour to every day.

How do you go with caching? And, in particular, if the user leaves a comment or the administrator adds / edits a message, you need to manually start cleaning the cache in order to update the version of this blog home page

Thank you for your patience.

Luke

thanks again

+3
source share
4 answers

So, the main use of the cache is shown by @mingos. He talks about the general cache, which is good. However, ZF has several different caching mechanisms that you can use for different things. You do not need to limit yourself to one type of cache. You can use a mixture of them. For example, to cache your static content, Zend_Cache_Frontend_Page is worth considering, as it will generate the full html file of your static pages. If you have a lot of configuration files, for example. long routes.ini or something else, you can cache them using Zend_Cache_Frontend_File. With this, you save time parsing ini files for each request. Much of your views can be cached using Zend_Cache_Frontend_Output, etc.

, - . , . , 100 , 100 (.. ). , , , . / , .

+4

: http://framework.zend.com/manual/1.11/en/zend.cache.html

, , , - .

:

public function _initCache () {
    $cache = Zend_Cache::factory(
        'Core',
        'File',
        array(
            'lifetime' => 3600 * 24, //cache is cleaned once a day
            'automatic_serialization' => true
        ),
        array('cache_dir' => APPLICATION_PATH.'/cache')
    );
    Zend_Db_Table_Abstract::setDefaultMetadataCache($cache); //cache database table schemata metadata for faster SQL queries
    Zend_Registry::set('Cache', $cache);
}

load() save() . :

$cache = Zend_Registry::get('Cache');
if (!$this->menu = $cache->load('main_menu')) {
    $model = new Model_Menu();
    $this->menu = $model->get();
    $cache->save($this->menu,'main_menu');
}

"main_menu". , .

, . :

Zend_Registry::get('Cache')->remove('main_menu');

, . .

+7

Zend . Zend . . , memcache, Sqlite ..

, , .

protected function _initCache(){

    $frontend= array(
        'lifetime' => 7200,
        'automatic_serialization' => true
    );

    $backend= array(
        'cache_dir' => '../application/tmp/',
    );

    $cache = Zend_Cache::factory('core',
            'File',
            $frontend,
            $backend
    );
    Zend_Registry::set('cache',$cache);
}

zend factory . ​​ zend- File, -, , .

, zend, , ..

, .

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

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

. , , , ,

, .

.

json.

+5

A simple cache is one that expires after a certain period of time. This simplifies and simplifies the caching process. The Zend Guide provides more information on the basics of caching .

However, real-time information and cached information are two worlds. If you need real-time, do not cache.

If the caching level is too complex, you can destroy your entire application.

0
source

All Articles