Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,471
There are 1970 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

An introduction to caching with php

Thread title: An introduction to caching with php
Closed Thread    
    Thread tools Search this thread Display Modes  
12-11-2005, 02:34 PM
#1
Nikolas is offline Nikolas
Status: I'm new around here
Join date: Dec 2005
Location: Greece
Expertise:
Software:
 
Posts: 21
iTrader: 0 / 0%
 

Nikolas is on a distinguished road

  Old  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 :

PHP Code:
<?php
class cache
{
    var 
$cache_dir './tmp/cache/';//This is the directory where the cache files will be stored;
    
var $cache_time 1000;//How much time will keep the cache files in seconds.
    
    
var $caching false;
    var 
$file '';

    function 
cache()
    {
        
//Constructor of the class
        
$this->file $this->cache_dir urlencode$_SERVER['REQUEST_URI'] );
        if ( 
file_exists $this->file ) && ( fileatime $this->file ) + $this->cache_time ) > time() )
        {
            
//Grab the cache:
            
$handle fopen$this->file "r");
            do {
                
$data fread($handle8192);
                if (
strlen($data) == 0) {
                    break;
                }
                echo 
$data;
            } while (
true);
            
fclose($handle);
            exit();
        }
        else
        {
            
//create cache :
            
$this->caching true;
            
ob_start();
        }
    }
    
    function 
close()
    {
        
//You should have this at the end of each page
        
if ( $this->caching )
        {
            
//You were caching the contents so display them, and write the cache file
            
$data ob_get_clean();
            echo 
$data;
            
$fp fopen$this->file 'w' );
            
fwrite $fp $data );
            
fclose $fp );
        }
    }
}


//Example :
$ch = new cache();
echo 
date("D M j G:i:s T Y");
$ch->close();
?>
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 :

PHP Code:
<?php
 $a 
= new cache();
 ....
 ....
 ....
 
$a->close();
?>
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.

A quick solution is to remove the exit() statement in the cache() function and then use the class this way :

PHP Code:
<?php
 $a 
= new cache();
 if ( 
$a->caching )
 {
 ....
 ....
 ....
 }
 
$a->close();
?>

* You can publish this article to your site, but only if you give back credit, and a link to http://www.webdigity.com/ Thanks

12-11-2005, 02:48 PM
#2
Salathe is offline Salathe
Salathe's Avatar
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
 
Posts: 3,820
iTrader: 25 / 100%
 

Salathe will become famous soon enough

Send a message via MSN to Salathe

  Old

Interesting simple article, have you considered using register_shutdown_function rather than relying on the user using the cache::close method?

12-11-2005, 03:05 PM
#3
Nikolas is offline Nikolas
Status: I'm new around here
Join date: Dec 2005
Location: Greece
Expertise:
Software:
 
Posts: 21
iTrader: 0 / 0%
 

Nikolas is on a distinguished road

  Old

Yes it could help, but the purpose of this article is to be an introduction on caching, and I made it as simple as it could be.

You can made a lot of changes to that code to make it more proffesional.

12-11-2005, 10:37 PM
#4
Salathe is offline Salathe
Salathe's Avatar
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
 
Posts: 3,820
iTrader: 25 / 100%
 

Salathe will become famous soon enough

Send a message via MSN to Salathe

  Old

Excuse me, but I don't understand what you mean by "more proffesional". How does one rate the "proffesionalness" of a PHP code snippet?

Anyway, in my view it would be much simpler to have the close method called automatically (by default, with the option to turn it off for custom close positions) than relying on the developer to always place it at the end of the script. Plus, the person learning from the article could then see where and why the register_shutdown_function function is useful.

What could be simpler than adding $Cache = new Cache(); to the top of the page and leaving it at that?

12-12-2005, 07:55 AM
#5
Nikolas is offline Nikolas
Status: I'm new around here
Join date: Dec 2005
Location: Greece
Expertise:
Software:
 
Posts: 21
iTrader: 0 / 0%
 

Nikolas is on a distinguished road

  Old

1) More proffessional = The script would be able to cache dynamic pages.

2) I am not using the automatically close way, because this way you wouldn't be able to use dynamic content in your pages after you call the caching class.

12-13-2005, 12:15 AM
#6
DateinaDash is offline DateinaDash
Status: The BidMaster
Join date: Nov 2004
Location: England
Expertise:
Software:
 
Posts: 10,821
iTrader: 0 / 0%
 

DateinaDash is on a distinguished road

  Old

Thanks for the article Nikolas but I think it would be more appropriate in the programming forum

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed