Latest

Archive

Community news

C++

Communities and Content

Databases

Editorials

Emacs

General

HTML

Java

Notices

PHP

XML

Apache

C++

Database

General

HTML

Java

Javascript

Linux

Object oriented programming

Open source

Perl

PHP

Python

Ruby

SOAP

XML

Suggest a link

Advertise on zez

Contribute

Contact us

About zez


Page caching with templates



If you have implemented a PHP application using an object oriented approach and are disappointed with the performance you get, then this article is worth looking at. Here I will explain how you can use templates to cache your web pages and serve pure static pages. Using this technique you can have complex algorithms for rendering articles, or whatever you like, and still get the performance of static pages.

If you are wondering about how templates works please read the article about block templates here. This first code snippet shows how you can implement the page caching mechanism. Here I check if page caching is enabled, if it is disabled the page is just generated as usual. If page caching is enabled the code checks for a cached version. If there exists no cached versions of the page, one is generated.


// read from .ini file 
$PageCaching = "enabled"

// check if caching is enabled
if ( $PageCaching == "enabled" )
{
    $cachedFile = "cache/page.cache";
                    
    if ( file_exists( $cachedFile ) )
    {
        include( $cachedFile );
    }
    else
    {
        createPage( true, $cachedFile );
    }            
}
else
{
    createPage( false );
}



| < 1 > | 2 | Next page >> | Printer-friendly page |

Comment List


There are no comments.


Forgot your password?

Register a new user

Results

Polls