GIDNetwork > Using APC functions in your PHP scripts
Register
« Arctic Cooling NV Silencer 5, Part 3 Linux on Dell Precision M90 - Part I: Overview »

Using APC functions in your PHP scripts

by: admin - May 12, 2006

Recently, I started modifying my existing PHP scripts to include some Alternative PHP Cache (APC) functions. The list of functions offered by this PHP extension can be found at the bottom of this page, just above the User Contributed Notes.

The ones that I find most useful (at this point) are: apc_store() and apc_fetch().

Let us assume we have some data that we store in a database or some text file. Let's also assume that this data almost never, or rarely, changes e.g. a list of country codes. I have one such script -- the script, a PHP class, handles (syntax) highlighting example CPP/C/C++ codes like the one found on this page: http://www.gidforums.com/t-689.html.

Every time that page is viewed, the "Highlighter" class reads a text file containing a list of KEYWORDS for the supported programming languages, and the text file looks something like this:

Generic Code Example:

[cpp]
;=====================
0	=	#define
1	=	#elif
2	=	#else
3	=	#endif
4	=	#error
5	=	#if
;... etc

Now, let's take a peek at the "Highlighter" class and how it 'loads' these keywords:

PHP Code Example:


<?php

class Highlighter
{
    // ... lines omitted ...

    function _loadKeywords$fullpath_to_file )
    {
        if( !@file_exists($fullpath_to_file) )
            trigger_error'Class: Highlighter cannot find CODE keywords file'E_USER_ERROR );

        $this->_keywords = @parse_ini_file$fullpath_to_filetrue );

        if( empty($this->_keywords) )
            trigger_error'Class: Highlighter KEYWORDS list is empty'E_USER_ERROR );
    }

    // ... lines omitted ...
}
\?>

Since the data in the text file almost never changes, and this class used heavily inside GIDForums, wouldn't it be more efficient to have it (the data) stored in the server's memory instead of having the script read the text file on every page view?

Before today this was quite complicated to do, but now with the APC functions, all it takes is a simple modification to the script! Here's a brief example how I modified the "Highlighter" class to include the APC functions, have the data stored to the computer's memory and have it work more efficiently.

Verify that PHP is extended with APC

At the top of the "Highlighter" class, I added a couple of lines, to always check if the APC extension is loaded.

PHP Code Example:


<?php
// Check if APC extension is loaded
if( !defined('GID_EXTENSION_LOADED_APC') )
    define'GID_EXTENSION_LOADED_APC'extension_loaded('apc') );

class Highlighter
//....
\?>

Using apc_store() and apc_fetch()

Next, I had to edit the _loadKeywords() class method to include a few lines.

PHP Code Example:


<?php

function _loadKeywords$fullpath_to_file )
{
    // only get the data off the text file if we can't get it from
    // APC i.e. from memory
    if( !(GID_EXTENSION_LOADED_APC && ($this->_keywords=apc_fetch('highlighter_keywords'))) )
    {
        if( !@file_exists($fullpath_to_file) )
            trigger_error'Class: Highlighter cannot find CODE keywords file'E_USER_ERROR );

        $this->_keywords = @parse_ini_file$fullpath_to_filetrue );

        if( empty($this->_keywords) )
            trigger_error'Class: Highlighter KEYWORDS list is empty'E_USER_ERROR );
        // if APC is loaded, store the data in memory        
        if( GID_EXTENSION_LOADED_APC )
            apc_store'highlighter_keywords'$this->_keywords );
    }
}
\?>

The following line just checks if APC is loaded AND if $this->_keywords is filled with the data.

PHP Code Example:


<?php
// only get the data off the text file if we can't get it from
// APC i.e. from memory
if( !(GID_EXTENSION_LOADED_APC && ($this->_keywords=apc_fetch('highlighter_keywords'))) )
{ //...
\?>

If both are true, the (combined) test fails and since $this->keywords was true, we can be certain that the data was retrieved from memory! This is the ideal situation, this is what we expect to happen most of the time.

If PHP was not extended with APC, the first test: GID_EXTENSION_LOADED_APC will fail and the method will run as normal (the script will proceed inside the IF() clause), reading the data off the text file.

If GID_EXTENSION_LOADED_APC is true and $this->_keywords is false, the (combined) test will pass and proceed inside the IF() clause, i.e. retrieving the data off the file and at the end:

PHP Code Example:


<?php
// if APC is loaded, store the data in memory        
if( GID_EXTENSION_LOADED_APC )
    apc_store'highlighter_keywords'$this->_keywords );
\?>

it will attempt to store the data to APC/memory. If the store was successful, every other request will fetch the data off apc_fetch()...

Easy? I think so. Efficient? You bet!

Would you like to comment? This story has been viewed 17,897 times.
« Arctic Cooling NV Silencer 5, Part 3 Linux on Dell Precision M90 - Part I: Overview »

__top__

Copyright © GIDNetwork™ 2001 - 2017

Another website by J de Silva

Page generated in : 0.03901 sec.