CodeIgniter 中的查询缓存

问题描述 投票:0回答:3

我想在 CodeIgniter 中缓存查询。我为测试所做的是制作一个控制器,我将其命名为

show.php
:

class Show extends CI_Controller{
public function __construct() 
{
    parent::__construct();
    $this->load->model('rejaal_show');
}

public function _remap($method = '',$param = array())
{
    $method = intval($method);
    $this->output->cache(5);
    var_dump ($this->rejaal_show->temp($method));
}
}

还有一个我命名为

rejaal_show.php
的模型:

public function temp($id)
{
    $this->db->cache_on();
    $this->db->where('id',$id);
    $query = $this->db->get('system_store_table');
    return $query->result();
}

当我第一次调用

http://localhost/rejaal/show/1
时,它会显示结果,但当我第二次调用它时,它不显示任何内容。

我应该删除查询缓存文件才能再次显示它吗?我该如何解决这个问题?

特别感谢您的关注。

php mysql codeigniter caching
3个回答
1
投票

您能否确认您已将

$db['default']['cachedir']
设置为
application/config/database.php
中可写文件夹的路径,并且首次运行查询时会在其中创建一个缓存文件?

我能想到的失败的唯一其他原因是您使用了

_remap
覆盖。我没有使用
_remap
来使用数据库缓存,但是知道 CodeIgniter 在缓存文件夹中创建了一个名为controller+action 的文件夹,如果使用重映射,可能无法很好地处理?如果我对此有误,请有人纠正我。


0
投票

网页缓存的CodeIgniter用户指南页面中,它说:

由于 CodeIgniter 存储输出内容的方式,只有当您使用 view 为控制器生成显示时,缓存才会起作用。

在视图中执行

var_dump


0
投票

在 codeigniter 中缓存特定部分有 3 部分

1- 启用缓存

配置/config.php

按此归档

$config['cache_query_string'] = TRUE;

2-将此行放入您的查询所在的函数中

$this->db->cache_on();

3-通过调用此函数清除项目特定部分(例如内容编辑页面)的缓存

public function clear_all_cache()`
{
$CI =& get_instance();
$path = $CI->config->item('cache_path');

$cache_path = ($path == '') ? APPPATH.'cache/' : $path;

$handle = opendir($cache_path);
while (($file = readdir($handle))!== FALSE) 
{
    //Leave the directory protection alone
    if ($file != '.htaccess' && $file != 'index.html')
    {
       @unlink($cache_path.'/'.$file);
    }
}
closedir($handle);       
}

注意:如果清除不起作用,请检查 APPPATH.'cache/' 目录,然后您可以通过实际缓存目录手动覆盖 $cache_path,如下所示

$cache_path = APPPATH.'cache/api+server3/';
© www.soinside.com 2019 - 2024. All rights reserved.