Installation:
composer require drone076/redis-bundle
Add the bundle to config/bundles.php:
return [
// ...
Drone076\RedisBundle\Drone076RedisBundle::class => ['all' => true],
];
Basic Configuration:
Create config/packages/sb_redis.yaml:
sb_redis:
clients:
default:
$options: []
$parameters: ['tcp://127.0.0.1:6379']
First Use Case: Inject the Redis client into a service/controller:
use Predis\ClientInterface;
class MyService
{
public function __construct(private ClientInterface $redis)
{
}
public function testRedis()
{
$this->redis->set('foo', 'bar');
return $this->redis->get('foo'); // Returns 'bar'
}
}
Register the service in config/services.yaml:
services:
App\Service\MyService:
arguments:
$redis: '@sb_redis.default'
Multi-Client Configuration: Define multiple Redis clients (e.g., for caching vs. pub/sub):
sb_redis:
clients:
cache:
$options: []
$parameters: ['tcp://127.0.0.1:6379?database=0']
pubsub:
$options: []
$parameters: ['tcp://127.0.0.1:6379?database=1']
Inject by tagging services:
// In a service
public function __construct(
ClientInterface $cacheRedis,
ClientInterface $pubsubRedis
) {
$this->cacheRedis = $cacheRedis;
$this->pubsubRedis = $pubsubRedis;
}
Connection Pooling: Reuse clients for performance-critical operations (e.g., batch jobs):
// In a command
public function handle()
{
$this->redis->pipeline(function ($pipe) {
$pipe->set('key1', 'value1');
$pipe->set('key2', 'value2');
});
}
Pub/Sub Integration: Use Redis for real-time updates (e.g., notifications):
$subscriber = $this->redis->subscriber();
$subscriber->subscribe(['channel:notifications'], function ($message) {
// Handle message
});
Caching with TTL: Leverage Redis for time-bound caching:
$this->redis->setex('expensive_query', 3600, $result);
Symfony Cache Adapter: Use PredisAdapter for Symfony’s cache system:
framework:
cache:
app: 'cache.adapter.redis'
Configure in sb_redis.yaml:
sb_redis:
clients:
cache:
$options: []
$parameters: ['tcp://127.0.0.1:6379?database=2']
Doctrine Redis Cache: For Doctrine ORM:
doctrine:
orm:
metadata_cache_driver: 'redis'
query_cache_driver: 'redis'
result_cache_driver: 'redis'
Requires doctrine/redis-cache-bundle.
Deprecation Warnings:
$client = new Predis\Client($params, [
'profile' => Predis\ClientInterface::PROFILE_2_2,
]);
sb_redis.yaml:
sb_redis:
clients:
default:
$options: ['profile' => Predis\ClientInterface::PROFILE_2_2]
$parameters: ['tcp://127.0.0.1:6379']
Connection Timeouts:
$options:
$options:
timeout: 2.5
read_write_timeout: 2.5
Database Isolation:
sb_redis:
clients:
cache: { $parameters: ['tcp://127.0.0.1:6379?database=0'] }
session: { $parameters: ['tcp://127.0.0.1:6379?database=1'] }
Serialization:
serialize()/unserialize() for complex objects:
$this->redis->set('user:1', serialize($user));
$user = unserialize($this->redis->get('user:1'));
Connection Issues:
redis-cli ping).6379.sb_redis:
clients:
default:
$options:
logger: ['@monolog.logger.redis']
Key Expiry:
TTL to debug expiry issues:
$this->redis->ttl('key'); // Returns -2 if key doesn’t exist
Custom Client Factories: Override the default factory in a compiler pass:
public function process(ContainerBuilder $container)
{
$definition = $container->findDefinition('sb_redis.factory');
$definition->addMethodCall('setCustomOption', ['profile' => Predis\ClientInterface::PROFILE_2_2]);
}
Middleware: Add Predis middleware (e.g., for retry logic):
sb_redis:
clients:
default:
$options:
parameters:
- 'tcp://127.0.0.1:6379'
middleware: ['@app.redis.middleware.retry']
Environment-Specific Configs:
Use %env% for dynamic configs:
sb_redis:
clients:
default:
$parameters: ['tcp://%env(REDIS_HOST)%:%env(REDIS_PORT)%']
Pipeline Commands: Batch operations to reduce round trips:
$this->redis->pipeline(function ($pipe) {
for ($i = 0; $i < 100; $i++) {
$pipe->set("key:$i", "value:$i");
}
});
Connection Pooling: Reuse clients across requests (managed by Symfony’s container). Avoid recreating clients in loops.
Monitoring:
Use INFO command to track memory/performance:
$this->redis->rawCommand('INFO', 'memory');
How can I help you explore Laravel packages today?