Welcome Guest, Not a member yet? Register   Sign In
Caching and headers
#11

[eluser]Aquillyne[/eluser]
Any hope of a future update to the CI core that just works headers into the caching by default? Tongue
#12

[eluser]Aquillyne[/eluser]
Okay I've been looking over the code and here's what I think.

I don't need to go as far as the Suffix extension. I just want to cache headers.

I'd need to extend the Output class. But, looking at the code, I'd have to extend a huge amount (which I'm never fond of, because it doesn't make you update-proof). On top of it, I'd be extending a huge amount of code just to access 10 or so lines in 2 functions.

So the Suffix extension is too much, and extending the Output class is too ugly. The solution?

The CI code for the Output class actually reads:

Code:
/**
* Set Header
*
* Lets you set a server header which will be outputted with the final display.
*
* Note:  If a file is cached, headers will not be sent.  We need to figure out
* how to permit header data to be saved with the cache data...
*
* @access    public
* @param    string
* @return    void
*/    
function set_header($header)
{
    $this->headers[] = $header;
}

Yes, even the developers admit this limitation. The solution: shout loud enough for the developers to hear!

The Suffix extension proved that it's not hard to cache headers. CI is also storing the time-stamp inside the file. Just store the headers there too. It takes 10 lines of code.

Please, someone get this to a developer's eye and beg them to work it into the next CI update. It will save me so much trouble, it would make CI better, and it would allow them to remove that embarassing comment... :cheese:
#13

[eluser]Colin Williams[/eluser]
Quote:Please, someone get this to a developer’s eye and beg them to work it into the next CI update. It will save me so much trouble, it would make CI better, and it would allow them to remove that embarassing comment…

Fix it and commit it back to the community. C'mon now.
#14

[eluser]Aquillyne[/eluser]
[quote author="Colin Williams" date="1216632635"]Fix it and commit it back to the community. C'mon now.[/quote]

How? I can do that? Where huh?
#15

[eluser]Colin Williams[/eluser]
Like, how do you fix it, can you fix it, and where do your fix it? Or, how do you commit it, can you commit it, and where do your commit it?

For the latter, the Wiki is a nice place to start, but it might serve the community better to house and document it on your own site with a link to it from the Wiki.
#16

[eluser]Aquillyne[/eluser]
[quote author="Colin Williams" date="1216637532"]For the latter, the Wiki is a nice place to start, but it might serve the community better to house and document it on your own site with a link to it from the Wiki.[/quote]

The latter. But that's just publishing a hack. And hacks are ugly. How can I get my little addition into a core CI release? It's just 10 pretty cool lines...
#17

[eluser]Derek Allard[/eluser]
Post your code please. In order for us to even think about it we need it vetted by all these smart people here, confirmation that it works on PHP 4 and 5, and it follows the CI coding standards. Incidently, "shouting loud enough for the developers to hear" is not nearly as good a solution as discuss and create a fix and vet it through the community.
#18

[eluser]Aquillyne[/eluser]
[quote author="Derek Allard" date="1216657518"]Post your code please. In order for us to even think about it we need it vetted by all these smart people here, confirmation that it works on PHP 4 and 5, and it follows the CI coding standards. Incidently, "shouting loud enough for the developers to hear" is not nearly as good a solution as discuss and create a fix and vet it through the community.[/quote]

Of course. Where shall I post it? Here? Make a new wiki page? Both?
#19

[eluser]Aquillyne[/eluser]
Here's the code changes to the Output class that enable caching of headers.

Wiki entry: Cache headers:

Quote:_write_cache function, around line 300 (surrounding code also shown)

Code:
// Implode all the headers into a newline-delimited string
// String is prepended with a pseudo-header giving the expiration time
$headers = "X-CI-Timestamp: " . (time() + ($this->cache_expiration * 60)) . "\n";
$headers .= implode("\n", $this->headers);

flock($fp, LOCK_EX);
fwrite($fp, $headers . "\n\n" . $output); // This line changed
flock($fp, LOCK_UN);
fclose($fp);
@chmod($cache_path, DIR_WRITE_MODE);

_display_cache function, around line 360 (surrounding code also shown)

Code:
// Seperate headers and cache
list($headers, $cache) = explode("\n\n", $cache, 2);
        
// Get each header as an array item
$headers = explode("\n", $headers);
foreach ($headers as $header)
{
    // Is this header the timestamp pseudo-header?
    if (FALSE === strpos($header, "X-CI-Timestamp: "))
    {
        $this->set_header($header); // No, it's not: set the header
    }
    else
    {
        // Has the file expired? If so we'll delete it.
        $timestamp = substr($header, strlen("X-CI-Timestamp: "));
        if (time() >= trim($timestamp))
        {        
            @unlink($filepath);
            log_message('debug', "Cache file has expired. File deleted");
            return FALSE;
        }
    }
}

// Display the cache
$this->_display($cache); // This line changed
#20

[eluser]beemr[/eluser]
Woah Woah Woah Waittaminute!

This is half of the code from Suffix cache, but it is the most dangerous half.

If the suffix is not used as well as the header, you will overwrite the same cache file with each different mime-type. For example, if you are using your controller structure as the organizing principle behind your views and your assets (as I am), your page at example.com/welcome/index will be calling for a css at example.com/welcome/index.css and a js at example.com/welcome/index.js. Without recognition of a particular suffix for each mime-type, the next time someone links to example.com/welcome/index they will get the cached javascript.

If you refuse to recognize suffixes, then you will require a separate controller for each mime-type. I don't want to see that happen to Codeigniter.

Use Cache headers as a library if you must, but a core change should be DRYer than that.




Theme © iAndrew 2016 - Forum software by © MyBB