作者 邓超

x

... ... @@ -283,22 +283,31 @@ trait DbQuery {
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;
$data = redis()->get($key,$default);
if(!$this->cacheNoData){
if($data || $data === 0){
return $data;
try {
if(redis()->has($key)){
$this->cache = 0;
$data = redis()->get($key,$default);
if(!$this->cacheNoData){
if($data || $data === 0){
return $data;
}
}
}
}
}catch (\Throwable $e){
logs($e->getMessage());
}
}
$query = $this->query($sql);
if($query){
$data = $flag? $query->fetch($flag) : $query->fetch();
if($this->cache){
redis()->set($key,$data,$this->cache);
try {
redis()->set($key, $data, $this->cache);
}catch (\Throwable $e){
logs($e->getMessage());
}
$this->cache = 0;
}
return $data;
... ... @@ -336,17 +345,28 @@ trait DbQuery {
*/
public function all(string|array $sql){
if($this->cache){
$key = 'data:'.md5(is_string($sql) ? $sql : json_encode($sql));
if(redis()->has($key)){
$this->cache = 0;
return redis()->get($key,[]);
try {
$key = 'data:'.md5(is_string($sql) ? $sql : json_encode($sql));
if(redis()->has($key)){
$this->cache = 0;
return redis()->get($key,[]);
}
}catch (\Throwable $e){
logs($e->getMessage());
}
}
$query = $this->query($sql);
if($query){
$data = $query->fetchAll();
if($this->cache){
redis()->set($key,$data,$this->cache);
try {
redis()->set($key,$data,$this->cache);
}catch (\Throwable $e){
logs($e->getMessage());
}
$this->cache = 0;
}
return $data;
... ...