tags:

views:

68

answers:

4

hi. i am seeking for guidance from all of u who can tell me about page caching for a website... i am working in php so if anyone can explain me how to perform caching in php.

A: 

Here's a useful link for you, regarding the basics of caching and how to apply that with php.

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

Keep in mind in most cases proper caching should apply earlier (aka the request doesn't even reach the php script).

Björn
+6  A: 

PHP offers an extremely simple solution to dynamic caching in the form of output buffering. The front page of the site (which generates by far the most traffic) is now served from a cached copy if it has been cached within 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 F Y H:i', filemtime($cachefile))." 
     -->n";
     exit;
  }
  ob_start(); // start the output buffer
?>

.. Your usual PHP script 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 has cache technique

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

RSK
+1  A: 

I'm rather surprised that none of the responses so far seem to have addressed the possibility of caching anywhere OTHER than on the server where PHP is running.

There's a lot of functionality within HTTP to allow proxies and browsers to re-use content previously supplied without having to refer back to the origin. So much so that I wouldn't even try to answer this in a S.O. reply.

See this tutorial for a good introduction to the topic.

C.

symcbean