RedisCli.php
5.2 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
namespace Jiaoyin;
use Swoole\Database\RedisConfig;
use Swoole\Database\RedisPool;
use function Jiaoyin\output;
class RedisCli
{
private $pool;
public $subRedis = null;
private $host;
private $port;
private $dbIndex;
public function __construct($host, $port, $password, $dbIndex, $num = 40)
{
$this->host = $host;
$this->port = $port;
$this->dbIndex = $dbIndex;
$this->pool = new RedisPool(
(new RedisConfig)
->withHost($host)
->withPort($port)
->withAuth($password)
->withDbIndex($dbIndex),
$num
);
}
private function exec(callable $fn, string $method)
{
$redis = $this->pool->get(); // 从池子里取连接
$isHealthy = false; // 标记连接是否健康
try {
$res = $fn($redis); // 执行用户逻辑
$isHealthy = true; // 没报错,认为健康
return $res;
} catch (\RedisException $e) {
$this->logError($method, $e);
// 遇到异常直接销毁该连接,不放回池子
try {
$redis->close();
} catch (\Throwable $t) {
}
return false;
} catch (\Throwable $e) {
// 其它错误(比如逻辑 bug),也不要放坏连接回池
$this->logError($method, $e);
try {
$redis->close();
} catch (\Throwable $t) {
}
return false;
} finally {
// 只有当连接执行成功且依然是连接状态时,才放回池子
if ($isHealthy && $redis && $redis->isConnected()) {
$this->pool->put($redis);
}
}
}
// ------------------ 集合 ------------------
public function sAdd($key, $member)
{
return $this->exec(fn($r) => $r->sAdd($key, $member), "sAdd");
}
public function sMembers($key)
{
return $this->exec(fn($r) => $r->sMembers($key), "sMembers");
}
public function sRem($key, $member)
{
return $this->exec(fn($r) => $r->sRem($key, $member), "sRem");
}
// ------------------ 哈希 ------------------
public function hSet($key, $field, $value)
{
return $this->exec(fn($r) => $r->hSet($key, $field, $value), "hSet");
}
public function hMSet($key, $field)
{
return $this->exec(fn($r) => $r->hMSet($key, $field), "hMSet");
}
public function hMGet($key, $field)
{
return $this->exec(fn($r) => $r->hMGet($key, $field), "hMGet");
}
public function hGet($key, $field)
{
return $this->exec(fn($r) => $r->hGet($key, $field), "hGet");
}
public function hGetAll($key)
{
return $this->exec(fn($r) => $r->hGetAll($key), "hGetAll");
}
public function hDel($key, $field)
{
return $this->exec(fn($r) => $r->hDel($key, $field), "hDel");
}
// ------------------ 发布 ------------------
public function publish($channel, $message)
{
return $this->exec(fn($r) => $r->publish($channel, $message), "publish");
}
// ------------------ 订阅模式(独立连接) ------------------
public function subscribe($channels, callable $onMessage)
{
$redis = $this->pool->get();
$this->subRedis = $redis;
try {
return $redis->subscribe($channels, function ($redis, $channel, $message) use ($onMessage) {
$onMessage($redis, $channel, $message);
});
} catch (\RedisException $e) {
$this->logError("subscribe", $e);
return false;
} finally {
$this->subRedis = null;
// 不放回池子,订阅是阻塞连接
}
}
public function psubscribe($channels, callable $onMessage)
{
$redis = $this->pool->get();
$this->subRedis = $redis;
try {
return $redis->psubscribe($channels, function ($redis, $pattern, $channel, $message) use ($onMessage) {
$onMessage($pattern, $redis, $channel, $message);
});
} catch (\RedisException $e) {
$this->logError("psubscribe", $e);
return false;
} finally {
$this->subRedis = null;
// 不放回池子
}
}
// ------------------ 健康检查 ------------------
public function ping()
{
return $this->exec(fn($r) => $r->ping(), "ping");
}
// ------------------ 错误日志 ------------------
private function logError($method, \RedisException $e)
{
if (function_exists('swoole_get_worker_id')) {
$workerId = swoole_get_worker_id();
$processInfo = "WorkerID:{$workerId}";
} else {
$pid = getmypid();
$uid = function_exists('posix_getuid') ? posix_getuid() : 'N/A';
$processInfo = "PID:{$pid}, UID:{$uid}";
}
output(sprintf(
"[RedisCli][%s][%s:%d][db:%d] %s failed: %s",
$processInfo,
$this->host,
$this->port,
$this->dbIndex,
$method,
$e->getMessage()
));
}
}