Home > Uncategorized > Implement Cache using PHP

Implement Cache using PHP

Cache is a very simple concept that can be used in a various web applications . A cache can be used for storing database queries for later use, to store rendered pages to be served again without generating them again, or to save indexed pages in a crawler application to be processed by multiple modules.

here will see how to implement Cache into your website

First of all, if you’re using sessions, you must disable session_cache_limiter (by setting it to noneor public). Headers it sends are the worst case of voodoo programming I’ve ever seen.

session_cache_limiter('none');

Then send Cache-Control: max-age=number_of_seconds and optionally equivalent Expires:header.

header('Cache-control: max-age='.(60*60*24*365));
header('Expires: '.gmdate(DATE_RFC1123,time()+60*60*24*365));

To get best cacheability, send Last-Modified header and reply with status 304 and empty body if browser sends If-Modified-Since header.

This is cheating a bit (doesn’t verify the date), but is valid as long as you don’t mind browsers keeping cached file forever:

if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
   header('HTTP/1.1 304 Not Modified');
   die();
}
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: