How to Use APC Caching with PHP

How to Use APC Caching with PHP

11 131275
APC caching with PHP

APC caching with PHP

PHP APC tutorial. Today I have another interesting article for PHP. We will talking about caching, and practice of using caching in php. I will make review of APC caching and will show you how you can use APC in PHP. We will prepare useful class for working with APC for us and several examples. A little of history: Long ago, when computer networks and Web sites only evolved, most web sites were static (and all data/files stored at disks). After, people invented a ‘database’ to store the data they. This was great innovation, was comfortably hold data in database, not in the files as before.


We started saving into database more and more, even small pieces of data. Years passed, the size of sites grew, and people began to notice that the performance of sites falls under the too frequent reading data from database. And we came up with new optimization way – caching (where we store data in the cache files on the server). Interesting, isn’t it? This looks like ‘Forward, to the past’ :-)

But why it happened? It’s simple – the speed of hard drives has increased, even outgrown the velocity of the databases. At the moment I am talking about MySQL. Possible another types of databases still faster. But in any case, progress is not standing still. Now people have learned to use the server memory for data storage. RAM is much faster than hard disk, and the price of memory falls all the time, so let’s use all these advantages of this.

Live Demo

[sociallocker]

download in package

[/sociallocker]


Step 1. PHP

I made this useful class for you. We will use this class to working with memory using APC caching system.

classes/apc.caching.php

<?
class CacheAPC {
    var $iTtl = 600; // Time To Live
    var $bEnabled = false; // APC enabled?
    // constructor
    function CacheAPC() {
        $this->bEnabled = extension_loaded('apc');
    }
    // get data from memory
    function getData($sKey) {
        $bRes = false;
        $vData = apc_fetch($sKey, $bRes);
        return ($bRes) ? $vData :null;
    }
    // save data to memory
    function setData($sKey, $vData) {
        return apc_store($sKey, $vData, $this->iTtl);
    }
    // delete data from memory
    function delData($sKey) {
        return (apc_exists($sKey)) ? apc_delete($sKey) : true;
    }
}
?>

I prepared here several necessary functions which we will use: getData, setData and delData. Now, lets check first example file:

index.php

<?php
$aData = array(
    'name' => 'table',
    'color' => 'brown',
    'size' => array(
        'x' => 200,
        'y' => 120,
        'z' => 150,
    ),
    'strength' => 10,
);
require_once('classes/apc.caching.php');
$oCache = new CacheAPC();
echo 'Initial data: <pre>'; // lets see what we have
print_r($aData);
echo '</pre>';
if ($oCache->bEnabled) { // if APC enabled
    $oCache->setData('my_object', $aData); // saving data to memory
    $oCache->setData('our_class_object', $oCache); // saving object of our class into memory too
    echo 'Now we saved all in memory, click <a href="ndex2.php">here</a> to check what we have in memory';
} else {
    echo 'Seems APC not installed, please install it to perform tests';
}
?>

In this file you can see that I saving 2 objects in memory: some predefined array and class object. Now, lets check second example file:

index2.php

<?php
require_once('classes/apc.caching.php');
$oCache = new CacheAPC();
if ($oCache->bEnabled) { // if APC enabled
    $aMemData = $oCache->getData('my_object'); // getting data from memory
    $aMemData2 = $oCache->getData('our_class_object'); // getting data from memory about our class
    echo 'Data from memory: <pre>'; // lets see what we have from memory
    print_r($aMemData);
    echo '</pre>';
    echo 'Data from memory of object of CacheAPC class: <pre>';
    print_r($aMemData2);
    echo '</pre>';
    echo 'As you can see - all data read successfully, now lets remove data from memory and check results, click <a href="index3.php">here</a> to continue';
} else {
    echo 'Seems APC not installed, please install it to perform tests';
}
?>

Here we only reading data from memory. And, as we see – all data is successfully read from the memory. Now, lets check last example file:

index3.php

<?php
require_once('classes/apc.caching.php');
$oCache = new CacheAPC();
if ($oCache->bEnabled) { // if APC enabled
    $oCache->delData('my_object'); // removing data from memory
    $oCache->delData('our_class_object'); // removing data from memory
    $aMemData = $oCache->getData('my_object'); // lets try to get data again
    $aMemData2 = $oCache->getData('our_class_object');
    echo 'Data from memory: <pre>'; // lets see what we have from memory
    print_r($aMemData);
    echo '</pre>';
    echo 'Data from memory of object of CacheAPC class: <pre>';
    print_r($aMemData2);
    echo '</pre>';
    echo 'As you can see - all data successfully removed. Great !';
} else {
    echo 'Seems APC not installed, please install it to perform tests';
}
?>

Conclusion

Today, I told you how we can use the server’s memory with APC caching. I hope you have any thoughts on optimizing your website(s). For example you can cache some frequently-used data (for example: information about users on your system, or some settings of website etc.) Good luck in your work!

SIMILAR ARTICLES


11 COMMENTS

  1. Great work! but would be nice if you give us a tutorial on how to install APC in the server in order to use it.. thanks !!

  2. I would greatly appreciate it if you could do this .. I can’t find any tutorial with detailed explanation on the web, has failed to install twice .. and still have not yet APC on my VPS .. :(

  3. How to config the APC Object Cache. and what is the deference between APC Cache and APC Object Cache? is Both are same?

    Thank You..

  4. Hey,
    I wrote a code to sync from mysql database to APC, check it out
    https://github.com/ankur0101/Project-CopyCat—MySQL-to-APC-Synchronization

Leave a Reply