-
Notifications
You must be signed in to change notification settings - Fork 439
/
Copy pathPhpRedis.php
114 lines (100 loc) · 2.32 KB
/
PhpRedis.php
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
<?php
namespace Enqueue\Redis;
class PhpRedis implements Redis
{
/**
* @var \Redis
*/
private $redis;
/**
* @var array
*/
private $config;
/**
* @param array $config
*/
public function __construct(array $config)
{
$this->config = array_replace([
'host' => null,
'port' => null,
'pass' => null,
'user' => null,
'timeout' => null,
'reserved' => null,
'retry_interval' => null,
'persisted' => false,
'database' => 0,
], $config);
}
/**
* {@inheritdoc}
*/
public function lpush($key, $value)
{
if (false == $this->redis->lPush($key, $value)) {
throw new ServerException($this->redis->getLastError());
}
}
/**
* {@inheritdoc}
*/
public function brpop($key, $timeout)
{
if ($result = $this->redis->brPop([$key], $timeout)) {
return $result[1];
}
}
/**
* {@inheritdoc}
*/
public function rpop($key)
{
return $this->redis->rPop($key);
}
/**
* {@inheritdoc}
*/
public function connect()
{
if (false == $this->redis) {
$this->redis = new \Redis();
if ($this->config['persisted']) {
$this->redis->pconnect(
$this->config['host'],
$this->config['port'],
$this->config['timeout']
);
} else {
$this->redis->connect(
$this->config['host'],
$this->config['port'],
$this->config['timeout'],
$this->config['reserved'],
$this->config['retry_interval']
);
}
if ($this->config['pass']) {
$this->redis->auth($this->config['pass']);
}
$this->redis->select($this->config['database']);
}
return $this->redis;
}
/**
* {@inheritdoc}
*/
public function disconnect()
{
if ($this->redis) {
$this->redis->close();
}
}
/**
* {@inheritdoc}
*/
public function del($key)
{
$this->redis->del($key);
}
}