Cesar D. Rodas, web development. Technology news. PHP, MySQL, Apache, C, Bash, ASM

gCache: Helping PHP to work faster

Many php sites usually do many SQL query for show a single page. That doesn’t affect the performance when the traffic is under the 10000 hits per day.

But the think became more hard to handle when the traffic is upper than 10000 hits, and more if the web server is a shared server.

Usually we need to find a way to help php to reduce the number of query to a database, because as a friend told me: “Most of time php is awaiting for a database to response”. And that is wasted resource (time, processor, main memory). The developer need to find a way to store some pages until it changes or something like that.

There are many cache classes over there, but exist many problems, I will focus on two problems. Implementation and concurrence.

Implementation cost (time, resource and money), and usually the cache data need to be redirected to a variable for the cache construction.

Concurrence, this is not a problem, most of cache over there implements a POSIX locking or PHP-locking which is compatible with almost all OS except with Windows with FAT hard disk. So there is a problem with compatibility across platforms that PHP offer.

Here is where I introduce my gCache class.

This class implements and easy API for cache generation. Actually it handle the PHP stdout buffer it, cache and prints. The next time gCache class will read and retrieve the cache.

Let me show you how gCache class works.


<?php

include(“gCache.php”);

$cache = new gCache;

$cache->folder = “./cache/”;

$cache->contentId=“var45″;

$cache->timeout = 1;

if ($cache->Valid()) {

echo $cache->content;

} else {

$cache->capture();

?>

<html>

<head>

<title>Cached page</title>

<head>

<body bgcolor=”#CCCCCC”>

<h1>Testing Cesar D. Rodas’ gCache Class</h1>

<hr>

<h2>Example of how to cache a hole page</h2>

<hr>

<p>Basically what the gCache do, is to store a web-page or a portion of it into

a<em> <em>cache file</em>. </em>The <em><em>cache file</em></em> has a $timeout

in second of cache vitality, after that the cache will be re-created.</p>

<p>Also this class provides and locking system which is not depending of POSIX

or other OS, this feature becomes to this class very portable.</p>

<hr>

<font size=”1″>This cache page was generated at

<?php echo date(“Y/m/d H:i:s”)?>

</font><font size=”1″> by <a href=”http://cesars.users.phpclasses.org/gcache”>gCache</a>

</font>

</body>

</html>

<?

$cache->endcapture();

}

?>

The first think is include the class file, the create and object.

$cache = new gCache;

Then set an folder for save cache, In this example is a web public folder, but it shouldn’t be one.

$cache->folder = “./cache/”;

And contentID, it must be unique

$cache->contentId=“var45″;

The cache Life-time (in minutes)

$cache->timeout = 1;

Then see if the cache is valid. If it is just print the content, if it is not. call the method capture before your page, and endcapture method after your page content.

if ($cache->Valid()) {

echo $cache->content;

} else {

$cache->capture();

HTML page also here you could put other PHP code, as in the example of above.

$cache->endcapture();

}

gCache class also implements the PHP-Lock and a rudimentary locking system for Windows, which has not a great performance but it works.

And also because it capture the direct output it has a simplest implementation than save the output into a variable.

The gCache class is public domain. So you are free to download, use it,modify it.

The source of gCache class is yours!

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages. Digg del.icio.us StumbleUpon Technorati BlinkList Furl NewsVine Reddit

7 Responses to “gCache: Helping PHP to work faster”

  1. PHPDeveloper.org Says:

    Cesarodas.com: gCache: Helping PHP to work faster…

  2. programame.net Says:

    gCache: Ayudando al PHP a trabajar mejor…

    Este entrada de blog habla d una clase gratuita que te permite manejar Cache de contendios HTML. A diferencias de otros existentes, esta clase se diferencia de las otras por su fácil implementación y porque sus sistema de bloqueo de archivo que hasta…

  3. www.webeame.net Says:

    gCache: Ayudando al PHP a trabajar mejor…

    Este entrada de blog habla d una clase gratuita que te permite manejar Cache de contendios HTML. A diferencias de otros existentes, esta clase se diferencia de las otras por su fácil implementación y porque sus sistema de bloqueo de archivo que hasta…

  4. Noticias externas Says:

    gCache: Ayudando al PHP a trabajar mejor…

    Este entrada de blog habla d una clase gratuita que te permite manejar Cache de contendios HTML. A diferencias…

  5. Josep Maria Aribau Says:

    http://www.tufuncion.com/cache-php
    Must easier!

  6. admin Says:

    I think http://www.tufuncion.com/cache-php won’t work always.
    If for example you have a very busy site, and supose that two peoples are accesing to the site at the sime, and the cache is not valid any more, both process will create the cache at same time, and your cache will not be valid.
    For that my class has a File Locking system.

    And also this class work not only for php5.

  7. Nadeem Says:

    Hi
    i was asking different forums for this kind of cache system and finally i got ur code.
    I was delighted.
    i tried to implemented the code on my site which has a phpBB forum installed.
    I installed and tested the script locally and it worked very well.
    But i was shocked to know that the script which is wrking locally in not working on the server at all :(
    my local machine is a windows XP professional with IIS, PHP 4.4.4 and MySQL %
    My server is Linux with Apache, PHP 4.3.11 and MySQL
    No error message is generated.
    the page is generated each time it is requested but no cache file is generated.
    i tried to place the test1.php file included with the gCache class. to my surprise, this page is being cached and served from cache until the cache is valid.
    Can u please help me find out whats missing ?
    Thanks

Enter your email address:

Delivered by FeedBurner

this Site

Archives

June 2007
S M T W T F S
« May   Jul »
 12
3456789
10111213141516
17181920212223
24252627282930

Syndication

Google