What Is APCu? The Beginner's Guide to PHP's User Data Cache

CodexFuse Team · 2026-11-04T18:18:08.447Z · 10 min read min read

What Is APCu, Exactly?

So what is APCu, and why would a beginner care about it? APCu is the PHP extension that lets your code store its own data — query results, API responses, computed values — in a fast in-memory key-value store, so the next request doesn't redo the expensive work. The official PHP manual defines it in one line: "APCu is an in-memory key-value store for PHP."

Keys are strings, values can be any PHP variable, and stored values persist between requests until they are removed, expire, or the PHP worker restarts. One common point of confusion: APCu is not OPcache. OPcache caches compiled PHP bytecode — the machine-readable version of your source files — while APCu caches the data your application itself chooses to remember. You can, and often should, run both.

The "u" in APCu is the whole story. Per the PHP manual, APCu is "APC stripped of opcode caching": the old APC extension did two jobs — opcode caching and user-data caching — but when PHP 5.5 shipped its own bundled OPcache, APC's opcode half became obsolete. The developers forked the user-cache half into APCu (the first codebase was versioned 4.0.0, forked from the head of the APC master branch), and the manual states it supports only "userland caching of variables." The legacy APC package has been dormant since 2012.

APCu vs OPcache: Why Does PHP Need Two Caches?

Because they cache two completely different things. OPcache caches compiled bytecode — the work PHP does parsing and compiling your .php files — while APCu caches your application's own data: the results of that expensive database query, that slow API call, that computed value. One speeds up PHP itself; the other speeds up your code's repeated work.

APCuOPcache
What it cachesYour application's data: query results, API responses, computed valuesCompiled PHP bytecode (opcodes)
You choose what's cachedYes — you call apcu_store() explicitlyNo — automatic once enabled
Ships with PHPNo — a separate extension you installYes — bundled since PHP 5.5
Cleared on restartYes — tied to the PHP worker processYes — rebuilt after a restart
Shared across serversNo — per PHP worker processNo — per process

Every row of this comparison is built from the official PHP manual and the PECL documentation: APCu is "an in-memory key-value store" your code calls into, while OPcache handles bytecode silently. They are complements, not competitors — most performance-tuned PHP servers run both, and our OPcache guide covers the bytecode side in full.

How Does APCu Actually Work?

APCu lives in a shared-memory segment owned by the PHP worker process (php-fpm or mod_php), so reading a value is a memory lookup — there is no network round trip, no socket, no separate server. That is the entire performance story: the fastest possible read is one that never leaves the process.

By default the segment is a single 32 MiB chunk of shared memory. A practical write-up by plumislandmedia notes that holds roughly 4,000 cache slots — enough for many small apps, and tunable upward with the apc.shm_size setting if you need more. Values you store persist between requests — unlike normal PHP variables, which die when the request ends — until you remove them, their TTL expires, or PHP restarts.

Two catches beginners hit: APCu is cleared whenever the PHP master process restarts (it cannot persist data between restarts), and with the default apc.enable_cli=0 setting, a CLI script's cache is invisible to the web server's PHP and vice versa. Also note that arrays and objects are serialized automatically, but resources such as database connections and file handles cannot be cached — cache the query result, not the connection (per reintech.io).

How Do You Install APCu?

Installing APCu takes about five minutes on a typical Linux server. The package name on PECL is APCu — the old apc package is the dead legacy one, so don't install that.

  1. Check your PHP version with php -v, because the apt package name tracks the version — for example php8.2-apcu on Ubuntu, or php-pecl-apcu on RHEL-based systems.
  2. Install the extension. On Debian/Ubuntu: sudo apt install php-apcu (the meta-package picks the right versioned package). On RHEL/CentOS/Fedora: sudo dnf install php-pecl-apcu. Or build from source with pecl install apcu — note that PECL itself is deprecated in favor of PIE (the PHP Installer for Extensions), per the PECL changelog page.
  3. Enable it by adding extension=apcu.so to your php.ini (apt installs usually handle this for you).
  4. Restart your PHP worker — sudo systemctl restart php8.2-fpm, adjusting for your version — so the new extension loads.
  5. Verify it loaded with php -m | grep -i apcu. One heads-up from plumislandmedia: apt-shipped versions trail upstream — Ubuntu 22.04 installs 5.1.21, while the current upstream release as of 2026-11-04 is 5.1.28, which added PHP 8.4 compatibility in 5.1.24 and build fixes against PHP 8.6 in 5.1.28 (per the PECL changelog).

