1

When I add new category to my database it insert fine. But the Codeigniter Database Query Cache does not update when add or remove

Question: Once I add a new category or delete a category how to make sure the codeigniter database cache query gets updated correct

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'community',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => TRUE,
    'cachedir' => APPPATH . 'cache/database/',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

Forum Model

public function getcategories() {
    $this->db->cache_on(); // Cache On

    $query = $this->db->select('*')
            ->order_by('name', 'asc')
            ->from('forum')
            ->get();

    if ($query->num_rows() > 0) {
        return $query->result_array();
    } else {
        return array();
    }
}

public function add() {
    $this->db->trans_start();
    $this->db->trans_strict(FALSE);

    $insert = array(
        'name' => $this->input->post('title'),
        'type' => $this->input->post('type'),
        'pid' => $this->input->post('pid')
    );

    $this->db->insert($this->db->dbprefix . 'forum', $insert);

    $this->db->trans_complete();
    if ($this->db->trans_status() === FALSE) {
        $this->db->trans_rollback();
        return FALSE;
    } else {
        $this->db->trans_commit();
        return TRUE;
    }
}

public function categories_for_deletion($fid) {
    $this->db->where('fid', $fid);
    $this->db->or_where('pid', $fid);

    $query = $this->db->delete('forum');

    $this->db->cache_delete('admin', 'category');
    if ($query) {
        return TRUE;
    } else {
        return FALSE;
    }
}

Controller

<?php

class Category extends MX_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->library('forum');
        $this->load->model('admin/forum/forum_model');
    }

    public function add() {     
        $this->form_validation->set_rules('title', 'title', 'required');

        if ($this->form_validation->run() == TRUE) {

            $this->forum_model->add();

            redirect('admin/category');

        }

        $data['header'] = Modules::run('admin/common/header/index');
        $data['footer'] = Modules::run('admin/common/footer/index');

        $data['categories'] = $this->forum->generate_select();

        $this->load->view('template/forums/category_add_view', $data);
    }

    public function edit($fid) {        
        $this->form_validation->set_rules('title', 'title', 'required');

        if ($this->form_validation->run() == TRUE) {

        }

        $data['header'] = Modules::run('admin/common/header/index');
        $data['footer'] = Modules::run('admin/common/footer/index');

        $this->load->view('template/forums/category_edit_view', $data);
    }

    public function delete($fid) {
        $delete = $this->forum_model->categories_for_deletion($fid);

        if ($delete == TRUE) {
            redirect('admin/category');
        }


        $this->index();
    }

    public function index() {
        $data['categories'] = '';

        if ($this->forum_model->getcategories()) {
            $data['categories'] = $this->forum->set_categories_list($this->forum_model->getcategories());
        }

        $data['header'] = Modules::run('admin/common/header/index');
        $data['navbar'] = Modules::run('admin/common/navbar/index');
        $data['footer'] = Modules::run('admin/common/footer/index');

        $this->load->view('template/forums/category_view', $data);
    }
}
3
  • Is it working like query cache (schema cache) or only data cache? Commented Dec 13, 2016 at 18:36
  • I read the documentation carefully and comes to know that cache files will not updated/expires. You need to do it explicitly. I tried the same thing and it happened with me too. So what I did, I introduce a cron job to delete the cache files manually after interval. I know running cron job is not good idea, but I did not have any other choice for that time. Commented Dec 13, 2016 at 18:45
  • @kishor10d Thanks for the info will not be using codeigniter database query cache any more, Commented Dec 13, 2016 at 20:24

1 Answer 1

1

As kishor10d said, the queries will neither update, nor expire automatically or after any duration. You'll have to do it manually, but it doesn't require a cron job or anything. Codeigniter has a method for it.

After every update, you have to do a

$this->db->cache_delete('controller', 'method');

If you don't pass any parameters, it uses current URI to delete associated cache. When your query completes, the cache will be added again giving an effect of an update.

Reference: $this->db->cache_delete() on CodeIgniter Documentation

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

Comments

Your Answer

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