Top

Follow me and receive all the latest free scripts:

By Email:

Categories
Most Popular Posts

Simple PHP Cache Tutorial

Published October 07, 2014 by , category PHP

Output CachingPHP CacheCache System

A simple and efficient PHP cache system to speed up your website

Introduction

Imagine you own a high-traffic blog. This blog has category pages. These category pages don't receive new articles every day, or sometimes once or twice a day.

Anyway, these pages obviously don't need to be updated each time a visitor comes and refresh the page. The idea is to cache the pages and actualize them 1 time per day, or one time per hour, depending how often you add content in your blog, to save processing of the same data over and over again.

At each visit, your page will load HTML content instead of running many database connections.

I didn't invent this cache system. Someone shared it with me few years ago, so I am not sure where it comes from. But it's so easy to implement and so efficient that I have to share it with you guys.

Create a "cache" folder

First, create a "cache" folder at the root, name it as you want and set up the correct permissions.

Depending of the size of your website, you may want to create few "cache" folders. A folder for each part of the website for example.

The code

As I said it's very simple! Surround the code you want to cache between these two code block:

<?php
// Cache part 1
$cacheFile="cache/page_name.html"; // folder name & page name

if (@filemtime($cacheFile)<time()-(3600*24)) { // 3600*24 = 1 day (time in seconds)
	ob_start(); // this function will turn output buffering on
// end

// Add you PHP code HERE, including database connections


// Cache part 2
	$contenuCache = ob_get_contents(); // return the contents of the output buffer
	ob_end_flush(); // flush (send) the output buffer and turn off output buffering
	$fd = fopen("$cacheFile", "w"); // open $cacheFile
	if ($fd) {
		fwrite($fd,$contenuCache); // write the new content in $cacheFile
		fclose($fd);
	}
} else {
	include ($cacheFile);
}
// end
?>

Let's say you choose a 1 day cache (3600x24 seconds). Basically, if the file cache/page_name.html is older than 1 day, the system runs your code, create the content again and store it. Else the system display what's in cache/page_name.html (it's the include ($cacheFile)).

Conclusion

Go ahead, cache your pages, it's easy :)

Don't hesitate to create few "cache" folders if you need. Easier if you want to manually delete your cache later, or if you have thousands of files inside.

A unique page_name.html can be created

  1. Or you add the page ID like page_name-$id.html
  2. Or with a $_SERVER['REQUEST_URI'] (cache/$_SERVER['REQUEST_URI'])

About Simon Laroche
Simon Laroche on Google+
Simon Laroche on Twitter
Simon Laroche on Facebook
Simon Laroche on Pinterest
Simon Laroche on LinkedIn
: I am a Coder, Designer, Webmaster and Expert SEO Consulting, I'm also a wise traveller and an avid amateur photographer. I created the website TipoCode and many others such as Landolia: a World of Photos...

If you need help about this script, please leave a comment below. I reply as much as I can depending of my time, you may also get help from others.
I also offer a paid support, if you are in the need to adapt or create a script...

Leave a comment

Comments (0 comment)

No comments for the moment!