RedisPool.php
3.5 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
184
185
186
187
188
189
190
191
<?php
/**
* redis 链接池
* @author:dc
* @time 2023/2/10 17:04
* Class RedisPool
*/
class RedisPool {
/**
* @var RedisPool[]
*/
static $instance = [];
/**
* @var \Redis
*/
private $client;
public function __construct()
{
$this->client = new \Redis();
$this->client->connect(REDIS_HOST,REDIS_PORT);
// 密码
REDIS_PASSWORD && $this->client->auth(REDIS_PASSWORD);
// 用库4
$this->client->select(REDIS_DB);
}
/**
* @param $key
* @return bool|int
* @author:dc
* @time 2023/2/10 18:06
*/
public function has($key)
{
return $this->client->exists($key);
}
/**
* @param $key
* @param null $default
* @return mixed|null
* @author:dc
* @time 2023/2/10 18:04
*/
public function get($key, $default=null)
{
$data = $this->client->get($key);
if($data === null){
return $default;
}
return $this->unserialize($data);
}
/**
* @param $key
* @param $val
* @param null $ttl
* @return bool
* @author:dc
* @time 2023/2/10 18:02
*/
public function set($key,$val,$ttl=null) {
return $this->client->set($key,$this->serialize($val),$ttl);
}
/**
* 如果有key返回false,没有则新增
* @param $key
* @param $val
* @param null $ttl
* @return mixed
* @author:dc
* @time 2023/2/10 17:53
*/
public function add($key,$val,$ttl=null):mixed {
return $this->client->eval(
"return redis.call('exists',KEYS[1])<1 and redis.call('setex',KEYS[1],ARGV[2],ARGV[1])",
[$key, $this->serialize($val), $ttl],
1
);
}
/**
* @param $key
* @param $value
* @return false|int
* @author:dc
* @time 2023/2/13 9:07
*/
public function lPush($key,$value){
return $this->client->lPush($key,$this->serialize($value));
}
/**
* @param $key
* @param $value
* @return false|int
* @author:dc
* @time 2023/2/13 9:07
*/
public function rPush($key,$value){
return $this->client->rPush($key,$this->serialize($value));
}
/**
* @param $key
* @return bool|mixed
* @author:dc
* @time 2023/2/13 9:08
*/
public function lPop($key){
return $this->unserialize($this->client->lPop($key));
}
/**
* @param $key
* @return mixed|string
* @author:dc
* @time 2023/2/13 9:09
*/
public function rPop($key){
return $this->unserialize($this->client->rPop($key));
}
/**
* @param $val
* @return string
* @author:dc
* @time 2023/2/10 17:57
*/
private function serialize($val){
return $val ? serialize($val) : '';
}
/**
* @param $val
* @return mixed
* @author:dc
* @time 2023/2/10 17:58
*/
private function unserialize($val){
return $val ? unserialize($val) : '';
}
public function __destruct()
{
// TODO: Implement __destruct() method.
$this->client->close();
$this->client = null;
}
/**
* @param $cid
* @return RedisPool
* @author:dc
* @time 2023/2/13 9:38
*/
public static function instance($cid){
if(empty(static::$instance[$cid])){
static::$instance[$cid] = new \RedisPool();
}
return static::$instance[$cid];
}
}