RedisCli.php
4.0 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
<?php
namespace jiaoyin;
use Swoole\Database\RedisConfig;
use Swoole\Database\RedisPool;
class RedisCli
{
private $pool;
public $subRedis = null;
public function __construct($host, $port, $password, $dbIndex, $num = 20)
{
$this->pool = new RedisPool((new RedisConfig)
->withHost($host)
->withPort($port)
->withAuth($password)
->withDbIndex($dbIndex),
$num //默认64个连接池
);
}
//集合存数据
public function sAdd($key, $member)
{
$redis = $this->pool->get();
try {
$res = $redis->sAdd($key, $member);
} catch (\RedisException $e) {
return false;
}
$this->pool->put($redis);
return $res;
}
//获取集合
public function sMembers($key)
{
$redis = $this->pool->get();
try {
$res = $redis->sMembers($key);
} catch (\RedisException $e) {
return false;
}
$this->pool->put($redis);
return $res;
}
//移除集合成员
public function sRem($key, $member)
{
$redis = $this->pool->get();
try {
$res = $redis->sRem($key, $member);
} catch (\RedisException $e) {
return false;
}
$this->pool->put($redis);
return $res;
}
//哈希 键 字段 值 设置
public function hSet($key, $field, $value)
{
$redis = $this->pool->get();
try {
$res = $redis->hSet($key, $field, $value);
} catch (\RedisException $e) {
return false;
}
$this->pool->put($redis);
return $res;
}
//获取 键对应字段的值
public function hGet($key, $field)
{
$redis = $this->pool->get();
try {
$res = $redis->hGet($key, $field);
} catch (\RedisException $e) {
return false;
}
$this->pool->put($redis);
return $res;
}
//获取键所有字段和值
public function hGetAll($key)
{
$redis = $this->pool->get();
try {
$res = $redis->hGetAll($key);
} catch (\RedisException $e) {
return false;
}
$this->pool->put($redis);
return $res;
}
//删除一个值
public function hDel($key, $field)
{
$redis = $this->pool->get();
try {
$res = $redis->hDel($key, $field);
} catch (\RedisException $e) {
return false;
}
$this->pool->put($redis);
return $res;
}
/*
* 发布订阅等
*/
//将信息发送到指定的频道
public function publish($channel, $message)
{
$redis = $this->pool->get();
try {
$res = $redis->publish($channel, $message);
} catch (\RedisException $e) {
return false;
}
$this->pool->put($redis);
return $res;
}
public function subscribe($channels, callable $onMessage)
{
$redis = $this->pool->get();
$this->subRedis = $redis;
try {
$res = $redis->subscribe($channels, function ($redis, $channel, $message) use ($onMessage) {
call_user_func($onMessage, $redis, $channel, $message);
});
} catch (\RedisException $e) {
return false;
}
$this->subRedis = null;
$this->pool->put($redis);
return $res;
}
public function psubscribe($channels, callable $onMessage)
{
$redis = $this->pool->get();
$this->subRedis = $redis;
try {
$res = $redis->psubscribe($channels, function ($redis, $pattern, $channel, $message) use ($onMessage) {
call_user_func($onMessage, $pattern, $redis, $channel, $message);
});
} catch (\RedisException $e) {
return false;
}
$this->subRedis = null;
$this->pool->put($redis);
return $res;
}
}