Sunday, September 29, 2013

how to use APC example cache file in php


APC caching with PHP

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 much faster than hard disk, and the price of memory falls all the time, so let’s use all these advantages of the?

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


Now –download in package
the source files and lets start coding !

Step 1. PHP

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

<?

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 first example file: 
 <?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: 

<?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:
<?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';
}

?>
 

No comments:

Post a Comment