Improve PHP Application Speed By Using APCu
Improve PHP Application Speed By Using APCu
Advertisement
CodeCanyon Advertisement

The Alternative PHP Cache (APC) is a free and open opcode cache for PHP. Its goal is to provide a free, open, and robust framework for caching and optimizing PHP intermediate code. This extension is considered unmaintained and dead. The new alternative is called APCu.

APCu is APC stripped of opcode caching. The first APCu codebase was versioned 4.0.0, it was forked from the head of the APC master branch at the time. PHP 7 support is available as of APCu 5.0.0.

APCu is used to keep copies of data in memory, then reducing the time required to make queries, data filtering, data processing, etc. Many frameworks like WordPress supports APCu caching by adding caching plugins such as W3 Total Cache, or you can make your own custom code to use it the way you want.

Installation

On Linux systems such as Centos, you can install it via the Yum package manager according to php version

On Microsoft Windows with XAMPP installed, you can enable it by adding the dll extension file; you can download it by going to this link, and appending the following settings to php.ini file:

In both cases, you need to restart Apache for the changes to take effects. Below is a short video tutorial for XAMPP PHP APCu installation and configuration.

How to use APCu

APCu is very simple to use, you can find full details by navigating to this link at php.net.
The following is a code sample, which you can use to make sure that APCu is working fine after installation:

If you are going to use APCu by creating a custom code, you need to create a mechanism for updating data stored in the cache by identifying the stored data with a timestamp in addition to the already defined key. You have to make a timestamp for the cached data and a timestamp for the latest CRUD operation related to the same data, so that you compare them everytime you invoke data to update cached data in case of the existence of old timestamps.

PHP Simple Caching Manager

I have written a simple static php class to use the APCu cache. You can reach it by clicking here. It provides the following static functions that you can use them directly without instantiating an object:

  • Store($key, $value): to store data value by any string key
  • Get($key) : to get the stored data
  • Exists($key): to check if data exists
  • Reset($key): to reset a specific data

In the next weeks, I am going to provide a timestamp mechanism with encryption for easy deployment.

LEAVE A REPLY