Max
Max

Reputation: 1548

Codeigniter Page cache with GET parameter

I am newbie to CI cache. I am facing some weird problem with codeigniter page caching. $this->output->cache(300); I was expecting that cached version would not load if arguments in GET[] would change. But it is loading cache without considering any GET[] parameters.

I have one page where it says whether comment has been saved or not [via get parameter], /product/product-name/?saved=true redirecting to same page where comment form is located. But it is not working. How can i invalidate old cache and create new one depending upon the get parameter? or i need to change the behavior of my comment system?

Thanks.

EDIT

Should i simply use database cache instead of Web page cache in this case?

Upvotes: 1

Views: 2472

Answers (4)

I test on CI 3+ , file system/core/Output.php 559 line, change this

if ($CI->config->item('cache_query_string') && !empty($_SERVER['QUERY_STRING']))
    {
        $uri .= '?'.$_SERVER['QUERY_STRING'];
    }

on this

if ($CI->config->item('cache_query_string') /* && ! empty($_SERVER['QUERY_STRING']) */ && !empty($_REQUEST))
    {
        // $uri .= '?'.$_SERVER['QUERY_STRING'];
        $uri .= '?'.http_build_query($_REQUEST);

    }

And add string to your application/config/config.php

$config['cache_query_string'] = true;

it will be work with GET, POST, COOKIE .... If need only GET, just $config['cache_query_string'] = true; - enough

Upvotes: 1

Seb33300
Seb33300

Reputation: 7566

You just have to enable the cache_query_string option in the config/config.php file.

$config['cache_query_string'] = TRUE;

Upvotes: 3

Ravi Dhoriya ツ
Ravi Dhoriya ツ

Reputation: 4414

I found no easier way using Hooks to prevent writing cache, as its calling _write_cache() inside the _display() method itself of CI_Output class.

For quick and easiest solution I added two conditions to display cache and write cache, if Query String parameter has variable defined( offset in my case, as I wanted for pagination)

Edit: system/core/Output.php

Add Condition to prevent writing cache, if specific GET parameter found:

function _write_cache($output)
{
    if (isset($_GET['offset']) AND ! empty($_GET['offset']))
    {
        log_message('debug', " Don't write cache please. As as its matching condition");
        return;
    }
    ...
    ...
}

Add Condition to prevent displaying cache, if specific GET parameter found:

function _display_cache(&$CFG, &$URI)
{
    if (isset($_GET['offset']) AND ! empty($_GET['offset']))
    {
        log_message('debug', " Don't display cache please. As as its matching condition");
        return FALSE;
    }
    ...
    ...
}

Upvotes: 0

Clifford James
Clifford James

Reputation: 161

Create a cache_override hook to check if there are any GET[] variables set and then skip the cache_override.

[EDIT #1]

Here is an example:

Create this file in your hooks directory:

<?php

class GetChecker {

    public function checkForGet()
    {
        global $OUT, $CFG, $URI;

        if (isset($_GET) AND ! empty($_GET))
        {
            return;
        }

        if ($OUT->_display_cache($CFG, $URI) == TRUE)
        {
            exit;
        }
    }

}

Then add this to the config/hooks.php:

$hook['cache_override'][] = array(
    'class' => 'GetChecker',
    'function' => 'checkForGet',
    'filename' => 'GetChecker.php',
    'filepath' => 'hooks'
);

I haven't tested it, it might need a little tweaking to work...

Upvotes: 2

Related Questions