Over a million developers have joined DZone.

APC caching with PHP

· Web Dev Zone

Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud, brought to you in partnership with IBM.

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

We started saving into the 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 were falling under the too frequent reading of data from the database. And we came up with a new optimization strategy – 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 did it happen? 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. Other types of databases are even 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.

Today I have prepared an example on using APC with PHP.

Live Demo

download in package


Now – download the source files and lets start coding !


Step 1. PHP

I made this useful class for you. We will use this class to work with memory using an 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) {
        $bRes = false;
        apc_fetch($sKey, $bRes);
        return ($bRes) ? apc_delete($sKey) : true;
    }
}

?>

I prepared here several necessary functions which we will use: getData, setData and delData. Now, lets check the 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 am 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 are 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';
}

?>
Live Demo
download in archive

Conclusion

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




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

Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud, brought to you in partnership with IBM.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}