File tree Expand file tree Collapse file tree 5 files changed +96
-5
lines changed Expand file tree Collapse file tree 5 files changed +96
-5
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ Make sure you installed either of them
1414* [ Consume message] ( #consume-message )
1515* [ Delete queue (purge messages)] ( #delete-queue-purge-messages )
1616* [ Delete topic (purge messages)] ( #delete-topic-purge-messages )
17+ * [ Connect Heroku Redis] ( #connect-heroku-redis )
1718
1819## Installation
1920
@@ -61,6 +62,12 @@ $psrContext = $factory->createContext();
6162
6263// if you have enqueue/enqueue library installed you can use a function from there to create the context
6364$psrContext = \Enqueue\dsn_to_context('redis:');
65+
66+ // pass redis instance directly
67+ $redis = new \Enqueue\Redis\PhpRedis([ /** redis connection options */ ]);
68+ $redis->connect();
69+
70+ $factory = new RedisConnectionFactory($redis);
6471```
6572
6673* With predis library:
@@ -155,4 +162,17 @@ $fooTopic = $psrContext->createTopic('aTopic');
155162$psrContext->deleteTopic($fooTopic);
156163```
157164
165+ ## Connect Heroku Redis
166+
167+ [ Heroku Redis] ( https://devcenter.heroku.com/articles/heroku-redis ) describes how to setup Redis instance on Heroku.
168+ To use it with Enqueue Redis you have to pass REDIS_URL to RedisConnectionFactory constructor.
169+
170+ ``` php
171+ <?php
172+
173+ // REDIS_URL: redis://h:asdfqwer1234asdf@ec2-111-1-1-1.compute-1.amazonaws.com:111
174+
175+ $connection = new \Enqueue\Redis\RedisConnectionFactory(getenv('REDIS_URL'));
176+ ```
177+
158178[ back to index] ( ../index.md )
Original file line number Diff line number Diff line change 22
33namespace Enqueue \Redis ;
44
5+ use Predis \Client ;
56use Predis \ClientInterface ;
67use Predis \Response \ServerException as PRedisServerException ;
78
89class PRedis implements Redis
910{
11+ /**
12+ * @var array
13+ */
14+ private $ config ;
15+
1016 /**
1117 * @var ClientInterface
1218 */
@@ -15,9 +21,19 @@ class PRedis implements Redis
1521 /**
1622 * @param ClientInterface $redis
1723 */
18- public function __construct (ClientInterface $ redis )
24+ public function __construct (array $ config )
1925 {
20- $ this ->redis = $ redis ;
26+ $ this ->config = $ this ->config = array_replace ([
27+ 'host ' => null ,
28+ 'port ' => null ,
29+ 'pass ' => null ,
30+ 'user ' => null ,
31+ 'timeout ' => null ,
32+ 'reserved ' => null ,
33+ 'retry_interval ' => null ,
34+ 'persisted ' => false ,
35+ 'database ' => 0 ,
36+ ], $ config );
2137 }
2238
2339 /**
@@ -63,6 +79,12 @@ public function rpop($key)
6379 */
6480 public function connect ()
6581 {
82+ $ this ->redis = new Client ($ this ->config , ['exceptions ' => true ]);
83+
84+ if ($ this ->config ['pass ' ]) {
85+ $ this ->redis ->auth ($ this ->config ['pass ' ]);
86+ }
87+
6688 $ this ->redis ->connect ();
6789 }
6890
Original file line number Diff line number Diff line change @@ -22,6 +22,8 @@ public function __construct(array $config)
2222 $ this ->config = array_replace ([
2323 'host ' => null ,
2424 'port ' => null ,
25+ 'pass ' => null ,
26+ 'user ' => null ,
2527 'timeout ' => null ,
2628 'reserved ' => null ,
2729 'retry_interval ' => null ,
@@ -82,6 +84,10 @@ public function connect()
8284 );
8385 }
8486
87+ if ($ this ->config ['pass ' ]) {
88+ $ this ->redis ->auth ($ this ->config ['pass ' ]);
89+ }
90+
8591 $ this ->redis ->select ($ this ->config ['database ' ]);
8692 }
8793
Original file line number Diff line number Diff line change 33namespace Enqueue \Redis ;
44
55use Interop \Queue \PsrConnectionFactory ;
6- use Predis \Client ;
76
87class RedisConnectionFactory implements PsrConnectionFactory
98{
@@ -29,17 +28,30 @@ class RedisConnectionFactory implements PsrConnectionFactory
2928 * 'persisted' => bool, Whether it use single persisted connection or open a new one for every context
3029 * 'lazy' => the connection will be performed as later as possible, if the option set to true
3130 * 'database' => Database index to select when connected (default value: 0)
31+ * user - The user name to use.
32+ * pass - Password.
3233 * ].
3334 *
3435 * or
3536 *
3637 * redis:
3738 * redis:?vendor=predis
3839 *
39- * @param array|string|null $config
40+ * or
41+ *
42+ * instance of Enqueue\Redis
43+ *
44+ * @param array|string|Redis|null $config
4045 */
4146 public function __construct ($ config = 'redis: ' )
4247 {
48+ if ($ config instanceof Redis) {
49+ $ this ->redis = $ config ;
50+ $ this ->config = $ this ->defaultConfig ();
51+
52+ return ;
53+ }
54+
4355 if (empty ($ config ) || 'redis: ' === $ config ) {
4456 $ config = [];
4557 } elseif (is_string ($ config )) {
@@ -88,7 +100,7 @@ private function createRedis()
88100 }
89101
90102 if ('predis ' == $ this ->config ['vendor ' ] && false == $ this ->redis ) {
91- $ this ->redis = new PRedis (new Client ( $ this ->config , [ ' exceptions ' => true ]) );
103+ $ this ->redis = new PRedis ($ this ->config );
92104 }
93105
94106 if ('custom ' == $ this ->config ['vendor ' ] && false == $ this ->redis ) {
Original file line number Diff line number Diff line change 22
33namespace Enqueue \Redis \Tests ;
44
5+ use Enqueue \Redis \Redis ;
56use Enqueue \Redis \RedisConnectionFactory ;
67use Enqueue \Test \ClassExtensionTrait ;
78use PHPUnit \Framework \TestCase ;
@@ -45,6 +46,17 @@ public function testThrowIfVendorIsInvalid()
4546 new RedisConnectionFactory (['vendor ' => 'invalidVendor ' ]);
4647 }
4748
49+ public function testCouldBeCreatedWithRedisInstance ()
50+ {
51+ $ redisMock = $ this ->createMock (Redis::class);
52+
53+ $ factory = new RedisConnectionFactory ($ redisMock );
54+ $ this ->assertAttributeSame ($ redisMock , 'redis ' , $ factory );
55+
56+ $ context = $ factory ->createContext ();
57+ $ this ->assertSame ($ redisMock , $ context ->getRedis ());
58+ }
59+
4860 /**
4961 * @dataProvider provideConfigs
5062 *
@@ -141,5 +153,24 @@ public static function provideConfigs()
141153 'redis ' => null ,
142154 ],
143155 ];
156+
157+ // heroku redis
158+ yield [
159+ 'redis://h:asdfqwer1234asdf@ec2-111-1-1-1.compute-1.amazonaws.com:111 ' ,
160+ [
161+ 'host ' => 'ec2-111-1-1-1.compute-1.amazonaws.com ' ,
162+ 'port ' => 111 ,
163+ 'timeout ' => null ,
164+ 'reserved ' => null ,
165+ 'retry_interval ' => null ,
166+ 'vendor ' => 'phpredis ' ,
167+ 'persisted ' => false ,
168+ 'lazy ' => false ,
169+ 'database ' => 0 ,
170+ 'redis ' => null ,
171+ 'user ' => 'h ' ,
172+ 'pass ' => 'asdfqwer1234asdf ' ,
173+ ],
174+ ];
144175 }
145176}
You can’t perform that action at this time.
0 commit comments