我喜欢使用CodeIgniters page caching。但我在控制器中有一个视图计数器,如下所示:CodeIgniter页面缓存异常
$this->db->query("UPDATE tb_product SET popularity=popularity+1 WHERE product_id=".$this->db->escape($this->uri->segment(2))."");
是否可以使用页面缓存但是只能运行此查询的异常?
我喜欢使用CodeIgniters page caching。但我在控制器中有一个视图计数器,如下所示:CodeIgniter页面缓存异常
$this->db->query("UPDATE tb_product SET popularity=popularity+1 WHERE product_id=".$this->db->escape($this->uri->segment(2))."");
是否可以使用页面缓存但是只能运行此查询的异常?
感谢您的回答,但我已经将计数器划出来并写了一个cronjob,它将通过Google Analytics(分析)通过GAPI获取统计信息。
是的。你可以尝试实现一个钩子。这必须是'cache_override'钩子。你可以从钩子做数据库调用。
http://ellislab.com/codeigniter/user-guide/general/hooks.html
你的钩子方法将不得不调用原始缓存本身做DB调用之后。
function your_hook()
{
// your DB code here
// Use some CI globals for this, see /system/core/Codeigniter.php
if ($OUT->_display_cache($CFG, $URI) == TRUE)
{
exit;
}
}
你可以编写一个自定义的Output.php类并覆盖(装饰)原始的_display_cache方法。将你的MY_Ouput.php放在/ application/core目录下,CI会自动使用它。
http://ellislab.com/codeigniter/user-guide/general/core_classes.html
把这样的事情在里面:
class MY_Output extends CI_Output
{
function _display_cache(&$CFG, &$URI)
{
// your DB call
// The original call
return parent::_display_cache($CFG, $URI);
}
}
我没有尝试这个自己,但它应该帮助你对你的方式。其中之一可能会奏效。祝你好运!