APC PHP Cache

 

APC stands for Alternative PHP Cache, and is a free open-source opcode (operation code) caching plugin for PHP. With APC setup on your server, your PHP script executions can be optimized to run more efficiently, by cutting down on needless dynamic PHP executions.

php-apc-cache

Usage:

STEP 1:

Make a class say named “apc_caching.php” and place it in “classes” folder for distinguishing it with other PHP files in your project.

This file has 3 functions defined and a constructor. As you can see, these functions use the default PHP APC functions to fetch, store, check and delete cache data. Now we can use this class to set and get data using APC.

apc1

STEP 2:

Locate a file where you want to use APC Cache. For this you first need to create an object of the class “CacheAPC”, then you can use the “setData” function to set the data using APC.

This is stated in below file.

apc2

STEP 3:

Now to get the data stored in APC cache variable, use “getData” function as stated below.

apc3

STEP 4:

To delete cache, use “delData” function as stated below.

apc4

Reference Link: http://www.script-tutorials.com/how-to-use-apc-caching-with-php/

If you do not have apc extension installed in your wamp, follow below instructions.
    • Find which version of php you have. For this create a page containing phpinfo(); call. Browse to this page to see php version and “Compiler” information. In my case php major version was 5.3 and “Compiler” was “MSVC9 (Visual C++ 2008)”.
      apc5
      Check PHP version and Compiler version
    • Now goto http://dev.freshsite.pl/php-accelerators/apc/sorting/1.html and download the appropriate version of the extension. In my case it was
      “APC 3.1.9 for PHP 5.3 vc9 (Win7/2008)”

      as I had php 5.3.x and MSVC9 as compiler. If you have MSVC6 compiler in phpinfo() than use VC6 files.

    • Now download the file and unzip it and find file similar to
      “php_apc_319_php53_vc9_win7-2008.dll”

      .

    • Now put this file in extension_dir directory. Exact location of this directory can also be found on same php info page.
    • Now we need to tell php to load this extension. This will be done by editing the php.ini file. Be sure to edit correct php.ini file.
    • In my case the file was C:wampbinapacheapache2.2.22binphp.ini. Again the exact location can be found on same php info page under “Loaded Configuration File” head.
    • Now put this line just after the
      “; Windows Extensions”

      section.

      extension=php_apc_319_php53_vc9_win7-2008.dll
    • Please note that you need to put proper name of the file here.
    • Now restart WAMP services. If Apache is started properly it means you have installed APC successfully, other wise try different versions of the extension. The APC section will look something like this.

apc6

  • At this point extension installation is complete.You can increase APC cache limit and change other configurations by putting following information in php.ini
    [apc]
    apc.shm_size = 256M
    apc.enabled = 1
    apc.shm_segments = 1
    apc.max_file_size = 10M
    apc.stat = 1
    [/apc]

 

Check out configuration details for APC here: http://php.net/manual/en/apc.configuration.php

Installing APC Dashboard on WAMP (Windows)

Luckly there is a PHP script already available which allows to see cache usage and cache keys, clear cache manually and some other useful stuff. To install this script do following steps

  • Download latest APC package from http://pecl.php.net/package/APC and extract it.
  • Once extracted find a file named apc.php. Put this script to WAMP root or any other place you like.
  • Open the file apc.php and edit the following lines to modify password for this dashboard.
    defaults(‘ADMIN_USERNAME’,’apc’);
    defaults(‘ADMIN_PASSWORD’,’password’);
  • If you don’t want to put any password for this page, then change value of USE_AUTHENTICATION to 0 in line
    defaults(‘USE_AUTHENTICATION’,1);
  • Once done browsing to apc.php will show stats like this
    apc7
Using the APC dashboard, you can view the cached variables that you might have created across your various PHP projects.

Example:

apc8

NOTE:

Data stored in the APC variable cache via apc_store() is always stored in memory. If you need to store more data than this makes sense for, you’ll need to come up with some other caching solution yourself.

The apc.shm_size configuration directive sets the size of the whole APC shared memory cache, which is used for both opcodes and user variables. If you write more data to the cache than specified here, elements will be dropped from the cache, starting with the least recently used. Your code needs to be able to deal with this — it’s a cache, after all, not a database.

By,
Nainshri Shah

Leave a Reply

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