enqueue-dev
enqueue-dev copied to clipboard
feat: allow creating PRedis with a client
This pull request introduces the ability to create a PRedis instance using an already initialized Predis\Client. A new static method createWithClient has been added to facilitate this functionality.
Use Case
If a user already has a configured Predis\Client instance, they can now pass it directly to the PRedis class. This avoids the need to reconfigure the client or duplicate configuration logic. For example, I want to pass through enqueue because I have a predis client configured with Sentinel.
Changes
- Added a check in the constructor to accept a
clientkey in the configuration array. If provided, it validates that the value is an instance ofPredis\Clientand uses it directly. - Added a static method
createWithClient(Client $client): selfto simplify the creation of aPRedisinstance with an existingPredis\Client.
Example Usage
use Predis\Client;
use Enqueue\Redis\PRedis;
$predisClient = new Client(['host' => '127.0.0.1', 'port' => 6379]);
$predis = PRedis::createWithClient($predisClient);
// Pass the PRedis instance to the RedisConnectionFactory
$connectionFactory = new RedisConnectionFactory($predis);
// Get the RedisContext
$redisContext = $connectionFactory->createContext();