Php

Simple Tutorial of APC Cache With PHP

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.

Related Post

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 name
120: 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)

Recent Posts

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

2 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

2 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

2 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

3 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

3 months ago

Common Practices for Laravel Blade Template

in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More

3 months ago

Categories