Repository navigation
Expand file tree
/
Copy pathRedisConnectionFactory.php
More file actions
144 lines (123 loc) · 3.91 KB
/
Copy pathRedisConnectionFactory.php
File metadata and controls
144 lines (123 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace Enqueue\Redis;
use Interop\Queue\PsrConnectionFactory;
use Predis\Client;
class RedisConnectionFactory implements PsrConnectionFactory
{
/**
* @var array
*/
private $config;
/**
* @var \Redis
*/
private $redis;
/**
* $config = [
* 'host' => can be a host, or the path to a unix domain socket
* 'port' => optional
* 'timeout' => value in seconds (optional, default is 0.0 meaning unlimited)
* 'reserved' => should be null if $retry_interval is specified
* 'retry_interval' => retry interval in milliseconds.
* 'vendor' => 'The library used internally to interact with Redis server
* 'persisted' => bool, Whether it use single persisted connection or open a new one for every context
* 'lazy' => the connection will be performed as later as possible, if the option set to true
* ].
*
* or
*
* redis:
* redis:?vendor=predis
*
* @param array|string|null $config
*/
public function __construct($config = 'redis:')
{
if (empty($config) || 'redis:' === $config) {
$config = [];
} elseif (is_string($config)) {
$config = $this->parseDsn($config);
} elseif (is_array($config)) {
} else {
throw new \LogicException('The config must be either an array of options, a DSN string or null');
}
$this->config = array_replace($this->defaultConfig(), $config);
$supportedVendors = ['predis', 'phpredis'];
if (false == in_array($this->config['vendor'], $supportedVendors, true)) {
throw new \LogicException(sprintf(
'Unsupported redis vendor given. It must be either "%s". Got "%s"',
implode('", "', $supportedVendors),
$this->config['vendor']
));
}
}
/**
* {@inheritdoc}
*
* @return RedisContext
*/
public function createContext()
{
if ($this->config['lazy']) {
return new RedisContext(function () {
return $this->createRedis();
});
}
return new RedisContext($this->createRedis());
}
/**
* @return Redis
*/
private function createRedis()
{
if (false == $this->redis) {
if ('phpredis' == $this->config['vendor'] && false == $this->redis) {
$this->redis = new PhpRedis($this->config);
}
if ('predis' == $this->config['vendor'] && false == $this->redis) {
$this->redis = new PRedis(new Client($this->config, ['exceptions' => true]));
}
$this->redis->connect();
}
return $this->redis;
}
/**
* @param string $dsn
*
* @return array
*/
private function parseDsn($dsn)
{
if (false === strpos($dsn, 'redis:')) {
throw new \LogicException(sprintf('The given DSN "%s" is not supported. Must start with "redis:".', $dsn));
}
if (false === $config = parse_url($dsn)) {
throw new \LogicException(sprintf('Failed to parse DSN "%s"', $dsn));
}
if ($query = parse_url($dsn, PHP_URL_QUERY)) {
$queryConfig = [];
parse_str($query, $queryConfig);
$config = array_replace($queryConfig, $config);
}
unset($config['query'], $config['scheme']);
$config['lazy'] = empty($config['lazy']) ? false : true;
$config['persisted'] = empty($config['persisted']) ? false : true;
return $config;
}
/**
* @return array
*/
private function defaultConfig()
{
return [
'host' => 'localhost',
'port' => 6379,
'timeout' => null,
'reserved' => null,
'retry_interval' => null,
'vendor' => 'phpredis',
'persisted' => false,
'lazy' => true,
];
}
}