Skip to content
Home » Using APC(Caching Mechanism) with PHP.

Using APC(Caching Mechanism) with PHP.

What is PHP APC:

  • Alternative PHP Cache, one of the most popular caching mechanism for PHP’s op-code caching.
  • Once activated, it starts caching PHP codes automatically.
  • Stores PHP pages in RAM and reduces hard disk activity.
  • Works nicely with W3 Total Cache plugin for storing Object & MySQL caches.

How a PHP program runs:

Generally, a PHP program is run by the Zend Engine, below are steps to go from a PHP script to the code really used by the Engine, this is commonly known as “opcodes”, representing the function of the code.

  • Step1: Reading the PHP code from the file system and put it into memory.
  • Step2 (plexing): The PHP code inside is converted into tokens or Lexicons.
  • Step3 (Parsing): Tokens are processed to derive at meaningful expressions.
  • Step4 (Compiling): The derived expressions are compiled into opcodes.
  • Step5 (Executing): Opcodes are executed to get the final result.

How APC play with the above steps to work and improve performance:

  • The goal of APC is to bypass the above steps from 1 to 4, caching in a shared memory segment, the opcodes generated and then copies them into the execution process so Zend can actually execute the opcodes.
  • Steps of APC:
    • During module init time ( MINIT), APC uses a map to map file into shared memory.
    • During request time, APC replaces zend_compile_file() with its own my_compile_file() to compile.
    • If APC couldn’t find the file in the cache, APC will call zend_compile_file to compile the file.
    • Get returned opcodes and store them into shared memory which read/write from different Apache child processes.
    • Also, store classes and function tables in shared memory.
    • Copy opcodes into Apache child process memory so Zend can execute the code.

Installation:

  • Widows:

    • Download the windows binary for the PHP version you have installed. http://downloads.php.net/pierre/
    • Save it into the PHP extensions folder.
    • Add extension = php_apc.dll in php.ini file.
    • Restart the web server.
  • Linux:

    • sudo apt-get install php-pear php5-dev apache2-threaded-dev build-essential
    • sudo pecl install apc
    • http://sam.bitmorse.com/posts/install-and-enable-alternative-php-cache-apc-on-ubuntu-12.04-lts/

Web-based User Interface to view the caching status:

  • Download the latest package from http://pecl.php.net/package/apc.
  • Look for the file apc.php as this is the file that displays APC monitoring information.
  • Save the file apc.php to server directory from where it can be accessed from the browser.
  • Open apc.php from the browser which displays options like Refresh Data, View Host Stats, System Cache Entries, Per-Directory Entries, User Cache Entries, Version Check.
  • It displays the total memory used, list of the file accessed for caching etc.

Cache user variable:

  • APC allows to cache variables, arrays, objects, functions, and references, etc.
  • Simple example to cache user variable:
    <?php
    if ($quote = apc_fetch('user_cache')) {
    echo $quote;
    echo " [cached]";
    } else {
    $quote = "This is a cached information";
    echo $quote;
    apc_add('user_cache', $quote, 120);
    }
    ?>

Clear APC cache:

  • If Apache is not restarted periodically, the cache could become fragmented, which can be viewed by visiting apc.php as explained above. So to avoid this, periodically empty the cache.
  • “Clear opcode Cache” option is present on the top right side of the page accessed from apc.php(web based user interface) or below code can be used if want to clear cache using program/code.
    <?php
    if (in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1'))) {
        apc_clear_cache();
        apc_clear_cache('user');
        apc_clear_cache('opcode');
        echo json_encode(array('success' => true));
    } else {
        die('SUPER TOP SECRET');
    }
    ?>
  • After running the above code, Browse apc.php to view cache is cleared and memory usage shows 0%.

Test Performance:

  • From command line go to the path where ab is installed (General in apache’s bin directory)
  • Run the below command by enabling APC and then disabling APC to test the performance.
    (use; before extension=php_apc.dll in php.ini to disable the APC and restart the web server)
    C:xampp/apache/bin > ab -n 1000 -c 5 http://localhost/project/index.php
    NOTE: ApacheBench (ab) is a single-threaded command line program for measuring the performance of HTTP web servers. 1000 is the number of requests with a concurrency level of 5.

1 thought on “Using APC(Caching Mechanism) with PHP.”

Leave a Reply

Your email address will not be published. Required fields are marked *

0 Shares
Tweet
Pin
Share
Share
Share