Installation
composer require bash/cache-bundle
Ensure phpredis, igbinary, and symfony/framework-bundle (≥5.0) are installed.
Enable the Bundle
Add to config/bundles.php:
Bash\CacheBundle\BashCacheBundle::class => ['all' => true],
Basic Configuration
Update config/packages/bash_cache.yaml (or create it):
bash_cache:
driver: 'redis' # or 'array' for testing
redis:
host: '127.0.0.1'
port: 6379
password: null
database: 0
First Use Case Cache a simple value:
use Bash\CacheBundle\Cache\CacheInterface;
class MyService {
public function __construct(private CacheInterface $cache) {}
public function getCachedData() {
$key = 'my_key';
$value = $this->cache->get($key);
if (!$value) {
$value = 'expensive_computation_result';
$this->cache->set($key, $value, 3600); // 1-hour TTL
}
return $value;
}
}
Dependency Injection
Inject CacheInterface (or RedisCache/ArrayCache) into services:
public function __construct(private RedisCache $redisCache) {}
Tag-Based Invalidation Use tags for grouped cache invalidation:
$this->redisCache->set('user:123', $userData, 3600, ['users']);
$this->redisCache->clearByTag('users'); // Invalidate all tagged keys
Automatic Tagging
Extend AbstractCache to auto-tag keys:
class MyCache extends AbstractCache {
protected function getTagsForKey(string $key): array {
return ['my_app', explode(':', $key)[0]];
}
}
Fallback Logic
Combine with Symfony’s CacheInterface for hybrid caching:
$data = $this->cache->get('key', function() {
return $this->fallbackService->loadData();
});
clearByTag() after bulk updates:
$entityManager->flush();
$this->redisCache->clearByTag('entities');
$response = $this->redisCache->get('api:users', function() {
return json_encode($this->userRepository->findAll());
});
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ClearCacheCommand extends Command {
protected function execute(InputInterface $input, OutputInterface $output) {
$this->redisCache->clear();
$output->writeln('Cache cleared!');
}
}
Redis Connection Issues
phpredis is installed (pecl install redis).php.ini for extension=redis.so.$this->redisCache->getRedis()->ping(); // Should return 'PONG'
Serialization Quirks
igbinary must be installed for efficient Redis serialization.serialize() in config/packages/bash_cache.yaml:
bash_cache:
serializer: 'serialize' # or 'igbinary'
Tagging Edge Cases
: in untagged scenarios (tags split on :).clearByTag() with empty tag arrays.TTL Granularity
3600 for 1 hour.Enable Logging
Add to config/packages/bash_cache.yaml:
bash_cache:
debug: true
Logs appear in var/log/dev.log.
Inspect Redis Keys
Use redis-cli to verify keys:
redis-cli KEYS "*"
redis-cli TTL "my_key"
Custom Drivers
Implement CacheInterface for alternative backends (e.g., Memcached):
class MemcachedCache implements CacheInterface {
// ...
}
Event Listeners
Extend cache events (e.g., CacheItemMissedEvent):
use Bash\CacheBundle\Event\CacheEvents;
$dispatcher->addListener(CacheEvents::MISS, function($event) {
if ($event->getKey() === 'critical_data') {
$event->setValue('fallback_value');
}
});
Configuration Overrides Override defaults in environment-specific configs:
# config/packages/dev/bash_cache.yaml
bash_cache:
redis:
host: 'redis-dev'
$this->redisCache->set('app:user:123', $data);
mget()/mset() for multiple keys:
$this->redisCache->mset([
'key1' => 'value1',
'key2' => 'value2',
]);
How can I help you explore Laravel packages today?