symfony/cache-contracts
Symfony Cache Contracts defines lightweight, PSR-friendly interfaces for cache and tag-aware caching, enabling consistent cache usage across Symfony components and third-party libraries. Use it to type-hint against stable APIs while swapping cache implementations.
No Installation Needed in Laravel
Since Laravel already bundles symfony/cache-contracts via symfony/cache, skip composer require. Focus on Laravel’s built-in Cache facade or dependency-injected interfaces.
First Use Case: Caching a Value
// Cache a value for 10 minutes
Cache::put('key', 'value', now()->addMinutes(10));
// Retrieve with fallback
$value = Cache::remember('key', now()->addMinutes(10), function () {
return expensiveOperation();
});
Where to Look First
vendor/symfony/cache-contracts for interfaces like CacheItemPoolInterface and TagAwareCacheInterface.Inject Psr\Cache\CacheItemPoolInterface (PSR-6) or Symfony\Contracts\Cache\CacheInterface (Symfony’s PSR-6 subset) into services:
use Psr\Cache\CacheItemPoolInterface;
class MyService {
public function __construct(private CacheItemPoolInterface $cache) {}
public function getCachedData(string $key): mixed {
$item = $this->cache->getItem($key);
return $item->isHit() ? $item->get() : $this->fetchFreshData($key);
}
}
Use Laravel’s Cache::tags() for grouped invalidation (leverages TagAwareCacheInterface):
// Store with tags
Cache::tags(['products', 'inventory'])->put('product:1', $product, now()->addHour());
// Invalidate all tagged items
Cache::tags(['products'])->flush();
Combine adapters (e.g., in-memory + Redis) using Cache::extend():
Cache::extend('fallback', function ($app) {
$store = new CacheManager();
$store->setDefaultDriver('redis');
$store->extend('array', function () {
return new ArrayAdapter(); // Fallback to in-memory
});
return $store->driver('redis');
});
Batch operations with CacheItemPoolInterface:
$pool = Cache::store('array'); // PSR-6 pool
$pool->getItem('key1')->set('value1');
$pool->getItem('key2')->set('value2');
$pool->commit(); // Atomic write
Extend Laravel’s cache system by implementing CacheItemPoolInterface:
// config/cache.php
'stores' => [
'custom' => [
'driver' => 'custom',
'connection' => 'default',
],
],
// app/Providers/AppServiceProvider.php
Cache::extend('custom', function () {
return new CustomCachePool(); // Your PSR-6 implementation
});
symfony/cache-contracts is only interfaces. Pair it with:
redis, file, database).FilesystemAdapter, RedisAdapter).doctrine/cache.cache-contracts v2.x (PSR-6).v1.x (older contracts).symfony/cache-contracts:^2.0 for PSR-6 compliance.composer require symfony/cache:^5.0 to auto-pull the correct contracts.serializer config).Cache::put('key', serialize($data), ...) for complex objects.storage/framework/cache for stale files.Cache::forget() or Cache::flush() to clear.CacheItemPoolInterface).Psr\SimpleCache\CacheInterface) is separate (use symfony/cache's SimpleCacheAdapter).get() (PSR-16) with getItem() (PSR-6) causes errors.CacheItemPoolDecorator to log/monitor cache hits.TagAwareCacheInterface for advanced invalidation.class LoggingCachePool extends CacheItemPoolDecorator {
public function getItem($key) {
$item = parent::getItem($key);
if ($item->isHit()) {
Log::debug("Cache hit for: $key");
}
return $item;
}
}
Cache::driver(): Returns a CacheManager (not a PSR-6 pool). Use Cache::store('redis')->getItem() for PSR-6.file, database, and redis drivers.Cache::shouldReceive('get') in PHPUnit or ArrayAdapter for isolated tests.How can I help you explore Laravel packages today?