symfony/polyfill-apcu
Symfony Polyfill APCu adds apcu_* functions and the APCuIterator class for environments relying on the legacy APC extension, improving compatibility across PHP setups. Part of Symfony’s polyfill suite and released under the MIT license.
Installation:
composer require symfony/polyfill-apcu
Add to composer.json under require:
"symfony/polyfill-apcu": "^1.35"
First Use Case:
apcu_* functions (e.g., apcu_fetch, apcu_store) and runs on PHP <7.4 or shared hosting without APCu, the polyfill will automatically enable these functions.$value = apcu_fetch('my_key'); // Works even without native APCu
apcu_store('my_key', 'data', 3600); // Persists for 1 hour
Where to Look First:
config/cache.php for APCu-related configurations.apcu_* function calls:
grep -r "apcu_" app/ --include="*.php"
Cache facade usage, especially for apcu driver.Laravel Cache Facade Integration:
apcu driver in Laravel’s cache configuration (config/cache.php):
'apcu' => [
'driver' => 'apcu',
'prefix' => 'laravel_',
],
Conditional Activation:
// app/Providers/AppServiceProvider.php
public function boot()
{
if (!extension_loaded('apcu') && app()->environment('shared')) {
\Symfony\Polyfill\Apcu\ApcuFunctions::register();
Log::info('APCu polyfill activated for shared hosting');
}
}
Environment-Specific Configuration:
.env to toggle polyfill:
APCU_POLYFILL_ENABLED=true
// config/cache.php
'stores' => [
'apcu' => [
'driver' => 'apcu',
'polyfill' => env('APCU_POLYFILL_ENABLED', false),
],
],
Fallback Logic:
try {
$value = Cache::store('apcu')->get('key');
} catch (\Exception $e) {
$value = Cache::store('file')->get('key'); // Fallback
}
Testing with Polyfill:
use Symfony\Polyfill\Apcu\ApcuFunctions;
public function setUp(): void
{
ApcuFunctions::register();
}
Shared Hosting Workflow:
apcu_* functions, allowing Laravel’s apcu cache driver to work.CI/CD Pipeline Workflow:
RUN pecl install apcu || true # Attempt install, fall back to polyfill
RUN composer require symfony/polyfill-apcu
Legacy App Modernization:
// config/cache.php
'default' => env('CACHE_DRIVER', 'redis'),
'stores' => [
'apcu' => ['driver' => 'apcu', 'polyfill' => true],
'redis' => ['driver' => 'redis'],
],
Local Development:
composer require symfony/polyfill-apcu
Homestead or Valet without ext-apcu.Laravel Cache Events:
Cache::store('apcu')->listen(function ($event) {
Log::debug('APCu cache event: ' . $event->key, ['polyfill' => ApcuFunctions::isRegistered()]);
});
Custom Cache Drivers:
ApcuStore to handle polyfill-specific logic:
class PolyfillApcuStore extends \Illuminate\Cache\ApcuStore
{
public function __construct(array $options)
{
if (!extension_loaded('apcu') && !\Symfony\Polyfill\Apcu\ApcuFunctions::isRegistered()) {
\Symfony\Polyfill\Apcu\ApcuFunctions::register();
}
parent::__construct($options);
}
}
Performance Monitoring:
Cache::store('apcu')->get('key', function () {
Log::debug('APCu polyfill fallback executed for key: key');
return 'default_value';
});
Environment Detection:
function isSharedHosting(): bool
{
return strpos($_SERVER['SERVER_SOFTWARE'] ?? '', 'HostGator') !== false;
}
Performance Overhead:
config, sessions).Memory Limits:
Allowed memory exhausted errors for large caches (>100MB).Unsupported Functions:
apcu_cas, apcu_delete) may not work with the polyfill.grep -r "apcu_cas\|apcu_delete" app/
Serialization Issues:
serialize()/unserialize() for problematic types:
$serialized = serialize($object);
apcu_store('key', $serialized);
$object = unserialize(apcu_fetch('key'));
Distributed Caching Limitations:
Opcode Caching:
opcache). Native APCu is required for this.Concurrent Write Conflicts:
apcu_fetch + apcu_store loops) or switch to Redis for high-concurrency scenarios.Check Polyfill Activation:
if (\Symfony\Polyfill\Apcu\ApcuFunctions::isRegistered()) {
echo 'Polyfill is active';
}
Log Cache Misses:
Cache::store('apcu')->get('key', function () {
Log::warning('APCu polyfill cache miss for key: key');
return 'default';
});
Memory Usage:
$memoryUsage = memory_get_usage(true);
Log::debug('APCu polyfill memory usage: ' . $memoryUsage);
Function Availability:
if (!function_exists('apcu_fetch')) {
Log::error('APCu functions not available (polyfill failed)');
}
ext-apcu is not loaded. This may cause unexpected behavior if not intended.
How can I help you explore Laravel packages today?