Installation:
composer require snc/redis-bundle
Ensure ext-redis is installed (preferred) or fall back to Predis.
Configuration:
Add Redis to your config/packages/snc_redis.yaml:
snc_redis:
clients:
default:
type: predis # or 'phpredis'
alias: default
dsn: redis://127.0.0.1:6379
First Use Case: Inject the Redis client into a service/controller:
use SNC\RedisBundle\Client;
class MyService {
public function __construct(private Client $redis) {}
public function cacheData(string $key, string $value, int $ttl = 3600) {
$this->redis->setex($key, $ttl, $value);
}
}
usage.md and configuration.md).default client is auto-configured if no other clients are defined.composer.json for ext-redis presence; use phpredis if available.Caching:
// Set with TTL
$this->redis->setex('user:123:data', 3600, json_encode($data));
// Get and auto-delete (e.g., for rate limiting)
$value = $this->redis->get('temp:key');
$this->redis->del('temp:key');
Pub/Sub:
// Subscribe in a background process
$pubsub = $this->redis->pubsubLoop();
$pubsub->subscribe(['channel:events']);
$pubsub->listen();
Transactions:
$this->redis->multi()
->set('key1', 'value1')
->incr('counter')
->exec();
Pipelining (for batch operations):
$pipeline = $this->redis->pipeline();
$pipeline->set('key1', 'value1');
$pipeline->set('key2', 'value2');
$pipeline->execute();
Dependency Injection:
Tag services needing Redis with snc_redis.client to auto-wire the correct client:
services:
App\Service\MyService:
tags: ['snc_redis.client:default']
Environment-Specific Clients:
Use %env(REDIS_URL)% in dsn for dynamic configurations:
dsn: redis://%env(REDIS_URL)%/0
Connection Pooling: Reuse clients (e.g., in a singleton service) to avoid connection overhead.
Laravel-Specific: If using Laravel, alias the bundle’s client to Laravel’s container:
// In a service provider
$this->app->bind(\SNCRedisBundle\Client::class, function ($app) {
return $app->make('snc_redis.client.default');
});
Connection Timeouts:
2.5 seconds. Increase via options.timeout in config if Redis is slow:
options:
timeout: 5.0
redis-cli ping to verify server connectivity.Predis vs. PhpRedis Behavior:
hScan) have slight syntax differences. Test both backends if switching.Serialization:
serialize()/unserialize() or JSON for complex objects:
$this->redis->set('user:1', serialize($user));
$user = unserialize($this->redis->get('user:1'));
Memory Leaks:
memory_usage() to monitor:
$this->redis->info('memory');
Symfony Cache vs. Redis:
Cache component with raw Redis calls for the same keys to prevent inconsistencies.Enable Logging:
Add to config/packages/monolog.yaml:
handlers:
snc_redis:
type: stream
path: "%kernel.logs_dir%/snc_redis.log"
level: debug
channels: ["snc_redis"]
Then enable Redis logging in snc_redis.yaml:
logging: true
Common Errors:
Connection refused: Check Redis server (redis-server running? Firewall?).Invalid argument: Verify dsn format (e.g., redis://user:pass@host:port/db).redis:// (not tcp://) and ensure credentials are URL-encoded.Custom Clients:
Define additional clients in snc_redis.yaml:
clients:
cache:
type: phpredis
alias: cache
dsn: redis://cache:6379/1
Inject via snc_redis.client.cache.
Middleware:
Use Symfony’s kernel.request event to wrap Redis calls in middleware (e.g., for retries):
$event->getRequest()->attributes->set('redis_retries', 3);
Event Listeners:
Subscribe to Redis events (e.g., key expiration) via snc_redis.event_listener:
services:
App\Listener\RedisKeyListener:
tags: ['snc_redis.event_listener']
Testing:
Use snc_redis.test client for tests:
clients:
test:
type: predis
dsn: "redis://localhost:6379/15" # Isolated DB
Inject via snc_redis.client.test in test services.
SET operations in one pipeline).$this->redis->hSet('user:1', [
'name' => 'John',
'email' => 'john@example.com',
]);
gzip before storing blobs:
$compressed = gzcompress($data);
$this->redis->set('large:data', $compressed);
How can I help you explore Laravel packages today?