Zend how to use cache component

前端 未结 4 1468
眼角桃花
眼角桃花 2020-12-29 17:04

Let\'s say you have this scenario:a simple blog home-page that loads both static content as well as dynamic content. The static content is composed of images that rarely cha

4条回答
  •  Happy的楠姐
    2020-12-29 17:49

    Zend cache provide a very simple way to store data in cache and to increase the speed. Zend uses frontend and back end to caching. Front end is useful to access or operate the cache. Back end is useful to store data in File , memcache, Sqlite etc.

    First of all initialize the fronted and backed in bootstrap file by creating on function in bootstrap file.

    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);
    }
    

    Then use the zend cache factory to define the cache object. The parameter core define the zend cache core means of generic type File parameter is to define the cache storage means where to store the records of cache then second and forth is for frontend and backend.

    Now register that cache array using zend registry so that you can use that are in any controller , model etc.

    Define Below code in any controller or any model where you want to use caching of data.

        $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);
    }
    

    First of all in above code we get the cache array. Now if result one is not set then caching done means the file is generated at the path that you define in back-end array

    For the Next time page load that data is retrieve from the file where the caching data store.

    You can check the file as per defined path.

    In that file data is in json format.

提交回复
热议问题