If you run PHP on your own VPS hosting, these steps work as written; on cPanel shared hosting, APCu is usually just a checkbox under the PHP modules section.

Your First APCu Cache: Storing and Fetching Data

Everything you need fits in five functions. apcu_store() writes a value with an optional TTL in seconds (0 means "keep until removed or restart"), and apcu_fetch() reads it back — with a $success flag that tells you whether it was a real hit, which matters because a cached false also returns false.

<?php
// Store for 5 minutes (300 seconds)
apcu_store('homepage_stats', $stats, 300);

// Fetch — $success distinguishes a miss from a cached false
$stats = apcu_fetch('homepage_stats', $success);
if (!$success) {
    $stats = expensive_query();
    apcu_store('homepage_stats', $stats, 300);
}

// apcu_add() only stores if the key does NOT exist yet
apcu_add('rate_limit_192_0_2_1', 0, 60);

// Atomic counter — safe even with many requests at once
$views = apcu_inc('page_views_home', 1, $success);

// Delete one key, many keys, or everything under a prefix
apcu_delete('homepage_stats');
apcu_delete(new APCUIterator('#^myprefix_#'));

// Check existence: true/false for one key, array of hits for many
apcu_exists('homepage_stats');

These signatures come straight from the PHP manual: apcu_store() overwrites an existing key, apcu_add() refuses to (that is the only difference between them), apcu_delete() accepts a single key, an array of keys, or an APCUIterator for regex-based bulk deletion, and apcu_exists() checks one or more keys. For read-modify-write patterns, apcu_entry() fetches or generates a value atomically and apcu_cas() does compare-and-swap — handy when two requests race.

APCu and WordPress: The Object-Cache Story

WordPress has an object cache built in, but by default it is non-persistent: cached data lives only for the duration of the request and vanishes on the next page load. To make it persistent you install a persistent-cache drop-in — a file named object-cache.php in wp-content — and APCu is one of the backends that drop-in can use.

On wordpress.org there are several maintained plugins that do exactly this: APCu Manager (a persistent object-cache backend with analytics, updated 2026-08-18), atec Cache APCu (drop-in object cache plus an optional page cache), Object Cache for APCu – ZapCu (which auto-installs the object-cache.php drop-in for you), and {eac}ObjectCache (which layers APCu shared memory over an SQLite database). W3 Total Cache can also use APCu for page caching, per plumislandmedia's notes. The caveat every one of these carries is the same: APCu is ideal for single-server setups and not recommended for multi-server or clustered environments.

If you're choosing a server stack to speed up WordPress, APCu is the cheapest win on a single VPS: no new service to run, no daemon to monitor — just the extension.

APCu vs memcached: When to Pick Which

APCu's whole pitch is locality: because it lives inside the PHP worker process, reading a value costs a memory lookup instead of a network round trip. An independent benchmark by voxelmanip.se (August 2023) measured 50,000 key reads at 0.005 seconds with APCu, versus 0.691 seconds over memcached TCP, 0.030 seconds over UDP, and 0.594 seconds over a Unix socket — writes showed the same pattern. The benchmark is small and self-run, but it demonstrates the structural point: in-process always beats over-the-wire.

APCumemcached
Speed for PHPFastest — no network round tripA network hop (TCP, UDP, or socket)
Shared across serversNo — per PHP worker processYes — clusterable across servers
Survives a PHP restartNoYes — separate process
Language-agnosticNo — PHP onlyYes
SetupInstall a PHP extensionRun a separate server

The takeaway, per voxelmanip.se: pick APCu when one PHP server does the work and speed is everything. The moment your app needs two web servers sharing one cache, you have outgrown APCu — that is exactly the job memcached was built for, being "clusterable across servers" and language-agnostic.

