作者 邓超

优化同步

... ... @@ -84,6 +84,8 @@ function start(){
$sync->noSyncFolder();
}
$sync->endSyncTime = time();
$sync->isUidAfter(2)->sync();
$sync = null;
... ...
... ... @@ -296,6 +296,17 @@ class SyncMail {
}
/**
* 这个时间之后的邮件不同步
* @var int
*/
public $endSyncTime = 0;
public function setEndSyncTime($time=0){
$this->endSyncTime = $time?$time:time();
return $this;
}
/**
* @param bool $syncMail
* @return bool|void|array
* @throws \Exception
... ... @@ -326,6 +337,7 @@ class SyncMail {
$syncNum = [];
// 循环文件夹
$startTime = time();
$inbox = null;
foreach ($folders->all() as $f){
if($this->isStop) return;
// 不同步的文件夹
... ... @@ -336,18 +348,31 @@ class SyncMail {
}
if($f->isSelect){ // 是否可以选择 只有可以选中的文件夹才有邮件
$folder = $this->imap->folder($f); // 选择文件夹后,有状态
// 最后同步
if($folder->getName() == 'INBOX'){
$inbox = $folder;
continue;
}
// 是否有邮件 有邮件才继续
if ($folder->getTotal()){
$num = $this->mail($folder);
if($num){
$syncNum[$folder->getName()] = $num;
// _echo($this->emailId().' ===> '.$folder->getName().' ===> '.$num);
}
}
}
}
// 收件箱最后同步 只需要保证收件箱最后同步即可
if($inbox){
if ($inbox->getTotal()){
$num = $this->mail($inbox);
if($num){
$syncNum[$inbox->getName()] = $num;
}
}
}
_echo($this->emailId()."=>runTime:".(time()-$startTime).'s => total: '.(array_sum($syncNum)));
return $syncNum;
}
... ... @@ -523,6 +548,13 @@ class SyncMail {
continue;
}
// 超过时间的不插入
if($this->endSyncTime){
if($data['udate'] > $this->endSyncTime){
continue;
}
}
if(empty($data['from'])&&empty($data['subject'])){
logs(['邮件没有主题和发件人',[$folder_id,$item->uid],$data??[],$item->header->getRaw()]);
}
... ...