psr-discovery/cache-implementations
Discovers installed PSR-6 cache implementations at runtime and returns the first available adapter, avoiding hard dependencies in libraries/SDKs. Supports php-cache adapters and Stash, with options to prefer or manually instantiate configurable caches.
The PSR-16 Cache Implementations package (psr-discovery/cache-implementations) simplifies integration of PSR-16 compliant cache backends in Laravel. To start, install via Composer:
composer require psr-discovery/cache-implementations
For Laravel-specific use, pair with laravel/cache and configure in config/cache.php under the stores array. Example:
'stores' => [
'redis' => [
'driver' => 'redis',
'connection' => 'cache',
// ... other config
],
'psr' => [
'driver' => 'psr',
'implementation' => 'Laminas\Cache\Storage\Adapter\Memory', // or 'Neos\Cache\Cache'
],
],
First use case: Replace a custom cache wrapper with a PSR-16 adapter (e.g., Laminas\Cache or Neos\Cache) for standardized caching across services.
Cache Configuration:
Use the psr driver in Laravel’s config/cache.php to delegate to supported PSR-16 implementations (e.g., Laminas\Cache, Neos\Cache). Example:
'psr' => [
'driver' => 'psr',
'implementation' => 'Laminas\Cache\Storage\Adapter\Filesystem', // Filesystem, Memory, Redis, etc.
'options' => [
'cache_dir' => storage_path('framework/cache'),
],
],
^3.0–^4.0 (now expanded to ^4.0).^5.0–^8.0 (newly expanded).Service Provider Binding: Bind the PSR-16 cache to Laravel’s container in a service provider:
use Psr\Cache\CacheItemPoolInterface;
use PsrDiscovery\CacheImplementations\Factory;
public function register()
{
$this->app->singleton(CacheItemPoolInterface::class, function ($app) {
return Factory::createCache('Laminas\Cache\Storage\Adapter\Redis', [
'host' => config('cache.redis.host'),
'port' => config('cache.redis.port'),
]);
});
}
Usage in Controllers/Jobs:
Inject CacheItemPoolInterface and use PSR-16 methods:
public function __construct(private CacheItemPoolInterface $cache) {}
public function storeData()
{
$item = $this->cache->getItem('key');
$item->set('value');
$this->cache->save($item);
}
file, redis) with PSR-16 adapters for flexibility.CacheItemPoolInterface in tests using PHPUnit\Framework\TestCase:
$this->app->instance(CacheItemPoolInterface::class, $mockCache);
php in .php-version or server config.Neos/Laminas Version Mismatches:
neos/cache:^7.0 for Neos Cache).composer.json for version conflicts:
composer why neos/cache
PSR-16 Adapter Options:
Laminas\Cache\Storage\Adapter\Filesystem) require specific options like cache_dir. Omit these and the adapter may fail silently.Laravel Cache Driver Overrides:
psr driver in config/cache.php, ensure no duplicate bindings exist in service providers to avoid conflicts.Adapter Not Found:
Laminas\Cache\Storage\Adapter\Redis) matches the installed package’s namespace.composer dump-autoload after adding new cache implementations.Performance Issues:
Laminas\Cache\Storage\Adapter\Memory is in-memory only (cleared on restart). Use Filesystem or Redis for persistence.laravel-debugbar to compare PSR-16 vs. native drivers.Custom Adapters: Extend the package by adding your own PSR-16 implementations. Register them via:
Factory::addImplementation('My\Cache\Adapter', MyCacheAdapter::class);
Dynamic Configuration:
Use Laravel’s Cache::extend() to dynamically configure PSR-16 stores:
Cache::extend('dynamic_psr', function ($app) {
return Factory::createCache('Laminas\Cache\Storage\Adapter\Redis', [
'host' => env('REDIS_HOST'),
]);
});
Fallback Logic: Implement fallback caches for critical data:
try {
$value = $this->cache->get('critical_key');
} catch (\Psr\Cache\InvalidArgumentException $e) {
$value = $this->fallbackCache->get('critical_key');
}
How can I help you explore Laravel packages today?