Page caching using php

I am looking for guidance from anyone who can tell me about page caching for a website ... I work in php, so if anyone can explain me how to do caching in php.

+6
php caching
source share
3 answers

PHP offers an extremely simple solution for dynamic caching in the form of output buffering. The front page of the site (which currently generates the largest traffic) is now served from a cached copy if it has been cached in the last 5 minutes.

<?php $cachefile = "cache/".$reqfilename.".html"; $cachetime = 5 * 60; // 5 minutes // Serve from the cache if it is younger than $cachetime if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) { include($cachefile); echo "<!-- Cached ".date('jS FYH:i', filemtime($cachefile))." -->n"; exit; } ob_start(); // start the output buffer ?> .. Your usual PHP  and HTML here ... <?php // open the cache file for writing $fp = fopen($cachefile, 'w'); // save the contents of output buffer to the file fwrite($fp, ob_get_contents()); // close the file fclose($fp); // Send the output to the browser ob_end_flush(); ?> 

This is a simple cache type,

you can see it here

http://www.theukwebdesigncompany.com/articles/php-caching.php

You can use smarty to cache

http://www.nusphere.com/php/templates_smarty_caching.htm

+7
source share

I am rather surprised that none of the answers so far has considered the possibility of caching somewhere OTHER than on a server running PHP.

There are many features in HTTP that allow proxies and browsers to reuse previously provided content without having to reference the source. So much so that I won’t even try to answer it in SO Reply.

See this tutorial for a good introduction to the topic.

FROM.

+1
source share

Here is a useful link for you regarding the basics of caching and how to apply this with php.

http://www.devshed.com/c/a/PHP/Output-Caching-with-PHP/

Keep in mind that in most cases, proper caching should be applied earlier (otherwise the request will not even reach the php script).

0
source share

All Articles