This tutorial will explain how to save objects or arrays into the cache using PHP APC Cache, here I will show you, How you can use APC Cache in PHP.
The APC is supported till the PHP 5.4 version after that you need to use APCu, I have shared a tutorial How To install and use APCu with PHP.
When we are working on huge amounts of databases and we need to get a huge amount of data from tables.
There is one option is to get each time records from the database otherwise we store data in the cache, We can hit the database only once and save records into cache object and use this object again and again.
Also checkout other tutorials of PHP Cache,
Below tutorial are using PHP APC cache to save objects into the cache, The main benefit of cache objects are removing extra loads from the database and improving application performance.
Here, We are saving config associative array in cache object, Whenever we need config data. First, we will look data into the cache if exits then we will use it otherwise create a new cache object and save config data in the cache object.
Key Terms of PHP APC cache:
apc_fetch($onjetName)
: This method is used to fetch objects from the cache.apc_add('config', $data, 120)
: Add cache object in APC cache.
Where params are:
config
: target object name$data
: Source array name120
: Time duration when this object expired.
Let’s take a sample example for APC Cache
if ($data = apc_fetch('config')) { echo 'you will see Cached data: '; } else { $data = array( 'Admins' => array( 'Admin' => array( 'name' => 'parvez', 'site' => 'https://phpflow.com' ) ), 'users' => array( 'user' => array( 'name' => 'John', 'site' => 'https://phpflow.com' ) ), ); apc_add('config', $data, 120); } // display data var_dump(apc_fetch('config'))
Output:
Using Class
We can also create a separate APC class(apc_caching.php
) and define the setter and getter methods, we can access these methods in other application classes.
Let’s create a CacheAPC class and add the below code into them.
<?php class CacheAPC { var $iTtl = 300; // 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; } } ?>
How To Use CacheAPC class
We can use apc_caching.php
class in the other class. We’ll include the APC class file and create a class instance.
require_once('apc_caching.php'); $oCache = new CacheAPC(); $data = array( 'Admins' => array( 'Admin' => array( 'name' => 'parvez', 'site' => 'https://phpflow.com' ) ), 'users' => array( 'user' => array( 'name' => 'John', 'site' => 'https://phpflow.com' ) ), ); if ($oCache->bEnabled) { // if APC enabled $oCache->setData('config', $data); // saving data to memory } else { echo 'APC not installed, please install it to perform tests'; } if ($data = $oCache->getData('config')) { echo 'You are getting data from Cache: '; } else { echo 'Data is not found in Cache and saving data into APC Cache: '; $oCache->setData('config', $data); // saving data to memory } // display data var_dump($data)