Codeigniter caching issue when dealing with query string parameters

て烟熏妆下的殇ゞ 提交于 2020-01-03 18:39:50

问题


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?


回答1:


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 :)




回答2:


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:


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.




回答4:


You should cache if the value of _GET is empty

if(!$_GET)
    $this->output->cache(0);


来源:https://stackoverflow.com/questions/6194714/codeigniter-caching-issue-when-dealing-with-query-string-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!