Installation
Add the bundle to your composer.json:
composer require dawen/phpredis-bundle
Enable it in config/bundles.php:
return [
// ...
Dawen\PhpRedisBundle\DawenPhpRedisBundle::class => ['all' => true],
];
Configuration
Define Redis connection parameters in config/packages/dawen_php_redis.yaml:
dawen_php_redis:
clients:
default:
host: '127.0.0.1'
port: 6379
database: 0
password: null
timeout: 0
read_timeout: 0
First Use Case Inject the Redis client into a service/controller:
use Dawen\PhpRedisBundle\Client\RedisClientInterface;
class MyService
{
public function __construct(private RedisClientInterface $redis)
{
}
public function cacheUserData(int $userId, array $data): void
{
$this->redis->hMSet("user:$userId", $data);
}
}
Key-Value Operations
Use set/get for simple caching:
$this->redis->set('cache_key', 'value', 3600); // Expires in 1 hour
$value = $this->redis->get('cache_key');
Hashes for Structured Data
Store user profiles with hMSet/hGetAll:
$this->redis->hMSet('user:1', ['name' => 'John', 'email' => 'john@example.com']);
$userData = $this->redis->hGetAll('user:1');
Lists for Queues Push/pop from a queue:
$this->redis->rPush('queue:jobs', json_encode(['task' => 'send_email']));
$job = $this->redis->rPop('queue:jobs');
Sets for Unique Collections Track unique visitors:
$this->redis->sAdd('visitors:today', $userId);
$count = $this->redis->sCard('visitors:today');
Sorted Sets for Leaderboards Rank users by score:
$this->redis->zAdd('leaderboard', 100, $userId);
$rank = $this->redis->zRank('leaderboard', $userId);
Symfony Cache Integration
Use Redis as a cache backend by configuring framework/cache in config/packages/framework.yaml:
framework:
cache:
app: cache.adapter.redis
default_memcached_provider: 'redis://default'
Doctrine Cache
Configure Redis for Doctrine caching in config/packages/doctrine.yaml:
doctrine:
orm:
metadata_cache_driver: 'redis'
query_cache_driver: 'redis'
result_cache_driver: 'redis'
Event-Driven Workflows Use Redis pub/sub for real-time updates:
$this->redis->publish('user:updated', json_encode(['id' => 1]));
$this->redis->subscribe(['user:updated'], function ($message) {
// Handle update
});
Connection Management
try {
$this->redis->getConnection()->ping();
} catch (\RedisException $e) {
// Reconnect or throw
}
Missing Methods
blPop, brPop, and sort are not implemented. Use raw PhpRedis calls if needed:
$this->redis->getConnection()->blPop(['queue:jobs'], 5); // 5-second timeout
Thread Safety
Serialization
json_encode/json_decode) to avoid corruption.Configuration Overrides
Enable Redis Logging
Configure the underlying PhpRedis client to log commands:
dawen_php_redis:
clients:
default:
# ...
log: '%kernel.debug%'
Check Connection Status
Use ping to verify connectivity:
if (!$this->redis->ping()) {
throw new \RuntimeException('Redis connection failed');
}
Monitor Performance
Use info to check Redis server stats:
$info = $this->redis->info();
Custom Clients Extend the base client to add domain-specific methods:
class CustomRedisClient extends AbstractRedisClient
{
public function getUserData(int $userId): array
{
return $this->hGetAll("user:$userId");
}
}
Middleware for Operations Add pre/post-processing logic:
$this->redis->getConnection()->registerScript('myScript.lua');
Event Listeners
Subscribe to Redis events (e.g., key expiration) using the PhpRedis event system:
$this->redis->getConnection()->registerScript('eventScript.lua');
Testing Use a mock Redis client in tests:
$this->redis = $this->createMock(RedisClientInterface::class);
$this->redis->method('get')->willReturn('mocked_value');
How can I help you explore Laravel packages today?