作者 邓超

h

@@ -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
@@ -277,7 +284,13 @@ trait DbQuery { @@ -277,7 +284,13 @@ trait DbQuery {
277 $key = 'data:'.md5(is_string($sql) ? $sql : json_encode($sql)); 284 $key = 'data:'.md5(is_string($sql) ? $sql : json_encode($sql));
278 if(redis()->has($key)){ 285 if(redis()->has($key)){
279 $this->cache = 0; 286 $this->cache = 0;
280 - return redis()->get($key,$default); 287 + $data = redis()->get($key,$default);
  288 + if(!$this->cacheNoData){
  289 + if($data || $data === 0){
  290 + return $data;
  291 + }
  292 + }
  293 +
281 } 294 }
282 } 295 }
283 $query = $this->query($sql); 296 $query = $this->query($sql);
@@ -146,47 +146,47 @@ class Mail { @@ -146,47 +146,47 @@ class Mail {
146 $pid = $db->value(folderSql::has(['uuid'=>md5($email_id.$folder['pname'])])); 146 $pid = $db->value(folderSql::has(['uuid'=>md5($email_id.$folder['pname'])]));
147 $pid = $pid ? $pid : 0; 147 $pid = $pid ? $pid : 0;
148 // try { 148 // try {
149 - $folder_name = '';  
150 - // 已发送  
151 - if(in_array('Send',$folder['check'])){  
152 - $folder_name = folderAlias('Send');  
153 - }  
154 - // 草稿  
155 - elseif(in_array('Drafts',$folder['check'])){  
156 - $folder_name = folderAlias('Drafts');  
157 - }  
158 - // 垃圾  
159 - elseif(in_array('Junk',$folder['check'])){  
160 - $folder_name = folderAlias('Junk');  
161 - }  
162 - // 回收站  
163 - elseif(in_array('Trash',$folder['check'])){  
164 - $folder_name = folderAlias('Trash');  
165 - } 149 + $folder_name = '';
  150 + // 已发送
  151 + if(in_array('Send',$folder['check'])){
  152 + $folder_name = folderAlias('Send');
  153 + }
  154 + // 草稿
  155 + elseif(in_array('Drafts',$folder['check'])){
  156 + $folder_name = folderAlias('Drafts');
  157 + }
  158 + // 垃圾
  159 + elseif(in_array('Junk',$folder['check'])){
  160 + $folder_name = folderAlias('Junk');
  161 + }
  162 + // 回收站
  163 + elseif(in_array('Trash',$folder['check'])){
  164 + $folder_name = folderAlias('Trash');
  165 + }
166 166
167 - if(!$folder_name){  
168 - $fn = explode('/',$folder['parseFolder']);  
169 - $folder_name = folderAlias(end($fn));  
170 - }  
171 - if(!$db->count(folderSql::has(['uuid'=>$uuid]))){  
172 - $db->insert(folderSql::$table,[  
173 - 'email_id' => $email_id,  
174 - 'folder' => folderAlias($folder_name),  
175 - 'origin_folder' => $folder['folder'],  
176 - 'uuid' => $uuid,  
177 - 'pid' => $pid  
178 - ],false);  
179 - }else{  
180 - $db->update(folderSql::$table,[  
181 - 'email_id' => $email_id,  
182 - 'folder' => folderAlias($folder_name),  
183 - 'origin_folder' => $folder['folder'],  
184 - 'uuid' => $uuid,  
185 - 'pid' => $pid  
186 - ],dbWhere(['email_id' => $email_id,'uuid' => $uuid]),false);  
187 - } 167 + if(!$folder_name){
  168 + $fn = explode('/',$folder['parseFolder']);
  169 + $folder_name = folderAlias(end($fn));
  170 + }
  171 + if(!$db->count(folderSql::has(['uuid'=>$uuid]))){
  172 + $db->insert(folderSql::$table,[
  173 + 'email_id' => $email_id,
  174 + 'folder' => folderAlias($folder_name),
  175 + 'origin_folder' => $folder['folder'],
  176 + 'uuid' => $uuid,
  177 + 'pid' => $pid
  178 + ],false);
  179 + }else{
  180 + $db->update(folderSql::$table,[
  181 + 'email_id' => $email_id,
  182 + 'folder' => folderAlias($folder_name),
  183 + 'origin_folder' => $folder['folder'],
  184 + 'uuid' => $uuid,
  185 + 'pid' => $pid
  186 + ],dbWhere(['email_id' => $email_id,'uuid' => $uuid]),false);
  187 + }
188 // }catch (\Throwable $e){ 188 // }catch (\Throwable $e){
189 - // 这里就不处理失败了 189 + // 这里就不处理失败了
190 // } 190 // }
191 191
192 unset($folders[$fk]); 192 unset($folders[$fk]);
@@ -251,7 +251,7 @@ class Mail { @@ -251,7 +251,7 @@ class Mail {
251 $blacklist = redis()->get('blacklist:'.$email_id); 251 $blacklist = redis()->get('blacklist:'.$email_id);
252 $blackFolder = ''; 252 $blackFolder = '';
253 if($blacklist){ 253 if($blacklist){
254 - $blackFolder = $db->value(folderSql::originFolder($email_id,'垃圾箱')); 254 + $blackFolder = $db->cache(86400*30)->value(folderSql::originFolder($email_id,'垃圾箱'));
255 } 255 }
256 256
257 // 257 //
@@ -281,11 +281,10 @@ class Mail { @@ -281,11 +281,10 @@ class Mail {
281 281
282 282
283 $uids = array_column($uids,'UID'); 283 $uids = array_column($uids,'UID');
284 - $existsUids = $db->all(listsSql::getUids($email_id,$folder_id,$uids));  
285 - if($existsUids){  
286 - $existsUids = array_column($existsUids,'uid');  
287 - // 获取不存在数据库的uid  
288 - $uids = array_diff($uids,$existsUids); 284 + foreach ($uids as $k=>$uid){
  285 + if($db->cache(86400*30,false)->value(listsSql::first(dbWhere(['email_id'=>$email_id,'folder_id'=>$folder_id,'uid'=>$uid]),'count(*) as c'))){
  286 + unset($uids[$k]);
  287 + }
289 } 288 }
290 289
291 290
@@ -313,7 +312,7 @@ class Mail { @@ -313,7 +312,7 @@ class Mail {
313 $unseen = $db->count(listsSql::listCount(dbWhere([ 312 $unseen = $db->count(listsSql::listCount(dbWhere([
314 'seen' => 0, 313 'seen' => 0,
315 'deleted' => 0, 314 'deleted' => 0,
316 - 'email_id' => $email_id, 315 + // 'email_id' => $email_id,
317 'folder_id' => $folder_id, 316 'folder_id' => $folder_id,
318 ]))); 317 ])));
319 $db->update( 318 $db->update(
@@ -445,7 +444,7 @@ class Mail { @@ -445,7 +444,7 @@ class Mail {
445 if(php_sapi_name()=='cli'){ 444 if(php_sapi_name()=='cli'){
446 go(function ($id,$header,$data){ 445 go(function ($id,$header,$data){
447 new syncMail($id,$header,$data); 446 new syncMail($id,$header,$data);
448 - }, 447 + },
449 ...[$id,$header,$data] 448 ...[$id,$header,$data]
450 ); 449 );
451 } 450 }