Wednesday, October 23, 2013

codeigniter web cache example, custom data cache with file adapter, db cache is automatic

//for db cache
$db['default']['cache_on'] = TRUE;//FALSE;
$db['default']['cachedir'] = APPPATH.'cache/db';//''
//for web cache
$config['cache_path'] = FCPATH.'cache/';

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class items extends HO_Site {

    function __construct() {
        parent::__construct();
       
        //for web cache
        $this->output->cache(intval(config_item('web_cache_minute')));
        $this->output->enable_profiler(ENVIRONMENT == 'development');
       
        //for custom cache
        $this->load->driver('cache', array('adapter' => 'file'));
    }
   
    //web output cache
    function show($id) {  
        $items = array();
        array_push($items, $id . ".boroo");
        array_push($items, $id . ".boldbaatar");

        // Load the subview
        $this->view($items);
    }
   
    //custom data cache
    function ss() {
        $this->load->model('category_model');
        $data = array();
        if (!$data['category'] = $this->cache->get('id')){
            $data['category'] = $this->category_model->get_list();
            $this->cache->save('id', $data['category'], 300);
        } else {
            echo 'sql queries cached...';
        }
        $this->view($data);
    }

}

?>

No comments: