APC Cache Example: Simple Tutorial of APC Cache With PHP

This tutorial is explained how to save object or array into cache using apc Cache, here i will show you how you can use APC in PHP.
When we are working on huge amounts of database and we need to get huge amount of data from tables.One option is to get each time records from database or other is we hit database only once and save records into cache object and use this object again and again.

Also checkout other tutorial of PHP Cache,

Below tutorial are using APC cache to save object into cache,main benefit of cache objects are remove extra loads from database and improve application performance.

Here we are saving config associative array in cache object, Whenever we need config data first we will look in cache if exits then we will use it otherwise create new cache object and saved config data in cache object.

Key Terms of APC cache:

apc_fetch($onjetName) : This method is used to fetch object from cache.
apc_add('config', $data, 120): Add cache object in APC cache.

Where:

config: target object name
$data: Source array name
120: Time duration when this object expired.

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
if ($data = apc_fetch('config')) {
  echo 'you will see Cached data: ';
} else {
  $data = array(
    'Admins' => array(
      'Admin' => array(
        'name' => 'parvez',
        'site' => 'http://phpflow.com'
      )
    ),
    'users' => array(
      'user' => array(
        'name' => 'John',
        'site' => 'http://phpflow.com'
      )
    ),    
  );
  apc_add('config', $data, 120);
}

// display data
var_dump(apc_fetch('config'))

Result:

apc_cache