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.
Cache facade, ApcuStore), requiring no architectural refactoring. Leverages Laravel’s service container and configuration system for environment-aware activation, aligning with Laravel’s modular design.session.driver = 'apcu' or object caching). Enables incremental migration to modern caching solutions without immediate codebase disruption.// config/cache.php
'stores' => [
'apcu' => [
'driver' => 'apcu',
'use_polyfill' => app()->environment('shared_hosting'),
],
],
apcu, redis) via configuration, preventing unintended performance degradation in critical paths. Example:
Cache::store('apcu')->put('key', 'value', 60); // Uses polyfill if enabled
Cache::store('redis')->put('key', 'value', 60); // Unaffected
Cache events (e.g., CacheStoredEvent, CacheMissedEvent) to log polyfill usage or trigger fallbacks (e.g., switch to database cache if polyfill fails). Example:
Cache::extend('apcu', function ($app) {
return new class extends \Illuminate\Cache\ApcuStore {
public function store($key, $value, $ttl = null) {
if (\Symfony\Polyfill\Apcu\ApcuFunctions::isRegistered()) {
Log::debug('APCu polyfill storing: ' . $key);
}
return parent::store($key, $value, $ttl);
}
};
});
apcu_* function calls (e.g., apcu_fetch, apcu_store) work out-of-the-box after installation. Ideal for quick wins in shared hosting or legacy environments with minimal developer effort.composer require symfony/polyfill-apcu.AppServiceProvider or a dedicated provider for centralized control:
// app/Providers/ApcuPolyfillServiceProvider.php
public function boot()
{
if ($this->app->environment('shared_hosting') && !extension_loaded('apcu')) {
\Symfony\Polyfill\Apcu\ApcuFunctions::register();
Log::info('APCu polyfill activated for shared hosting');
}
}
Cache::shouldReceive()) and is mockable via PHPUnit. Example test:
public function test_apcu_polyfill_in_shared_hosting()
{
if (!extension_loaded('apcu')) {
\Symfony\Polyfill\Apcu\ApcuFunctions::register();
}
Cache::put('test_key', 'test_value');
$this->assertEquals('test_value', Cache::get('test_key'));
}
Cache::remember with short TTLs).config, sessions) via Laravel’s cache tags:
Cache::tags(['non_critical'])->store('apcu', 'key', $value);
config/cache.php to disable polyfill for performance-sensitive stores.serialize()/unserialize(), which may fail for non-serializable types.if (!is_serializable($object)) {
throw new \RuntimeException('Object not serializable for APCu polyfill');
}
serialize()/unserialize() helpers or fall back to file cache for problematic types.apcu, file, database) rely on APCu? Can polyfill be scoped to specific stores via config/cache.php?ApcuStore) or third-party packages (e.g., laravel-apcu) that require polyfill integration?session.driver)? Will polyfill affect session handling?Cache::get('key')) in production? Use Laravel Telescope or Blackfire to benchmark.'stores' => [
'apcu' => ['driver' => 'apcu', 'polyfill' => env('APCU_POLYFILL')],
'redis' => ['driver' => 'redis', 'connection' => 'cache'],
],
.env flags, CI/CD pipelines, Laravel’s AppServiceProvider)?
# .env
APP_ENV=shared_hosting
APCU_POLYFILL=true
if (app()->environment('production')) use Redis).config/cache.php to disable polyfill post-migration.file or database cache)?
try {
return Cache::store('apcu')->get($key);
} catch (\Exception $e) {
return Cache::store('file')->get($key);
}
apcu_* functions)?$this->expectException(\RuntimeException::class);
Cache::store('apcu')->put('large_key', str_repeat('a', 100 * 1024 * 1024)); // 100MB
Cache facade, ApcuStore, and custom drivers. No changes required for existing apcu_* usage in:
session.driver = 'apcu').config('cache.stores.config.driver')).Cache::remember()).// config/cache.php
'stores' => [
'apcu' => [
'driver' => 'apcu',
'polyfill' => function () {
return !extension_loaded('apcu') && app()->environment(['shared_hosting', 'ci']);
},
],
],
// app/Providers/AppServiceProvider.php
public function register()
{
if (config('cache.stores.apcu.polyfill')) {
How can I help you explore Laravel packages today?