TAN'S Blog

if you obey all the rules you miss all the fun…

CACHING

Leave a comment

What is caching?

Caching is an area of a computer’s memory devoted to temporarily storing recently used information. The content, which includes HTML pages, images, files and Web objects, is stored on the local hard drive in order to make it faster for the user to access it, which helps improve the efficiency of the computer and its overall performance.

Most caching occurs without the user knowing about it. For example, when a user returns to a Web page they have recently accessed, the browser can pull those files from the cache instead of the original server because it has stored the user’s activity. The storing of that information saves the user time by getting to it faster, and lessens the traffic on the network.

Caching Mechanism:

Drawing21

Very Simple Example to learn caching in PHP:

Step One: Create the top-cache.php file:

We need to create two files. Here’s the first one: Create a new file named top-cache.php and paste the code below in it.

<?php
  $url = $_SERVER["SCRIPT_NAME"];
  $break = Explode('/', $url);
  $file = $break[count($break) - 1];
  $cachefile = 'cached-'.substr_replace($file ,"",-4).'.html';
  $cachetime = 18000; //in seconds

  // Serve from the cache if it is younger than $cachetime
  if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
      echo "<!-- Cached copy, generated ".date('H:i', filemtime($cachefile))." -->\n";
      include($cachefile);
      exit;
  }
  ob_start(); // Start the output buffer
?>

So, what this code does? The first 5 lines create the cached file name according to the current php file. So, if you’re using a file named index.php, the cached file will be named cached-index.html.

Line 6 creates a $cachetime(in seconds) variable which determines the life of the cache. You can experiment by changing the time.

Lines 9 to 13 are a conditional statement which look for a file named $cachefile. If the file is found, a comment is inserted (line 10) and the $cachefile file is included. Then, the exit statement stops the execution of the script and the file is sent to the client brother. Which means that if a static file is found, no php code is interpreted by the server.

Line 14 creates a buffer, if the $cachefile file isn’t found. That’s all for the top-cache.php file.

Step two: Create the bottom-cache.php file:

Now, create a second php file, named bottom-cache.php and paste the code below in it.

 <?php
  // Cache the contents to a file
  $cached = fopen($cachefile, 'w');
  fwrite($cached, ob_get_contents());
  fclose($cached);
  ob_end_flush(); // Send the output to the browser
?>

If a file named $cachefile isn’t found on your server, this code is executed and create the file, so next time the page will be called, the $cachefile static file will be served to the client browser instead of executing the whole PHP file.

Step three: Include cache files on your page
Now that you have created the two necessary files, you simply have to include them on the php page you wish to cache. As you probably guessed, the top-cache.php file must be included in the beginning of your php page and the bottom-cache.php at the end, as shown below:

You can save this page as index.php.

<?php
  include('top-cache.php');

  // Your regular PHP code goes here.
  //You can uncomment the code and test it.
  /*
  for($i=1;$i<=100000;$i++) {
    echo "Hello world";
  }
  */

  include('bottom-cache.php');
?>

Now if you test the cache on a slow page, you’ll be amazed by how faster the page is.

Note: Give full read and write permission to the directory where you will put the above 3 files. For Windows OS no need to give file permission.
Ex: If this is my folder structure in ubuntu():

cac

Then you should give the read and write permission to caching folder.

When you will browse the page after uncommenting the code in step three “http://localhost/caching/index.php” for very 1st time you will see it will take time around 50-70ms. Next time you will see it will show the result in 17 to 25 ms. Its amazing.

Before caching(time taken 63 ms):

before

After Caching(time taken 18 ms):

after

Reference:

http://en.wikipedia.org/wiki/Cache_(computing)

http://www.catswhocode.com/blog/how-to-create-a-simple-and-efficient-php-cache

http://www.citrix.com/glossary/caching.html

/*Hope you will love it*/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s