|
@@ -243,21 +243,7 @@ trait DbQuery { |
|
@@ -243,21 +243,7 @@ trait DbQuery { |
243
|
* @time 2023/2/14 16:19
|
243
|
* @time 2023/2/14 16:19
|
244
|
*/
|
244
|
*/
|
245
|
public function count(string|array $sql):int{
|
245
|
public function count(string|array $sql):int{
|
246
|
- if($this->cache){
|
|
|
247
|
- $key = 'data:'.md5(is_string($sql) ? $sql : json_encode($sql));
|
|
|
248
|
- if(redis()->has($key)){
|
|
|
249
|
- return (int) redis()->get($key,0);
|
|
|
250
|
- }
|
|
|
251
|
- }
|
|
|
252
|
- $query = $this->query($sql);
|
|
|
253
|
- if($query){
|
|
|
254
|
- $count = $query->fetch(\PDO::FETCH_COLUMN);
|
|
|
255
|
- if($this->cache){
|
|
|
256
|
- redis()->set($key,$count,$this->cache);
|
|
|
257
|
- }
|
|
|
258
|
- return $count;
|
|
|
259
|
- }
|
|
|
260
|
- return 0;
|
246
|
+ return $this->getCacheData($sql,0,\PDO::FETCH_COLUMN);
|
261
|
}
|
247
|
}
|
262
|
|
248
|
|
263
|
/**
|
249
|
/**
|
|
@@ -277,6 +263,37 @@ trait DbQuery { |
|
@@ -277,6 +263,37 @@ trait DbQuery { |
277
|
|
263
|
|
278
|
|
264
|
|
279
|
/**
|
265
|
/**
|
|
|
266
|
+ * 是否使用缓存
|
|
|
267
|
+ * @param $sql
|
|
|
268
|
+ * @param string $default
|
|
|
269
|
+ * @param null $flag
|
|
|
270
|
+ * @return int|mixed|string
|
|
|
271
|
+ * @throws DbException
|
|
|
272
|
+ * @author:dc
|
|
|
273
|
+ * @time 2024/9/23 11:57
|
|
|
274
|
+ */
|
|
|
275
|
+ private function getCacheData($sql, $default = '', $flag = null){
|
|
|
276
|
+ if($this->cache){
|
|
|
277
|
+ $key = 'data:'.md5(is_string($sql) ? $sql : json_encode($sql));
|
|
|
278
|
+ if(redis()->has($key)){
|
|
|
279
|
+ $this->cache = 0;
|
|
|
280
|
+ return (int) redis()->get($key,$default);
|
|
|
281
|
+ }
|
|
|
282
|
+ }
|
|
|
283
|
+ $query = $this->query($sql);
|
|
|
284
|
+ if($query){
|
|
|
285
|
+ $data = $query->fetch($flag);
|
|
|
286
|
+ if($this->cache){
|
|
|
287
|
+ redis()->set($key,$data,$this->cache);
|
|
|
288
|
+ $this->cache = 0;
|
|
|
289
|
+ }
|
|
|
290
|
+ return $data;
|
|
|
291
|
+ }
|
|
|
292
|
+
|
|
|
293
|
+ return $default;
|
|
|
294
|
+ }
|
|
|
295
|
+
|
|
|
296
|
+ /**
|
280
|
* 查询一条数据
|
297
|
* 查询一条数据
|
281
|
* @param string|array $sql
|
298
|
* @param string|array $sql
|
282
|
* @return mixed|null
|
299
|
* @return mixed|null
|
|
@@ -285,6 +302,8 @@ trait DbQuery { |
|
@@ -285,6 +302,8 @@ trait DbQuery { |
285
|
*/
|
302
|
*/
|
286
|
public function first(string|array $sql){
|
303
|
public function first(string|array $sql){
|
287
|
|
304
|
|
|
|
305
|
+ return $this->getCacheData($sql,[]);
|
|
|
306
|
+
|
288
|
$query = $this->query($sql);
|
307
|
$query = $this->query($sql);
|
289
|
|
308
|
|
290
|
if($query){
|
309
|
if($query){
|