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
| Approach | What changes |
|---|---|
| APCu versus Redis and OPcache | APCu stores local application data, Redis is external and can be shared among hosts, and OPcache stores compiled code. |
| Choosing safely | Define TTL, invalidation, and concurrency behavior separately from the cache storage API. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Assuming a cache is shared across multiple servers | Confirm process- and host-level cache scope; use a distributed store when needed. |
| Never invalidating cached data | Set a TTL or versioned cache key and test release-time invalidation. |
| Skipping operational constraints | APCu 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.