layout | title | parent | nav_order |
---|---|---|---|
default |
Redis |
Transports |
3 |
{% include support.md %}
The transport uses Redis as a message broker. It creates a collection (a queue or topic) there. Pushes messages to the tail of the collection and pops from the head. The transport works with phpredis php extension or predis library. Make sure you installed either of them
Features:
- Configure with DSN string
- Delay strategies out of the box
- Recovery&Redelivery support
- Expiration support
- Delaying support
- Interchangeable with other Queue Interop implementations
- Supports Subscription consumer
Parts:
- Installation
- Create context
- Send message to topic
- Send message to queue
- Send expiration message
- Send delayed message
- Consume message
- Delete queue (purge messages)
- Delete topic (purge messages)
- Connect Heroku Redis
- With php redis extension:
$ apt-get install php-redis
$ composer require enqueue/redis
- With predis library:
$ composer require enqueue/redis predis/predis:^1
- With php redis extension:
<?php
use Enqueue\Redis\RedisConnectionFactory;
// connects to localhost
$factory = new RedisConnectionFactory();
// same as above
$factory = new RedisConnectionFactory('redis:');
// same as above
$factory = new RedisConnectionFactory([]);
// connect to Redis at example.com port 1000 using phpredis extension
$factory = new RedisConnectionFactory([
'host' => 'example.com',
'port' => 1000,
'scheme_extensions' => ['phpredis'],
]);
// same as above but given as DSN string
$factory = new RedisConnectionFactory('redis+phpredis://example.com:1000');
$context = $factory->createContext();
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$context = (new \Enqueue\ConnectionFactoryFactory())->create('redis:')->createContext();
// pass redis instance directly
$redis = new \Enqueue\Redis\PhpRedis([ /** redis connection options */ ]);
$redis->connect();
// Secure\TLS connection. Works only with predis library. Note second "S" in scheme.
$factory = new RedisConnectionFactory('rediss+predis://user:pass@host/0');
$factory = new RedisConnectionFactory($redis);
- With predis library:
<?php
use Enqueue\Redis\RedisConnectionFactory;
$connectionFactory = new RedisConnectionFactory([
'host' => 'localhost',
'port' => 6379,
'scheme_extensions' => ['predis'],
]);
$context = $connectionFactory->createContext();
- With predis and custom options:
It gives you more control over vendor specific features.
<?php
use Enqueue\Redis\RedisConnectionFactory;
use Enqueue\Redis\PRedis;
$config = [
'host' => 'localhost',
'port' => 6379,
'predis_options' => [
'prefix' => 'ns:'
]
];
$redis = new PRedis($config);
$factory = new RedisConnectionFactory($redis);
<?php
/** @var \Enqueue\Redis\RedisContext $context */
$fooTopic = $context->createTopic('aTopic');
$message = $context->createMessage('Hello world!');
$context->createProducer()->send($fooTopic, $message);
<?php
/** @var \Enqueue\Redis\RedisContext $context */
$fooQueue = $context->createQueue('aQueue');
$message = $context->createMessage('Hello world!');
$context->createProducer()->send($fooQueue, $message);
<?php
/** @var \Enqueue\Redis\RedisContext $context */
/** @var \Enqueue\Redis\RedisDestination $fooQueue */
$message = $context->createMessage('Hello world!');
$context->createProducer()
->setTimeToLive(60000) // 60 sec
//
->send($fooQueue, $message)
;
<?php
/** @var \Enqueue\Redis\RedisContext $context */
/** @var \Enqueue\Redis\RedisDestination $fooQueue */
$message = $context->createMessage('Hello world!');
$context->createProducer()
->setDeliveryDelay(5000) // 5 sec
->send($fooQueue, $message)
;
<?php
/** @var \Enqueue\Redis\RedisContext $context */
$fooQueue = $context->createQueue('aQueue');
$consumer = $context->createConsumer($fooQueue);
$message = $consumer->receive();
// process a message
$consumer->acknowledge($message);
//$consumer->reject($message);
<?php
/** @var \Enqueue\Redis\RedisContext $context */
$fooQueue = $context->createQueue('aQueue');
$context->deleteQueue($fooQueue);
<?php
/** @var \Enqueue\Redis\RedisContext $context */
$fooTopic = $context->createTopic('aTopic');
$context->deleteTopic($fooTopic);
Heroku Redis describes how to setup Redis instance on Heroku. To use it with Enqueue Redis you have to pass REDIS_URL to RedisConnectionFactory constructor.
<?php
// REDIS_URL: redis://h:[email protected]:111
$connection = new \Enqueue\Redis\RedisConnectionFactory(getenv('REDIS_URL'));