An introduction to caching with php
Member Login:
Article Sender Submissions
 
:. MAIN SERVICES
:. Webmaster Radio Sites

Webmaster Radio

Webmaster Radio, Podcasts for Marketing and SEO Profesionals.

White Label Audio

Providing our clients with fully branded turnkey audio solutions for a number of 'on demand' and 'live' applications. From simple audio commercials, to professionally produced product launches.

Radio Advertising

Buy Category based Ads with Webmaster Radio Display audio ads and banners base on show or show category.

Search Bash

If you haven't been to a search bash party, you're missing out! If you an advertiser help sponsor a Search Bash Event for maximum exposure.

Affiliate Bash

If you haven't been to a Affiliate bash party, you're missing out! If you an advertiser help sponsor a Search Bash Event for maximum exposure.

Free Trade Publications

Looking for Trade Publications, we have tons of them and their all free!

SEO Services

SEO Seek offers you Professional SEO Help Information and SEO Services.



   Web-Site » Development » An introduction to caching with php
An introduction to caching with php
Introduction

In this article I will try to give a view of what is the custom caching with php, why and how we can use it.

In the modern days, most of the sites are database driven. That means that your site is actually an application which retrieves data from a DBMS ( database managment system, eg MySQL) , parses the data and shows the result to the user. Most of these data are usually don't change frequently or don't change at all, and the reason that we use the database is that we can easilly update the site and the content.

A problem that this process creates is the server overhead. Every time we execute a query in the database, the instance of our script will call the DBMS, and then the DBMS will send the results of the query. This is time consuming, and especcially for sites with heavy traffic is a real big problem.

How we can solve this problem?

There are two ways to solve this if you want to make your site faster. First is optimizing the queries, but we will not talk about this at the present article. The second and most valuable is using some kind of custom caching technique.

Custom caching with php

First let me explain the idea behind custom caching. When we have dynamic pages that their data is not updated frequently, we can use a 'system' that will be able to create the page, and then store it for later use. That means that after the page's creation, our application will not run the queries again in order to display the page, but it will show the cached one. Of course this system must be able to keep the cached pages for a time period that we will set.

Let's code it

Here is a simple class that will do the job. Let's see the code first :

Code:


Now let me explain :

function cache()

This is the constructor function of the class. The job of this function is to check if there is a cached file for the page that we want, or it should create it. Here is how this is done :

$this->file = $this->cache_dir . urlencode( $_SERVER['REQUEST_URI'] );

This line creates the file name of our cached page. So the cached file will be something like /path/to/cache/dir/request_uri

if ( file_exists ( $this->file ) && ( fileatime ( $this->file ) + $this->cache_time ) > time() )

Here we check if there is a cached version of this page, and if the file must be recreated because it has expired. If the file is cached, it will show the cached page and the exit. I will explain later why exit. If the cached file must be created this code will be executed :

$this->caching = true;
ob_start();

The first statement indicates to the close() function that it is creating the cache file, and the ob_start() will start buffering the output. The buffer's data will be used later by the close() function to save the cache file.

function close()

This function must be called from the end of your script, and it will do the rest of the job. Actually it is needed only when we are in the process of caching that's why it starts with the statement if ( $this->caching )
Let me explain what is happening here :

$data = ob_get_clean();

Here we get all the data from the output buffer while we unset it, and put the data in the $data variable. The four statements that folow up are showing the data and then write the cache file.

Troubleshooting

This is a very simple class, and the purpose is to learn how you can implement a caching solution for your site. The obligation using this class is that you must use it only in this form :

Code:


If you have code after the $a->close() statement, the class will not work right. This is because of the exit() statement in the cache() function.

Of course you can take this code and make it work for your own needs.

Nick Papanotas is a proffesional web developer since 1998 living in Greece. At 2001 he enstablished his own web development firm. Also he owns some webmaster related sites such as the webdigity webmaster forum and the Topsites sublime directory
Visit Our Site at Sublime directory
:. ARTICLE CATEGORIES
Affiliate
Business
Computers & Internet
Economics
Entertainment
Finance & Accounting
Humanities
Industry Publications
Life Style
Web-Site
Writing


:. Featured Articles

How to Increase Your Website’s Search Engine Ranking

There is a significant competition online for search engine ranking; how does your compete? There are several simple methods that you can implement that will immediately work to improve your business’s search engine ranking.

Even You Can Password Protect a Directory (and a File)

Protecting a directory can seem daunting and technical. But with the right lesson (this one) the right tools and examples, it really is easy - easy enough that even you can do it!

Video conference and PowerPoint in presentation

PowerPoint presentations can be done with the help of videoconference. However use a PowerPoint slide or other PC graphics only if it is actually going to enhance your presentation. It should not be used as a replacement for notes, but just as a pres

Tips on facing your audience in a PowerPoint presentation:

Presenting a perfect PowerPoint presentation is not all that lies in making a perfect presentation. The actual effectiveness of a PowerPoint presentation depends on the ability of the speaker to communicate and involve the audience in the presentatio

Top 10 tips to improve your PowerPoint presentation

A PowerPoint presentation is used as a visual aid when making a presentation. It is easy to learn and use, thereby making it easy to be misused or not to be used effectively. So to prevent this, it is always better to follow these tips when making a


©2008 ArticleSender.com All Rights Reserved.