Five Beginner Mistakes to Avoid

1. Expecting the CLI to see the web cache. With the default apc.enable_cli=0, a cron script and your website get separate caches — a cache warmer run from cron won't populate the cache your visitors read.

2. Skipping key prefixes on shared hosting. Plumislandmedia warns that a single APCu cache is shared by every application on the same web server — two apps both caching a key named config will collide. Prefix every key with your app name (myapp_config), and remember apcu_clear_cache() wipes everyone's entries.

3. Treating APCu as persistent storage. It is cleared on every PHP restart — it is a cache, not a database. Anything you can't afford to lose belongs elsewhere.

4. Caching resources. Arrays and objects serialize fine, but database connections and file handles cannot be cached (reintech.io). Cache the query result, not the connection.

5. Storing secrets on a shared PHP-FPM pool. Per plumislandmedia, any PHP process on the pool can enumerate every APCu entry — key prefixes don't stop that. Sensitive values don't belong in APCu on shared hosting.

Sources

  • PHP Manual — "APCu - Manual" (book.apcu.php): official definition, version history, function reference, and configuration defaults — https://www.php.net/manual/en/book.apcu.php
  • PECL APCu package changelog: release history including 5.1.24 (PHP 8.4 compatibility) and 5.1.28 (current as of 2026-11-04) — https://pecl.php.net/package-changelog.php?package=APCu
  • reintech.io — "A Guide to the PHP APCu Library for Caching and Performance Optimization": install routes, config examples, serialization notes — https://reintech.io/blog/guide-php-apcu-library-caching-performance-optimization
  • plumislandmedia.net — "APCu in php — some notes" (published 2025-08-11, updated 2026-07-31): practical install notes, shared-pool cautions, W3 Total Cache note — https://www.plumislandmedia.net/programming/php/apcu-in-php-some-notes/
  • voxelmanip.se — "Memcached vs. APCu" (2023-08-15): independent benchmark, restart and clustering trade-offs — https://voxelmanip.se/2023/08/15/memcached-vs-apcu/
  • wordpress.org plugin directory: APCu Manager, atec Cache APCu, Object Cache for APCu – ZapCu, {eac}ObjectCache — https://wordpress.org/plugins/
  • Wikipedia — "Alternative PHP Cache": APC naming history and 2012 dormancy — https://en.wikipedia.org/wiki/Alternative_PHP_Cache

Frequently Asked Questions

Is APCu the same as OPcache?

No. OPcache caches compiled PHP bytecode and has shipped with PHP since 5.5; APCu caches your application's own data and is a separate extension you install. APCu is literally "APC stripped of opcode caching" — the opcode half of the old APC extension became unnecessary when PHP bundled OPcache.

Do I need APCu if I already have OPcache?

Probably yes, if your app repeats expensive work. OPcache makes PHP itself faster, but it won't remember your database query results, API responses, or computed values — that is APCu's job. Most performance-tuned PHP servers run both.

Does APCu work with PHP 8.4?

Yes — APCu 5.1.24 added PHP 8.4 compatibility, and 5.1.28 is the current release as of 2026-11-04 (per the PECL changelog). Check pecl.php.net for anything newer before installing.

Can I use APCu on multiple web servers?

No. APCu lives inside the PHP worker process on one machine and cannot share data across servers — every object-cache plugin's docs say it is "not recommended for multi-server/clustered environments." If you need that, use a network cache store like memcached.

Is APCu still maintained?

Yes — 5.1.28 is current and recent releases include PHP 8.4 and 8.6 build support. It is the old APC package that is dead: no releases since September 2012. If you have legacy code calling the old apc_*() functions, the PECL apcu_bc package (stable 1.0.5) provides a backwards-compatible API over APCu.

Featured on Twelve Tools Hunt for Tools Featured on LaunchBuff Launch Llama Tools Launch your MCP Top Web Development Companies in the US - OnToplist.com Designer Listings Listed on SeeWhatNewAI