Welcome Guest, Not a member yet? Register   Sign In
Benchmark class and _output function together with cache enabled (SOLVED)
#1

[eluser]luismartin[/eluser]
I'm using page cache by means of the Output class and I also want to display a small comment at the end of the page so that it displays something like:

Code:
<!-- LT: 0.2111 ms -->

Thus I will be able to see the different loading times of a page both when it's pulled out from cache and when it's rendered, and it will be totally transparent to the user (this is why I'm not using the Profiler class).

The problem is that the following call has to be made outside any view/template (or it will be cached as well):

$this->benchmark->elapsed_time('starting_point', 'end_point');

I tried by using the predefined _output function, but seems that caching has no effect when this function is used, so if I use it, I have no cache.

Any suggestion on how to properly manage this?
#2

[eluser]Aken[/eluser]
Does this help you any?
#3

[eluser]luismartin[/eluser]
Hey Aken!
So I would have to do something like this inside the _output() function, would I?

Code:
function _output($output) {

   if ($this->output->cache_expiration > 0)
   {
       $this->output->_write_cache($output);
       echo $output . '<!-- LT: ' . $this->benchmark->elapsed_time(‘starting_point’, ‘end_point’) . 'ms -->';
   }

}
#4

[eluser]CroNiX[/eluser]
Look at the order that things happen in /system/core/CodeIgniter.php. It checks to see if a cached file exists and hasn't expired. If the file exists and is valid, it displays it and immediately exits CI without even loading the controller. If it doesn't exist or is expired, it executes the controller and generates a new one. So, your _output() function in a controller will only get called if the cached file is not generated yet or has expired.
#5

[eluser]luismartin[/eluser]
[quote author="CroNiX" date="1337719504"]Look at the order that things happen in /system/core/CodeIgniter.php. It checks to see if a cached file exists and hasn't expired. If the file exists and is valid, it displays it and immediately exits CI without even loading the controller. If it doesn't exist or is expired, it executes the controller and generates a new one. So, your _output() function in a controller will only get called if the cached file is not generated yet or has expired.[/quote]

Now I'm confused again. So what's the point in what explained in the link provided by Aken?:

Code:
if ($this->output->cache_expiration > 0)
{
    $this->output->_write_cache($output);
}

Th documentation say to use this within _output, but you say if the requested page is already cached, this code will not be executed. What else to do?

Excuse me if I don't try it right now. I'll have to wait for tomorrow when I'm at the office.
#6

[eluser]CroNiX[/eluser]
Not sure, but it seems the easiest way to solve your problem would be to extend the Output class and just override the _display_cache() and _display() methods to append the string you want to the output so it gets appended regardless if it's a cached file or not. That would work for all controllers and you wouldn't need to add it to each controllers _output() method.

However, doing what you are wanting could mess up some browsers since your code you are adding is outside of the closing </html> tag, producing invalid html.

It would be:
Code:
<html>
<head>
</head>
<body>
  blah blah
</body>
</html><!-- your message outside of the html structure -->
#7

[eluser]luismartin[/eluser]
aha, I understand. Well, what about using preg_replace to insert the message just before the html closing tag?

Edit: I've just recalled the pseudovariable {elapsed_time}
http://ellislab.com/codeigniter/user-gui...utput.html

Would this work in my case?
#8

[eluser]Aken[/eluser]
An HTML comment doesn't need to be inside the <html> tags. You can put it directly at the end of the file if you want; no need for preg_replace();
#9

[eluser]CroNiX[/eluser]
Unless you are using conditional comments meant for IE. It's just best to just put it within the html tags as not all browsers ignore comments (specifically, IE, which can and does use them).
#10

[eluser]luismartin[/eluser]
Notice the text I added to post #6 about the use of the pseudovariable {elapsed_time} Could this fit my needs?




Theme © iAndrew 2016 - Forum software by © MyBB