作者 邓超

优化同步

@@ -84,6 +84,8 @@ function start(){ @@ -84,6 +84,8 @@ function start(){
84 $sync->noSyncFolder(); 84 $sync->noSyncFolder();
85 } 85 }
86 86
  87 + $sync->endSyncTime = time();
  88 +
87 $sync->isUidAfter(2)->sync(); 89 $sync->isUidAfter(2)->sync();
88 90
89 $sync = null; 91 $sync = null;
@@ -296,6 +296,17 @@ class SyncMail { @@ -296,6 +296,17 @@ class SyncMail {
296 } 296 }
297 297
298 /** 298 /**
  299 + * 这个时间之后的邮件不同步
  300 + * @var int
  301 + */
  302 + public $endSyncTime = 0;
  303 +
  304 + public function setEndSyncTime($time=0){
  305 + $this->endSyncTime = $time?$time:time();
  306 + return $this;
  307 + }
  308 +
  309 + /**
299 * @param bool $syncMail 310 * @param bool $syncMail
300 * @return bool|void|array 311 * @return bool|void|array
301 * @throws \Exception 312 * @throws \Exception
@@ -326,6 +337,7 @@ class SyncMail { @@ -326,6 +337,7 @@ class SyncMail {
326 $syncNum = []; 337 $syncNum = [];
327 // 循环文件夹 338 // 循环文件夹
328 $startTime = time(); 339 $startTime = time();
  340 + $inbox = null;
329 foreach ($folders->all() as $f){ 341 foreach ($folders->all() as $f){
330 if($this->isStop) return; 342 if($this->isStop) return;
331 // 不同步的文件夹 343 // 不同步的文件夹
@@ -336,18 +348,31 @@ class SyncMail { @@ -336,18 +348,31 @@ class SyncMail {
336 } 348 }
337 if($f->isSelect){ // 是否可以选择 只有可以选中的文件夹才有邮件 349 if($f->isSelect){ // 是否可以选择 只有可以选中的文件夹才有邮件
338 $folder = $this->imap->folder($f); // 选择文件夹后,有状态 350 $folder = $this->imap->folder($f); // 选择文件夹后,有状态
339 - 351 + // 最后同步
  352 + if($folder->getName() == 'INBOX'){
  353 + $inbox = $folder;
  354 + continue;
  355 + }
340 // 是否有邮件 有邮件才继续 356 // 是否有邮件 有邮件才继续
341 if ($folder->getTotal()){ 357 if ($folder->getTotal()){
342 $num = $this->mail($folder); 358 $num = $this->mail($folder);
343 if($num){ 359 if($num){
344 $syncNum[$folder->getName()] = $num; 360 $syncNum[$folder->getName()] = $num;
345 -// _echo($this->emailId().' ===> '.$folder->getName().' ===> '.$num);  
346 } 361 }
347 } 362 }
348 363
349 } 364 }
350 } 365 }
  366 + // 收件箱最后同步 只需要保证收件箱最后同步即可
  367 + if($inbox){
  368 + if ($inbox->getTotal()){
  369 + $num = $this->mail($inbox);
  370 + if($num){
  371 + $syncNum[$inbox->getName()] = $num;
  372 + }
  373 + }
  374 + }
  375 +
351 _echo($this->emailId()."=>runTime:".(time()-$startTime).'s => total: '.(array_sum($syncNum))); 376 _echo($this->emailId()."=>runTime:".(time()-$startTime).'s => total: '.(array_sum($syncNum)));
352 return $syncNum; 377 return $syncNum;
353 } 378 }
@@ -523,6 +548,13 @@ class SyncMail { @@ -523,6 +548,13 @@ class SyncMail {
523 continue; 548 continue;
524 } 549 }
525 550
  551 + // 超过时间的不插入
  552 + if($this->endSyncTime){
  553 + if($data['udate'] > $this->endSyncTime){
  554 + continue;
  555 + }
  556 + }
  557 +
526 if(empty($data['from'])&&empty($data['subject'])){ 558 if(empty($data['from'])&&empty($data['subject'])){
527 logs(['邮件没有主题和发件人',[$folder_id,$item->uid],$data??[],$item->header->getRaw()]); 559 logs(['邮件没有主题和发件人',[$folder_id,$item->uid],$data??[],$item->header->getRaw()]);
528 } 560 }