RedisQuery.php
4.9 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
namespace Lib;
/**
* redis 的操作
* @author:dc
* @time 2024/5/30 10:50
* Class RedisQuery
* @package Lib
*/
trait RedisQuery {
/**
* @var \Redis
*/
protected $client;
/**
* @param $key
* @return bool|int
* @author:dc
* @time 2023/2/10 18:06
*/
public function has($key)
{
return $this->getClient()->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->getClient()->get($key);
if($data === null){
return $default;
}
return $this->unserialize($data);
}
/**
* 获取原数据
* @param $key
* @return false|mixed|string
* @author:dc
* @time 2023/4/12 16:51
*/
public function getOriginData($key){
return $this->getClient()->get($key);
}
/**
* @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->getClient()->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->getClient()->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->getClient()->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->getClient()->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->getClient()->lPop($key));
}
/**
* @param $key
* @return mixed|string
* @author:dc
* @time 2023/2/13 9:09
*/
public function rPop($key){
return $this->unserialize($this->getClient()->rPop($key));
}
/**
* 自增
* @param $key
* @param null $ttl
* @return int
* @author:dc
* @time 2023/2/17 15:29
*/
public function incr($key, $ttl = null){
if($ttl){
return $this->getClient()->eval(
"
if redis.call('exists',KEYS[1]) == 0 then
local x = redis.call('incr',KEYS[1])
if x then
redis.call('expire',KEYS[1],ARGV[1])
end
return x
else
return redis.call('incr',KEYS[1])
end",
[$key, $ttl],
1
);
}
return $this->getClient()->incr($key);
}
/**
* 自减
* @param $key
* @param null $ttl
* @return int
* @author:dc
* @time 2023/3/16 11:19
*/
public function decr($key,$ttl = null){
if($ttl){
return $this->getClient()->eval(
"
if redis.call('exists',KEYS[1]) == 0 then
local x = redis.call('decr',KEYS[1])
if x then
redis.call('expire',KEYS[1],ARGV[1])
end
return x
else
return redis.call('decr',KEYS[1])
end",
[$key, $ttl],
1
);
}
return $this->getClient()->decr($key);
}
/**
* 删除
* @param $key
* @return int
* @author:dc
* @time 2023/2/14 14:04
*/
public function delete(...$key):int {
return $this->getClient()->del(...$key);
}
/**
* 获取值并删除
* @param $key
* @return mixed
* @author:dc
* @time 2023/3/16 11:36
*/
public function getDel($key){
return $this->getClient()->eval(
"local x = redis.call('get',KEYS[1]);if x then redis.call('del',KEYS[1]) end return x",
[$key],
1
);
}
/**
* @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) : '';
}
/**
* @return \Redis
* @author:dc
* @time 2023/4/12 15:11
*/
public function getClient(){
return $this->client;
}
}