作者 邓超

优化 同步

... ... @@ -733,7 +733,7 @@ class Imap {
}elseif($header2Body=='body'){
$filed = 'RFC822.TEXT';
}else{
$filed = 'UID FLAGS INTERNALDATE';
$filed = $header2Body;
}
// 读取数据
... ...
... ... @@ -194,29 +194,35 @@ class Mail {
false
);
if(!$status['EXISTS']){
return true;
}
// 最后拉取的msgno
$lastMsgno = $db->value(listsSql::lastMsgno($email_id,$folder_id));
//
$nu = 100;
$msgno = 1;
while (true){
// 是否结束了
if($status['EXISTS'] < $msgno){
break;
}
$nu = 20;
$uids = $this->client->fetch(range($msgno,($msgno-1)+$nu),'UID');
if(!$uids){
break;
}
if(!$lastMsgno){
$msgno = range(1,$nu);
}else{
$msgno = range($lastMsgno,$lastMsgno+$nu);
$uids = array_column($uids,'UID');
$existsUids = $db->value(listsSql::getUids($email_id,$folder_id,$uids));
// 获取不存在数据库的uid
$uids = array_diff($uids,$existsUids);
if($lastMsgno > $status['EXISTS']){
$msgno = range($status['EXISTS'] > $nu ? $status['EXISTS'] - $nu : 1,$status['EXISTS']);
}
// 一样就不拉新的
if($lastMsgno == $status['EXISTS']){
return true;
}
}
$msgno += $nu;
// 开始同步
if($uids){
$results = $this->client->fetchHeader($uids,true);
// 循环
$results = $this->client->fetchHeader($msgno);
if($results && is_array($results)){
// 表示已存在新邮件
if($folder == 'INBOX') redis()->incr('have_new_mail_'.$email_id,120);
... ... @@ -230,11 +236,6 @@ class Mail {
}
try {
// if(empty($header['Subject'])){
// logs($result);
// }
// 没有收件人
if(!empty($header['To'])){
$header['To'] = MailFun::toOrFrom($header['To']);
... ... @@ -288,42 +289,35 @@ class Mail {
continue;
}
$results[$key] = $data;
}
// 保存数据,这里其实不用再次写循环的。我想写一个
$uuids = $db->all(listsSql::hasUuid(array_column($results,'uuid')));
$uuids = $uuids ? array_column($uuids,null,'uuid') : [];
// $db->transaction();
foreach ($results as $insert){
if(empty($uuids[$insert['uuid']])){
// 新增
// 插入数据库
try {
$id = $db->insert(listsSql::$table,$insert);
$id = $db->insert(listsSql::$table,$data);
if($id){
// 同步body内容
redis()->rPush('sync_email_body', [
'lists_id' => $id,
'email_id' => $email_id,
'folder_id' => $folder_id,
'folder' => $folder,
'uid' => $insert['uid'],
'uid' => $data['uid'],
]);
}
}catch (\Throwable $e){
}
}
}else{
// 修改
$db->update(
listsSql::$table,
$insert,
dbWhere(['id'=>$uuids[$insert['uuid']]['id']])
);
// 结束操作了
if(redis()->get(SYNC_RUNNING_REDIS_KEY) == 'stop'){
break;
}
}
}
}
// $db->commit();
// 更新数量
$db->update(
... ... @@ -332,14 +326,6 @@ class Mail {
dbWhere(['id'=>$folder_id]),
false
);
// 结束操作了
if(redis()->get(SYNC_RUNNING_REDIS_KEY) != 'stop'){
// 再次调用
$this->syncMail($email_id,$folder_id,$folder,$db);
}
}
return true;
... ...
... ... @@ -60,6 +60,19 @@ class listsSql {
}
/**
* 获取已有的uid
* @param int $email_id
* @param int $folder_id
* @param array $uids
* @return string
* @author:dc
* @time 2023/4/23 16:54
*/
public static function getUids(int $email_id, int $folder_id, array $uids){
return "select `uid` from `".static::$table."` where ".dbWhere(['email_id'=>$email_id,'folder_id'=>$folder_id,'uid'=>$uids]);
}
/**
* 获取已存在的id
* @param $email_id
* @param $folder_id
... ...