Codeigniter caching issue when dealing with query

2019-06-25 13:04发布

Greetings,

I'm writing a CI web application which implements the standard file caching functionality as such:

$this->output->cache(n);

I'm using a combination of segments and query string parameters, and appear to be experiencing an issue as a result. What I'm seeing in my use cases and in the Output class code is that the caching is solely segment based. As such, http://www.example.com/segment/?q=foo and http://www.example.com/segment/?q=bar are treated as identical requests.

Does anyone have any insight or recommendations regarding how the url_helper, Output class, or CI base class can be edited such that the above example treats example.com/segment/?q=foo and example.com/segment/?q=bar as separate, unique requests and stores the responses in separate files individually?

4条回答
看我几分像从前
2楼-- · 2019-06-25 13:09

Here's some code to override Codeigniter's Output class which seems to work for me.

Create the file application/core/MY_Output.php, copy in the _write_cache() and _display_cache() functions from Output.php and update it like this:

class MY_Output extends CI_Output {

    function __construct() {
        parent::__construct();
    }

    function _write_cache($output) {
        ....

        $uri = $CI->config->item('base_url').
               $CI->config->item('index_page').
               $CI->uri->uri_string();

        // append querystring
        $qs = (empty($_SERVER['QUERY_STRING'])) ? '' : '?'.$_SERVER['QUERY_STRING'];
        $uri .= $qs;
        // append querystring  

        ....
    }

    function _display_cache(&$CFG, &$URI)
        ....

        $uri = $CI->config->item('base_url').
               $CI->config->item('index_page').
               $URI->uri_string;

        // append querystring
        $qs = (empty($_SERVER['QUERY_STRING'])) ? '' : '?'.$_SERVER['QUERY_STRING'];
        $uri .= $qs;
        // append querystring

        ....
    }
查看更多
爷的心禁止访问
3楼-- · 2019-06-25 13:12

You should cache if the value of _GET is empty

if(!$_GET)
    $this->output->cache(0);
查看更多
Emotional °昔
4楼-- · 2019-06-25 13:21

this can fix codeigniter cache with querystring codeigniter cache with querystring

it is thai language page but you can just copy that code and put it in application/core/MY_Output.php :)

查看更多
我只想做你的唯一
5楼-- · 2019-06-25 13:21

Into config/config.php

You should enable cache_query_string like this

$config['cache_query_string'] = TRUE;

take all query parameters into account. Please be aware that this may result in numerous cache files generated for the same page over and over again.

查看更多
登录 后发表回答