...
|
...
|
@@ -243,21 +243,7 @@ trait DbQuery { |
|
|
* @time 2023/2/14 16:19
|
|
|
*/
|
|
|
public function count(string|array $sql):int{
|
|
|
if($this->cache){
|
|
|
$key = 'data:'.md5(is_string($sql) ? $sql : json_encode($sql));
|
|
|
if(redis()->has($key)){
|
|
|
return (int) redis()->get($key,0);
|
|
|
}
|
|
|
}
|
|
|
$query = $this->query($sql);
|
|
|
if($query){
|
|
|
$count = $query->fetch(\PDO::FETCH_COLUMN);
|
|
|
if($this->cache){
|
|
|
redis()->set($key,$count,$this->cache);
|
|
|
}
|
|
|
return $count;
|
|
|
}
|
|
|
return 0;
|
|
|
return $this->getCacheData($sql,0,\PDO::FETCH_COLUMN);
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -277,6 +263,37 @@ trait DbQuery { |
|
|
|
|
|
|
|
|
/**
|
|
|
* 是否使用缓存
|
|
|
* @param $sql
|
|
|
* @param string $default
|
|
|
* @param null $flag
|
|
|
* @return int|mixed|string
|
|
|
* @throws DbException
|
|
|
* @author:dc
|
|
|
* @time 2024/9/23 11:57
|
|
|
*/
|
|
|
private function getCacheData($sql, $default = '', $flag = null){
|
|
|
if($this->cache){
|
|
|
$key = 'data:'.md5(is_string($sql) ? $sql : json_encode($sql));
|
|
|
if(redis()->has($key)){
|
|
|
$this->cache = 0;
|
|
|
return (int) redis()->get($key,$default);
|
|
|
}
|
|
|
}
|
|
|
$query = $this->query($sql);
|
|
|
if($query){
|
|
|
$data = $query->fetch($flag);
|
|
|
if($this->cache){
|
|
|
redis()->set($key,$data,$this->cache);
|
|
|
$this->cache = 0;
|
|
|
}
|
|
|
return $data;
|
|
|
}
|
|
|
|
|
|
return $default;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查询一条数据
|
|
|
* @param string|array $sql
|
|
|
* @return mixed|null
|
...
|
...
|
@@ -285,6 +302,8 @@ trait DbQuery { |
|
|
*/
|
|
|
public function first(string|array $sql){
|
|
|
|
|
|
return $this->getCacheData($sql,[]);
|
|
|
|
|
|
$query = $this->query($sql);
|
|
|
|
|
|
if($query){
|
...
|
...
|
|