当我向我的数据库添加新类别时,它插入正常。但是,添加或删除
时Codeigniter Database Query Cache不会更新问题:添加新类别或删除类别后,如何确保codeigniter数据库缓存查询更新正确
$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
);
论坛模型
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;
}
}
控制器
<?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);
}
}
答案 0 :(得分:0)
正如kishor10d所述,查询既不会自动更新,也不会自动过期或在任何持续时间后过期。你必须手动完成它,但它不需要cron工作或任何东西。 Codeigniter有一个方法。
每次更新后,您都必须执行
$this->db->cache_delete('controller', 'method');
如果未传递任何参数,则使用当前URI删除关联的缓存。查询完成后,将再次添加缓存,从而产生更新效果。