1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kazuakim\Reddish; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Clients. |
7
|
|
|
* |
8
|
|
|
* @property array $config |
9
|
|
|
* @property array $_defaultConfig |
10
|
|
|
* |
11
|
|
|
* @copyright KazuakiM <[email protected]> |
12
|
|
|
* @author KazuakiM <[email protected]> |
13
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
14
|
|
|
* |
15
|
|
|
* @link https://github.com/KazuakiM/reddish |
16
|
|
|
*/ |
17
|
|
|
class Clients extends \Redis //{{{ |
18
|
|
|
{ |
19
|
|
|
protected $config; |
20
|
|
|
private $_defaultConfig = [ |
21
|
|
|
'host' => null, |
22
|
|
|
'port' => 6379, |
23
|
|
|
'timeout' => 0.0, |
24
|
|
|
'password' => null, |
25
|
|
|
'serializer' => \Redis::SERIALIZER_NONE, |
26
|
|
|
'persistent' => false, |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* __construct. |
31
|
|
|
* |
32
|
|
|
* @param array $config |
33
|
|
|
* |
34
|
|
|
* @throws \RedisException |
35
|
|
|
*/ |
36
|
6 |
|
public function __construct(array $config) //{{{ |
37
|
|
|
{ |
38
|
6 |
|
$this->config = array_merge($this->_defaultConfig, $config); |
39
|
|
|
|
40
|
6 |
|
$this->connection(); |
41
|
6 |
|
} //}}} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* connection. |
45
|
|
|
* |
46
|
|
|
* @throws \RedisException |
47
|
|
|
* |
48
|
|
|
* @return \Kazuakim\Reddish\Clients |
49
|
|
|
*/ |
50
|
6 |
|
public function connection(): \Kazuakim\Reddish\Clients //{{{ |
51
|
|
|
{ |
52
|
|
|
// connect |
53
|
6 |
|
if (!$this->config['persistent'] && !@$this->connect($this->config['host'], $this->config['port'], $this->config['timeout'])) { |
54
|
1 |
|
throw new \RedisException('connect errored.'); |
55
|
6 |
|
} elseif (!@$this->pconnect($this->config['host'], $this->config['port'], $this->config['timeout'])) { |
56
|
1 |
|
throw new \RedisException('pconnect errored.'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// auth |
60
|
6 |
|
if (0 < strlen($this->config['password']) && !@$this->auth($this->config['password'])) { |
61
|
1 |
|
throw new \RedisException('auth errored.'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// serializer |
65
|
6 |
|
$this->setSerializer($this->config['serializer']); |
66
|
|
|
|
67
|
6 |
|
return $this; |
68
|
|
|
} //}}} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* setSerializer. |
72
|
|
|
* |
73
|
|
|
* @param int $serializer |
74
|
|
|
* |
75
|
|
|
* @throws \Expectation |
76
|
|
|
* |
77
|
|
|
* @return \Kazuakim\Reddish\Clients |
78
|
|
|
*/ |
79
|
6 |
|
public function setSerializer($serializer): \Kazuakim\Reddish\Clients //{{{ |
80
|
|
|
{ |
81
|
6 |
|
assert(in_array($serializer, self::_getSerializerArray(), true), 'serializer setting errored.'); |
82
|
|
|
|
83
|
6 |
|
$this->setOption(\Redis::OPT_SERIALIZER, $serializer); |
84
|
|
|
|
85
|
6 |
|
return $this; |
86
|
|
|
} //}}} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* close. |
90
|
|
|
*/ |
91
|
2 |
|
public function close() //{{{ |
92
|
|
|
{ |
93
|
2 |
|
if ($this->config['persistent']) { |
94
|
2 |
|
parent::close(); |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
return; |
98
|
|
|
} //}}} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* _getSerializerArray. |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
6 |
|
private static function _getSerializerArray(): array //{{{ |
106
|
|
|
{ |
107
|
6 |
|
if (false === extension_loaded('igbinary')) { |
108
|
|
|
return [ |
109
|
6 |
|
\Redis::SERIALIZER_NONE, |
110
|
|
|
\Redis::SERIALIZER_PHP, |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return [ |
115
|
|
|
\Redis::SERIALIZER_NONE, |
116
|
|
|
\Redis::SERIALIZER_PHP, |
117
|
|
|
\Redis::SERIALIZER_IGBINARY, |
118
|
|
|
]; |
119
|
|
|
} //}}} |
120
|
|
|
} //}}} |
121
|
|
|
|