Home > Article > Backend Development > How to implement CI framework database query cache optimization
This article mainly introduces the method of optimizing the CI framework database query cache, and analyzes in detail the opening, closing, use, clearing and other related operating skills and precautions of the cache in the CI framework in the form of examples. Friends who need it can Refer to the following
The example of this article describes the CI framework database query cache optimization method. Share it with everyone for your reference, the details are as follows:
There is a better query optimization in the CI framework, which is database cache optimization
1. Turn on the cache
//在application/config.php中开启 $db['default']['cache_on'] = TRUE; //在application/config.php中开启 $db['default']['cachedir'] = './cache'; //并在对应的目录中加一个可写缓存目录cache
2. Enable cache statements in the corresponding query
// 打开缓存开关 $this->db->cache_on(); $query = $this->db->query("SELECT * FROM mytable"); // 使下面这条查询不被缓存 $this->db->cache_off(); $query = $this->db->query("SELECT * FROM members WHERE member_id = '$current_user'"); // Turn caching back on $this->db->cache_on(); $query = $this->db->query("SELECT * FROM another_table");
3. Add the corresponding clear cache
//缓存不会自动删除 只能手动删除 //这样 你可以在对应的 增改删 语句中清除缓存 就ok了 //清空所有缓存 $this->db->cache_delete_all() /* 清空单个缓存 example.com/index.php/blog/comments的页面, 缓存系统会把所有生成的缓存文件放进 一个以 blog+comments做为名称的文件夹里. 如果您要删除关于刚才提到的这个例子与 之对应的缓存文件 需要执行以下代码: */ $this->db->cache_delete('/blog', 'comments');
The above is the entire content of this article, I hope it will be helpful to everyone Learning is helpful. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
How to use the CI framework to optimize file uploads and upload multiple files
How to implement PHP CodeIgniter Paging and multi-condition query
The above is the detailed content of How to implement CI framework database query cache optimization. For more information, please follow other related articles on the PHP Chinese website!