Home  >  Article  >  Backend Development  >  CI framework database query cache optimization method_php example

CI framework database query cache optimization method_php example

WBOY
WBOYOriginal
2016-12-05 13:28:281181browse

The example in 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 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 corresponding clear cache

//缓存不会自动删除 只能手动删除
//这样 你可以在对应的 增改删 语句中清除缓存 就ok了
//清空所有缓存
$this->db->cache_delete_all()
/*
清空单个缓存
example.com/index.php/blog/comments的页面, 缓存系统会把所有生成的缓存文件放进
一个以 blog+comments做为名称的文件夹里. 如果您要删除关于刚才提到的这个例子与
之对应的缓存文件 需要执行以下代码:
*/
$this->db->cache_delete('/blog', 'comments');

Readers who are interested in more CodeIgniter related content can check out the special topics of this site: "codeigniter introductory tutorial", "CI (CodeIgniter) framework advanced tutorial", "php excellent development framework summary", "ThinkPHP introductory tutorial", "ThinkPHP Summary of common methods", "Zend FrameWork framework introductory tutorial", "php object-oriented programming introductory tutorial", "php+mysql database operation introductory tutorial" and "php common database operation skills summary"

I hope this article will be helpful to everyone’s PHP program design based on the CodeIgniter framework.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn