Today I will write a light reading for php cache on html. Here we are talking about caching the output of our code and not as I wrote to cache scripts to opcode level with eAccelerator. So what are we talking about – let's quickly recall the work of php. We apply to web server-a he accepts the parameters that we submit then he submits them to the php script he compiles and spits the result in html version. This is in pretty general terms. What would be our idea here to skip queries, to skip large blocks or not so big blocks by directly drawing the already compiled output. The benefits are obvious – reduced execution time, less workload and resource consumption. In general, it is not the discovery of hot water, nor is it anything that complicated. There are many classes for this purpose such as PHP Pear Cache_Lite which has great functionality but I think in the future to write mine with a much lighter structure and my requirements for caching. We will now consider the most aboriginal variant with Output Control Functions. So let's cache something –
//start cache all output after that will be saved ob_start(); //generate output echo 'Some dynamic output'; echo 'Some other dynamic output ...'; //assign output into variable $var=ob_get_contents(); //close cache output ob_end_flush();
The above code is trivial but let's explain what happened. First we declare from which part of the code the caching starts. Then we generate the output of the code in a standard way. Then the generated output is joined to a variable that will be available later, whether through a file or through sessions, this is your decision.. Finally, we clear and stop caching. Quite a trivial operation if, say, cache generation goes through huge blocks of code so we can save a lot of CPU time by caching for a while or for a session. It's all about what you want, whether it's a public cache or a different user.