Caching Your Website Content

I’ve always tried to include some varying content on my websites because many people believe it helps your search engine rankings. The logic being that fresh content is likely to be more relevant and get a boost in the SERPS. I don’t know if it’s true or not because I can’t find anything definitive posted by anyone from Google or a similar major search engine. In any event, it seems like a good idea and I’ve been including a small amount of changing content on my website for years. Thinks like the last 5 blog entries or customer testimonials mainly.

That sort of content is database driven so rather than hit your database every time there’s a pageview you should consider creating the content on a regular basis and having your website display the cached information. I do this with PHP and CRON jobs. My PHP script generates the content and writes it to a file. A bit of PHP in the web template includes that file to display the content. The CRON job runs the PHP that generates the content, perhaps hourly, but more commonly, daily.

Here’s what my CRON jobs generally look like:

14 */8 * * * php /srv/www/public_html/cron-scripts/create-blog-links.php

And a skeleton PHP script to generate some content looks something like what I’ve shown below. Note that I echo out the data created because (generally) when your cron jobs are run you’ll get an email from your server displaying the output from the script.

	$now=microtime(true);

        //code to generate content goes here

	$file_name="/path/to/webroot/generated-includes/blog-links.php";
	
	$content=get_content();
	
	if (strlen($content)>0)
	{
		$file_handle=fopen($file_name,'w') or die('cannot open file');

		fwrite($file_handle,$content);
		fclose($file_handle);
	}
	
	echo "create-blog-links.php complete, run time:".number_format(microtime(true)-$now,4)." seconds<br /><br />";
	echo "$content<br />";
	
	
?>

And finally, the include for my web templates that actually show the generated content. Again I do this in PHP.

include('/usr/www/generated-includes/latest-blog-links.php');

I believe this process could be taken one step further (and it’s something I plan on experimenting with) by actually rotating the parts of the static content of a website. I think I’d do this less frequently, perhaps weekly or monthly and you’d want to make sure you have a large pool of static content to rotate in and out.

This entry was posted in php, seo, web servers on by .

About markn

Mark is the owner and founder of Timesheets MTS Software, an mISV that develops and markets employee timesheet and time clock software. He's also a mechanical engineer, father of four, and a lifelong lover of gadgets.