|
@@ -28,6 +28,12 @@ trait DbQuery { |
|
@@ -28,6 +28,12 @@ trait DbQuery { |
28
|
*/
|
28
|
*/
|
29
|
protected int $cache = 0;
|
29
|
protected int $cache = 0;
|
30
|
|
30
|
|
|
|
31
|
+ /**
|
|
|
32
|
+ * 没有数据时 也缓存
|
|
|
33
|
+ * @var bool
|
|
|
34
|
+ */
|
|
|
35
|
+ protected bool $cacheNoData = true;
|
|
|
36
|
+
|
31
|
|
37
|
|
32
|
public function getClient()
|
38
|
public function getClient()
|
33
|
{
|
39
|
{
|
|
@@ -51,8 +57,9 @@ trait DbQuery { |
|
@@ -51,8 +57,9 @@ trait DbQuery { |
51
|
* @author:dc
|
57
|
* @author:dc
|
52
|
* @time 2024/8/14 14:04
|
58
|
* @time 2024/8/14 14:04
|
53
|
*/
|
59
|
*/
|
54
|
- public function cache(int $ttl){
|
60
|
+ public function cache(int $ttl,bool $noData = true){
|
55
|
$this->cache = $ttl;
|
61
|
$this->cache = $ttl;
|
|
|
62
|
+ $this->cacheNoData = $noData;
|
56
|
return $this;
|
63
|
return $this;
|
57
|
}
|
64
|
}
|
58
|
|
65
|
|
|
@@ -278,7 +285,13 @@ trait DbQuery { |
|
@@ -278,7 +285,13 @@ trait DbQuery { |
278
|
$key = 'data:'.md5(is_string($sql) ? $sql : json_encode($sql));
|
285
|
$key = 'data:'.md5(is_string($sql) ? $sql : json_encode($sql));
|
279
|
if(redis()->has($key)){
|
286
|
if(redis()->has($key)){
|
280
|
$this->cache = 0;
|
287
|
$this->cache = 0;
|
281
|
- return redis()->get($key,$default);
|
288
|
+ $data = redis()->get($key,$default);
|
|
|
289
|
+ if(!$this->cacheNoData){
|
|
|
290
|
+ if($data || $data === 0){
|
|
|
291
|
+ return $data;
|
|
|
292
|
+ }
|
|
|
293
|
+ }
|
|
|
294
|
+
|
282
|
}
|
295
|
}
|
283
|
}
|
296
|
}
|
284
|
$query = $this->query($sql);
|
297
|
$query = $this->query($sql);
|