laminas/laminas-cache-storage-adapter-ext-mongodb
laminas/laminas-cache library. While Laravel uses Symfony Cache (PSR-6/PSR-16) by default, this adapter can be adapted via PSR-6 compatibility layers (e.g., symfony/cache + laminas-cache-bridge).file, redis, or database cache drivers may suffice for simpler needs, but this provides MongoDB-specific optimizations (e.g., ObjectId metadata, native BSON serialization).laminas-cache-bridge or symfony/cache adapter) to integrate with Laravel’s Cache facade. Example:
use Laminas\Cache\Storage\Adapter\ExtMongoDb;
use Symfony\Bridge\PsrCache\Psr16Cache;
use Psr\Cache\CacheItemPoolInterface;
$mongoAdapter = new ExtMongoDb(['resourceManager' => $mongoClient]);
$cachePool = new Psr16Cache($mongoAdapter, [], false);
Cache::setStore($cachePool); // Laravel 8+ custom store
ext-mongodb PHP extension (not the mongodb/mongodb library). Verify compatibility with your Laravel server’s PHP version (8.1–8.3).ExtMongoDbOptions), unlike Laravel’s simple config/cache.php drivers.| Risk Area | Assessment |
|---|---|
| Breaking Changes | Major version (3.x) introduces final classes and laminas-cache v4 dependency. Ensure Laravel’s PSR-6 layer supports this. |
| Performance | MongoDB latency may impact cache speed. Test under load vs. Redis/Memcached. |
| Maintenance | Low-starred package (2 stars) with no dependents. Risk of stagnation. |
| Compatibility | PHP 8.1+ only; Laravel 8+ (Symfony 5+) required for PSR-6 integration. |
| Data Serialization | Uses MongoDB’s native BSON; may require custom serialization for complex Laravel data (e.g., Eloquent models). |
Why MongoDB?
Performance Trade-offs
Laravel Integration
cache()->tags()) be handled?Fallback Strategy
Long-Term Viability
jenssegers/mongodb + custom cache layer.symfony/cache or laminas-cache-bridge).ext-mongodb (not mongodb/mongodb library).ExtMongoDb:
// app/Providers/AppServiceProvider.php
use Laminas\Cache\Storage\Adapter\ExtMongoDb;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
public function register()
{
$mongoAdapter = new ExtMongoDb([
'resourceManager' => new \MongoDB\Driver\Manager("mongodb://user:pass@host:port"),
'namespace' => 'cache',
]);
$cachePool = new TagAwareAdapter(new Psr16Cache($mongoAdapter));
Cache::extend('mongodb', function () use ($cachePool) {
return new CacheManager($cachePool);
});
}
view, config).config/cache.php to use the new mongodb driver.| Component | Compatibility Notes |
|---|---|
| Laravel | Requires Laravel 8+ (Symfony 5+) for PSR-6 support. |
| MongoDB | Needs ext-mongodb extension (not mongodb/mongodb library). |
| Laminas Cache | Version 4+ required (due to final classes and type changes). |
| PHP | 8.1–8.3 only (drop PHP 7.x support). |
| Existing Cache Keys | May need migration if using custom serialization (e.g., JSON vs. BSON). |
get, set, delete).Cache facade.ext-mongodb: Requires server-level extension installation (not Composer-managed).laminas-cache v4).$mongoAdapter->setLogger(new \Monolog\Logger('mongodb_cache'));
explain() to analyze slow cache operations.try {
return Cache::store('mongodb')->get($key);
} catch (\Exception $e) {
return Cache::store('file')->get($key);
}
secondary) for read-heavy workloads.cache collection (e.g., { namespace: 1, key: 1 }).wtimeout/j flags in MongoDB driver for write consistency.db.currentOp() for blocking operations.| Failure Scenario | Mitigation Strategy |
|---|---|
| MongoDB Downtime | Fallback to file/database cache with graceful degradation. |
| Connection Pool Exhaustion | Increase maxPoolSize in Mongo |
How can I help you explore Laravel packages today?