How to Install and Use PHP APCu for High‑Performance Caching
This guide explains what PHP APCu is, its key features, step‑by‑step installation, configuration, basic usage examples, and a performance comparison with Redis, helping developers boost PHP application speed with shared‑memory caching.
Overview
PHP APCu (Advanced and Performance Caching User Cache) is a shared‑memory cache for PHP 5.5+ that provides a user‑level caching mechanism. It is a branch of APC without OPcache.
Features
Shared‑memory cache : multiple PHP processes can access the same cached data, improving performance.
User cache : stores session data and application‑level caches, not compiled PHP code.
Simple API : functions such as apcu_store(), apcu_fetch(), apcu_add(), and apcu_delete() handle cache operations.
Performance boost : reduces database queries and file I/O by caching frequently accessed data.
Memory management : automatically evicts old entries when memory is low.
Process isolation : cache data is isolated per PHP process, enhancing security.
Configuration : parameters like cache size and cleanup policy are set in php.ini.
Installation
Download and extract the source package:
wget https://pecl.php.net/get/apcu-5.1.23.tgz
tar -zxvf apcu-5.1.23.tgzPrepare the build environment:
cd apcu-5.1.23
/usr/local/php-7.4/bin/phpizeConfigure the extension (use sudo if permission errors occur):
./configure --with-php-config=/usr/local/php-7.4/bin/php-configCompile and install:
sudo make -j4
sudo make installVerify that apcu.so appears in the PHP extensions directory, then enable it in php.ini:
[apcu]
extension=apcu.so
apc.shm_size=1024MCheck the installation:
php -i | grep apcuSimple Usage
Example script that writes and reads 10,000 keys:
<?php
$start = microtime(true);
for ($i = 0; $i < 10000; $i++) {
$key = 'apcu' . $i;
apcu_add($key, $i); // fails if key exists, similar to Redis SETNX
apcu_fetch($key); // returns false if not found
}
echo microtime(true) - $start . PHP_EOL;Typical runtime on a test machine is around 0.001 seconds.
Performance Comparison with Redis
Read‑only (10 k ops): APCu 0.011 s vs Redis 1.162 s
Write‑only (10 k ops): APCu 0.012 s vs Redis 1.062 s
Read/Write with a single Redis connection: APCu 0.011 s vs Redis 2.117 s
Read/Write with multiple Redis connections: APCu 0.011 s vs Redis 3.646 s
The benchmark shows that APCu provides dramatically lower latency for in‑process caching compared with Redis, making it suitable for fast local data storage in PHP applications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
