Content · Web Development · PHP

PHP apcu_cache_info() Function: Usage and Examples

Reports APCu shared cache statistics and optionally entry listings.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Admin

Reports APCu shared cache statistics and optionally entry listings.

What It Does and When to Use It


Inspect global statistics without dumping every cache key. APCu stores local application data, Redis is external and can be shared among hosts, and OPcache stores compiled code.

Requirements, Inputs and Expected Behavior


Confirm the runtime, extension, environment variables, and permissions before running the examples in production. APCu in CLI may be disabled and is process-group/local-host cache, not a shared distributed cache.

Example 1: Basic Operation


Inspect global statistics without dumping every cache key.

<?php
if (function_exists('apcu_cache_info') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOL)) {
     $info = apcu_cache_info(true);
     echo 'Hits: ', $info['num_hits'] ?? 0;
 }

Example 2: Behavior and Validation


Calculate hit rate with safe handling for an empty cache.

<?php
$stats = apcu_cache_info(true);
 $hits = (int)($stats['num_hits'] ?? 0);
 $misses = (int)($stats['num_misses'] ?? 0);
 echo $hits + $misses ? round($hits / ($hits + $misses) * 100, 1) . '%' : 'No traffic';

Practical Example: Application Use


Review cache pressure and evictions; keys are sensitive in production.

<?php
$details = apcu_cache_info(false);
 foreach (['num_entries', 'mem_size', 'expunges'] as $key) {
     echo $key, ': ', $details[$key] ?? 'not available', "
";
 }

Important Behavior and Edge Cases


The second example highlights: Calculate hit rate with safe handling for an empty cache. The practical example adds: Review cache pressure and evictions; keys are sensitive in production.

Comparison with Related Approaches


ApproachWhat changes
APCu versus Redis and OPcacheAPCu stores local application data, Redis is external and can be shared among hosts, and OPcache stores compiled code.
Choosing safelyDefine TTL, invalidation, and concurrency behavior separately from the cache storage API.

Common Beginner Mistakes


MistakeCorrect approach
Assuming a cache is shared across multiple serversConfirm process- and host-level cache scope; use a distributed store when needed.
Never invalidating cached dataSet a TTL or versioned cache key and test release-time invalidation.
Skipping operational constraintsAPCu in CLI may be disabled and is process-group/local-host cache, not a shared distributed cache.

Key Takeaways


  • Reports APCu shared cache statistics and optionally entry listings.
  • Review cache pressure and evictions; keys are sensitive in production.
  • Define TTL, invalidation, and concurrency behavior separately from the cache storage API.
Share this resourceSend this page to someone who may find it useful.