symfony/cache
Symfony Cache provides fast, low-overhead PSR-6 caching with adapters for common backends. Includes PSR-16 bridge plus implementations of symfony/cache-contracts CacheInterface and TagAwareCacheInterface for flexible app caching.
Installation (updated for security):
composer require symfony/cache:^8.1.0-BETA3
Basic PSR-6 Cache Pool (unchanged):
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
$cache = new FilesystemAdapter(); // Defaults to `/dev/shm/` or `/tmp/` on Unix
$cache->set('foo', 'bar', 3600); // Cache for 1 hour
$value = $cache->get('foo'); // Returns 'bar' or null
First Use Case (unchanged):
use Symfony\Component\Cache\Adapter\RedisAdapter;
$redis = new RedisAdapter();
$cacheKey = 'user_'.$userId;
$data = $redis->get($cacheKey);
if (!$data) {
$data = User::find($userId)->toArray();
$redis->set($cacheKey, $data, 300); // Cache for 5 minutes
}
(See previous assessment for table and examples)
(See previous assessment for example)
(See previous assessment for example, now with resolved expiry propagation)
(See previous assessment for example, now with fixed getMultiple())
(See previous assessment for example)
Critical Fix: The AbstractAdapter::clear() method now validates prefixes to prevent potential security issues (CVE-2026-45073). Ensure your code doesn’t pass unsafe prefixes:
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
// Safe: Uses default prefix
$cache = new FilesystemAdapter();
$cache->clear(); // Validates prefix automatically
// Safe: Custom prefix (validated)
$cache = new FilesystemAdapter('', 3600, 'myapp_');
$cache->clear(); // Validates 'myapp_' prefix
// Unsafe (throws exception in v8.1.0-BETA3+):
// $cache = new FilesystemAdapter('', 3600, '../malicious_prefix');
// $cache->clear(); // Throws InvalidArgumentException
(See previous assessment for example)
(See previous assessment for example)
(See previous assessment for example, now with fixed reset())
(See previous assessment for example)
Enhanced Stats Collection (unchanged):
use Symfony\Component\Cache\Stats\StatsCollector;
$statsAdapter = new StatsAdapter(new FilesystemAdapter());
$statsAdapter->set('key', 'value', 3600);
$stats = $statsAdapter->getStats(); // Includes hit/miss counts, latency, and memory usage
Laravel-Specific Tip: When using custom cache prefixes in Laravel’s Cache::store() or Cache::prefix(), ensure they comply with Symfony’s validation:
// Safe in Laravel (validated by Symfony under the hood)
Cache::store('redis')->prefix('myapp_')->set('key', 'value');
// Avoid unsafe prefixes (will throw in v8.1.0-BETA3+):
// Cache::store('redis')->prefix('../malicious_')->set('key', 'value');
Tag Invalidation Race Conditions (unchanged) (See previous assessment for workaround)
Expiry in Chained Adapters (resolved) (See previous assessment for fix)
Redis Connection Pooling (unchanged) (See previous assessment for fix)
APCu Limitations (unchanged) (See previous assessment for fix)
Doctrine DBAL Transactions (unchanged) (See previous assessment for fix)
PSR-16 getMultiple() (resolved)
(See previous assessment for fix)
Prefix Validation Security Risk (NEW)
../, absolute paths) to AbstractAdapter::clear() can lead to cache directory traversal or other security issues.InvalidArgumentException for unsafe values.clear() with custom prefixes must ensure they are safe (e.g., alphanumeric + underscores).$cache = new FilesystemAdapter('', 3600, '../malicious_prefix');
$cache->clear(); // Throws InvalidArgumentException
Enable Cache Logging (unchanged) (See previous assessment for example)
Check Cache Hit/Miss Ratios (unchanged) (See previous assessment for example)
Validate Redis Keys (unchanged) (See previous assessment for example)
Inspect Tag Operations (unchanged) (See previous assessment for example)
New: Prefix Validation Debugging
If your application uses custom prefixes and encounters InvalidArgumentException in clear(), validate the prefix format:
$prefix = 'myapp_';
if (!preg_match('/^[a-zA-Z0-9_\-]+$/', $prefix)) {
throw new InvalidArgumentException("Invalid cache prefix: {$prefix}");
}
Memory Usage Tracking (unchanged) (See previous assessment for example)
Key Changes in v8.1.0-BETA3:
AbstractAdapter::clear() to prevent CVE-2026-45073.clear()).clear().How can I help you explore Laravel packages today?