cache/void-adapter
PSR-6 “void” (null/blackhole) cache pool that never stores anything and always returns empty cache items. Useful for disabling caching in tests or no-op environments. Part of the PHP-Cache organization.
Installation:
composer require cache/void-adapter
No additional configuration is required.
First Use Case:
Replace any PSR-6 cache pool with VoidCachePool for testing or debugging:
use Cache\VoidCachePool;
$cache = new VoidCachePool();
$item = $cache->getItem('test-key');
$item->set('value'); // Data is discarded immediately
$cache->save($item); // No-op
Laravel Integration:
Register as a custom driver in config/cache.php:
'void' => [
'driver' => 'cache',
'pool' => Cache\VoidCachePool::class,
],
Use via:
Cache::driver('void')->put('key', 'value'); // Silent no-op
getItem, save, deleteItem, etc.) work as expected (they do, but return empty results).Cache::driver('void')->remember() to confirm no persistence.getItemsByTag() returns empty results (expected behavior).Debugging Workflows:
void in AppServiceProvider during debugging:
if (app()->environment('debug')) {
Cache::extend('void', fn() => new VoidCachePool());
Cache::setDefaultDriver('void');
}
Feature Flag Isolation:
if (Feature::isEnabled('experimental-feature')) {
Cache::driver('void')->put('feature-data', $data);
}
Fallback Strategy:
try {
return Cache::driver('redis')->get('key');
} catch (RedisException $e) {
return Cache::driver('void')->get('key'); // Fallback
}
Test Environments:
void in phpunit.xml:
<env name="CACHE_DRIVER" value="void"/>
Cache-Agnostic Development:
$cache = Cache::driver('void'); // Works identically to Redis/APCu
$data = $cache->get('key', fn() => fetchExpensiveData());
Dynamic Driver Switching:
$driver = env('CACHE_DRIVER', 'redis');
Cache::driver($driver)->put('key', 'value');
Tagging (Limited Use):
Cache::driver('void')->tags(['analytics'])->put('report', $data);
Laravel Cache Events:
void usage:
Cache::store('void')->listen(function ($events) {
Log::debug('Void cache event triggered:', $events);
});
Service Container Binding:
VoidCachePool as a singleton for dependency injection:
$app->singleton(Cache\VoidCachePool::class, fn() => new VoidCachePool());
Hybrid Caching:
$cache = Cache::driver('redis');
if (app()->environment('local')) {
$cache = Cache::driver('void'); // Override locally
}
Cache Tagging in Laravel:
Cache::tags() with void for testing tag-based invalidation logic:
Cache::tags(['users'])->put('user:1', $user);
Cache::tags(['users'])->flush(); // No-op, but tests pass
Silent Data Loss:
if (app()->environment('production') && Cache::getStore()->getDriver() === 'void') {
Log::warning('Void cache detected in production!');
}
Tagging Misuse:
getItemsByTag() return empty results, which may break logic assuming tags work.Laravel-Specific Issues:
void will not persist across requests.void will not survive worker restarts.void for critical Laravel features like event caching.Performance Metrics:
void from cache analytics in production.Accidental Deployment:
void is never the default driver in config/cache.php for production.// config/cache.php
'default' => env('CACHE_DRIVER', 'redis'),
Verify No Persistence:
Cache::driver('void')->put('test', 'value');
$this->assertNull(Cache::driver('void')->get('test'));
Tagging Debugging:
Cache::driver('void')->tags(['test'])->put('key', 'value');
$this->assertEmpty(Cache::driver('void')->getItemsByTag('test'));
Laravel Cache Events:
Cache::store('void')->listen(function ($events) {
dd($events); // Inspect triggered events
});
PSR-6 Compliance:
getMetadata) return empty results.class CustomVoidCachePool extends VoidCachePool {
public function getMetadata($key) {
return new CacheItemMetadata(); // Return empty metadata
}
}
Laravel Cache Manager:
Cache::extend('void', fn() => new VoidCachePool());
Cache::driver('void') instead of Cache::store('void') for clarity.Environment-Specific Drivers:
$driver = app()->environment('local') ? 'void' : 'redis';
Cache::driver($driver)->put('key', 'value');
Custom Void Pool:
VoidCachePool to add logging or mock behavior:
class LoggingVoidCachePool extends VoidCachePool {
public function save(CacheItemInterface $item) {
Log::debug("Void save called for key: {$item->getKey()}");
parent::save($item);
}
}
Hybrid Cache Logic:
$cache = Cache::driver('void');
if (app()->environment('production')) {
$cache = Cache::driver('redis');
}
Tagging Simulation:
$mockTags = ['users', 'reports'];
$cache = new class extends VoidCachePool {
public function getItemTags($key) {
return $mockTags;
}
};
Fallback with Retry:
function getWithFallback($key, callable $callback) {
try {
return Cache::driver('redis')->get($key);
} catch (Exception $e) {
return Cache::driver('void')->get($key, $callback);
}
}
How can I help you explore Laravel packages today?