← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

APCu — In-Process User Cache

performance PHP 5.4+ Intermediate

Also Known As

APCu APC user cache PHP in-memory cache

TL;DR

APCu stores PHP values in shared memory within the PHP-FPM pool — the fastest possible cache with no network hop overhead.

Explanation

APCu (APC User Cache) provides a key-value store in shared memory segments accessible to all PHP-FPM worker processes on a single server. With no serialisation over a network socket, APCu reads are extremely fast — nanoseconds vs microseconds for Redis. Ideal for: parsed application configuration, compiled route tables, permission trees, and any rarely-changing but frequently-read data. Limitations: data is local to one server (multi-server deployments need a distributed cache for shared state) and all data is lost on PHP-FPM restart. Use apcu_store(), apcu_fetch(), apcu_exists(), and apcu_delete(). Check memory usage with apcu_sma_info().

Diagram

flowchart TD
    REQ[PHP Request] --> CHECK{APCu cache<br/>has key?}
    CHECK -->|HIT| RETURN[Return cached value<br/>O of 1 microseconds]
    CHECK -->|MISS| DB[(Database or API)]
    DB --> STORE[Store in APCu<br/>shared memory]
    STORE --> RETURN
    subgraph Shared Memory
        W1[Worker 1] & W2[Worker 2] & W3[Worker 3]
        W1 & W2 & W3 <-->|all share| MEM[(APCu<br/>in-process cache<br/>per server)]
    end
    INFO[No network hop<br/>Same process memory<br/>Lost on restart]
style RETURN fill:#238636,color:#fff
style MEM fill:#1f6feb,color:#fff

Common Misconception

APCu cache is shared across all web requests automatically. APCu is per-process in PHP-FPM — data cached in one worker process is not visible to others unless you use a shared cache like Redis or Memcached. APCu is suitable for per-process memoization, not cross-request shared state.

Why It Matters

APCu is PHP's in-process user cache — it stores computed values in shared memory across requests on the same server, eliminating redundant computation with near-zero latency.

Common Mistakes

  • Using APCu in a multi-server environment expecting shared state — APCu is per-server, not distributed; use Redis for that.
  • Not setting a reasonable TTL — stale data lives forever without expiry.
  • Not checking apcu_fetch() return value — it returns false for a cache miss, which is falsy and can be confused with a cached false value; use the success parameter.
  • Caching objects that hold database connections or file handles — those are not serializable.

Avoid When

  • Multi-server deployments — APCu is per-process/per-server and does not share data across nodes.
  • Storing large objects — APCu memory is shared with PHP workers and exhausting it crashes requests.
  • Data that must be consistent across all servers — use Redis or Memcached for shared cache.
  • CLI scripts — APCu in CLI uses a separate memory pool isolated from the web server processes.

When To Use

  • Single-server applications or worker processes where shared-memory cache is sufficient.
  • Caching config, translations, or compiled data that is identical across requests on the same server.
  • Ultra-low-latency caching — APCu is faster than Redis because there is no network roundtrip.
  • OPcache extension companion — APCu caches user data while OPcache caches compiled bytecode.

Code Examples

✗ Vulnerable
// Miss detection bug — cached false indistinguishable from cache miss:
$result = apcu_fetch('key');
if (!$result) { // Wrong — also triggers when cached value is false or 0
    $result = expensiveQuery();
    apcu_store('key', $result, 300);
}

// Correct:
$result = apcu_fetch('key', $success);
if (!$success) {
    $result = expensiveQuery();
    apcu_store('key', $result, 300);
}
✓ Fixed
// APCu — in-process shared memory cache (same server, same PHP-FPM pool)
// Install: pecl install apcu; extension=apcu.so in php.ini

// Store & retrieve
apc_store('config:theme', 'dark', 3600);   // TTL in seconds
$theme = apc_fetch('config:theme', $success);
if (!$success) { /* cache miss */ }

// Atomic increment — race-condition-free counters
apc_inc('page_views:home');

// Clear specific key or all
apc_delete('config:theme');
apc_clear_cache();

// Good for: PHP-computed config, feature flags, hot lookup tables
// Not good for: session data, shared across multiple servers (use Redis instead)

Added 15 Mar 2026
Edited 25 Mar 2026
Views 30
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 1 ping W 0 pings T 1 ping F 0 pings S 2 pings S 0 pings M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 0 pings S 0 pings M 1 ping T 1 ping W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Perplexity 9 Amazonbot 8 Ahrefs 4 Google 2 Unknown AI 2 SEMrush 2
crawler 26 crawler_json 1
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Low
⚡ Quick Fix
Use APCu (apcu_store/apcu_fetch) for per-process in-memory caching of expensive computed values — it's faster than Redis for single-server setups with no serialisation overhead
📦 Applies To
PHP 5.4+ web cli
🔗 Prerequisites
🔍 Detection Hints
Expensive computation repeated on every request that could be cached in APCu for single-server setups; Redis used for single-server cache when APCu would suffice
Auto-detectable: ✗ No blackfire xdebug
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: File

✓ schema.org compliant