![]() |
||||
|
||||
|
« 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 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:
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 APCAt the top of the "Highlighter" class, I added a couple of lines, to always check if the APC extension is loaded. PHP Code Example:
Using apc_store() and apc_fetch()Next, I had to edit the _loadKeywords() class method to include a few lines. PHP Code Example:
The following line just checks if APC is loaded AND if $this->_keywords is filled with the data. PHP Code Example:
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:
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!
|
GIDNetwork Sites
Archives
Recent GIDBlog Posts
Recent GIDForums Posts
Contact Us
|
« Arctic Cooling NV Silencer 5, Part 3 | Linux on Dell Precision M90 - Part I: Overview » |