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

?>
 

Saturday, September 28, 2013

what is ACID use in PHP??

Database ACID (Atomicity, Consistency, Isolation, Durability) Properties

There are a set of properties that guarantee that database transactions are processed reliably, referred to as ACID (Atomicity, Consistency, Isolation, Durability).

Atomicity

Atomicity refers to the ability of the database to guarantee that either all of the tasks of a transaction are performed or none of them are. Database modifications must follow an all or nothing rule. Each transaction is said to be atomic if when one part of the transaction fails, the entire transaction fails.

Consistency

The consistency property ensures that the database remains in a consistent state before the start of the transaction and after the transaction is over (whether successful or not). For example, in a storefront there is an inconsistent view of what is truly available for purchase if inventory is allowed to fall below 0, making it impossible to provide more than an intent to complete a transaction at checkout time. An example in a double-entry accounting system illustrates the concept of a true transaction. Every debit requires an associated credit. Both of these happen or neither happen.
A distributed data system is either strongly consistent or has some form of weak consistency. Once again, using the storefront example, a database needs to provide consistency and isolation, so that when one customer is reducing an item in stock and in parallel is increasing the basket by one, this is isolated from another customer who will have to wait while the data store catches up. At the other end of the spectrum is BASE (Basically Available Soft-state Eventual consistency).
Weak consistency is sometimes referred to as eventual consistency, the database eventually reaches a consistent state. Weak consistency systems are usually ones where data is replicated; the latest version is sitting somewhere in the cluster, older versions are still out there. Eventually all nodes will see the latest version.

Isolation

Isolation refers to the requirement that other operations cannot access or see the data in an intermediate state during a transaction. This constraint is required to maintain the performance as well as the consistency between transactions in a database. Thus, each transaction is unaware of another transactions executing concurrently in the system.

Durability

Durability refers to the guarantee that once the user has been notified of success, the transaction will persist, and not be undone. This means it will survive system failure, and that the database system has checked the integrity constraints and won't need to abort the transaction. Many databases implement durability by writing all transactions into a transaction log that can be played back to recreate the system state right before a failure. A transaction can only be deemed committed after it is safely in the log.
Durability does not imply a permanent state of the database. Another transaction may overwrite any changes made by the current transaction without hindering durability.