A high-performance, multi-level caching system for Symfony applications. This bundle provides a flexible, extensible, and developer-friendly way to combine multiple cache backends (memory, Redis, file, etc.) for optimal speed and reliability.
- Multi-level cache (memory, Redis, file, etc.)
- Pluggable cache implementations
- Cache statistics and profiling (Symfony Profiler integration)
- Beta decay and TTL randomization to prevent stampedes
- Easy integration with Symfony Dependency Injection
- Extensible via interfaces and factories
- Exception handling and diagnostics
- PHP >= 8.4
- Symfony >= 7.4
- Redis (optional, for Redis cache level)
Install via Composer from GitHub:
- Add the repository to your
composer.json:{ "repositories": [ { "type": "vcs", "url": "https://github.com/tbessenreither/multi-level-cache" } ] } - Require the package:
composer require tbessenreither/multi-level-cache
- Enable the Bundle in
config/bundles.php:Tbessenreither\MultiLevelCache\Bundle\MultiLevelCacheBundle::class => ['all' => true],
- Configure Environment Variables as needed:
REDIS_DSN(if using Redis)MLC_DISABLE_READ(optional, disables cache reads)MLC_COLLECT_ENHANCED_DATA(optional, enables enhanced data collection but has performance impact)
You can use the multi-level cache in two ways:
You can instantiate and configure the MultiLevelCacheService directly in your constructor, providing the cache implementations you want to use. The Redis client should be injected via dependency injection:
use Tbessenreither\MultiLevelCache\Service\MultiLevelCacheService;
use Tbessenreither\MultiLevelCache\Service\Implementations\InMemoryCacheService;
use Tbessenreither\MultiLevelCache\Service\Implementations\DirectRedisCacheService;
use Redis; // or RedisCluster
public function __construct(Redis $redisClient) {
$inMemory = new InMemoryCacheService();
$redis = new DirectRedisCacheService($redisClient);
$this->cache = new MultiLevelCacheService([
$inMemory,
$redis,
]);
}Inject the MultiLevelCacheFactory and use it to create a pre-configured cache service:
use Tbessenreither\MultiLevelCache\Factory\MultiLevelCacheFactory;
public function __construct(MultiLevelCacheFactory $cacheFactory) {
$this->cache = $cacheFactory->createDefault2LevelCache();
}Once you have a MultiLevelCacheService instance (from either method above), usage is identical:
$this->cache->set('my_key', $object, 3600);
$value = $this->cache->get('my_key', function() {
// This callback is called only if there is a cache miss.
// Return the value to be cached and returned.
return $expensiveComputationOrFetch();
}, 3600);
$this->cache->delete('my_key');- Bundle: Entry point for Symfony integration (src/Bundle/MultiLevelCacheBundle.php)
- Service: Main cache logic (src/Service/MultiLevelCacheService.php)
- Factory: Helper for common cache setups (src/Factory/MultiLevelCacheFactory.php)
- Implementations:
- Key Generator: (src/Service/CacheKeyGeneratorService.php)
- Interfaces:
- DTOs: Data transfer objects for cache and profiling (src/Dto/)
- Enums: Error and warning enums (src/Enum/)
- Exceptions: Custom exception types (src/Exception/)
- Profiler Integration: Data collector and templates (src/DataCollector/, src/Templates/Profiler/)
Note: While it is possible to configure the MultiLevelCacheService directly in
services.yaml, this approach is discouraged. The intended and recommended way is to use theMultiLevelCacheFactoryfor setup and configuration.
# config/services.yaml
services:
Tbessenreither\MultiLevelCache\Service\MultiLevelCacheService:
arguments:
$caches:
- '@Tbessenreither\MultiLevelCache\Service\Implementations\InMemoryCacheService'
- '@Tbessenreither\MultiLevelCache\Service\Implementations\DirectRedisCacheService'
$writeL0OnSet: true
$ttlRandomnessSeconds: 10
$cacheGroupName: 'default'Run PHPUnit tests:
ddev composer test- src/Bundle/: Symfony bundle integration
- src/Service/: Main service and cache implementations
- src/Factory/: Factory for cache setup
- src/Dto/: Data transfer objects
- src/Enum/: Error and warning enums
- src/Exception/: Custom exceptions
- src/Interface/: Interfaces for extensibility
- src/DataCollector/: Profiler and statistics
- src/Templates/Profiler/: Symfony profiler templates
- tests/: PHPUnit tests
MIT