作者 邓超

同步到es脚本优化

... ... @@ -90,7 +90,7 @@ class SyncToEsCmd {
// 为文件夹打标 方便查询
$data['folder_as_int'] = folder2int($this->folders[$data['folder_id']]);
// postid ai邮箱要用 这个是查询黑格
$data['postid'] = $this->getPostid($data['email_id']);
list($data['postid'],$data['source']) = $this->getPostid($data['email_id']);
}catch (Throwable $e){
redis()->rPush('sync_to_es'.S_V2,$id);
_echo('sync to es '.$e->getMessage());
... ... @@ -115,28 +115,23 @@ class SyncToEsCmd {
}
protected $postids = [];
/**
* 项目id
* 查询项目id 和 邮件来源
* @param int $email_id 邮箱id
* @return array
* @author:dc
* @time 2025/5/20 15:44
* @time 2025/5/29 11:47
*/
public function getPostid($email_id){
$h = date('dh');
if(!isset($this->postids[$h][$email_id])){
// 未删除状态
$id = (int) $this->fob_db->throw()->value("select `post_id` from `e_mail_binds` where `email_id` = '{$email_id}' and `deleted_at` is null order by `id` desc limit 1 ");
if(!$id){
// 已删状态
$id = (int) $this->fob_db->throw()->value("select `post_id` from `e_mail_binds` where `email_id` = '{$email_id}' order by `id` desc limit 1 ");
}
$this->postids[$h][$email_id] = $id;
}
$data = redis()->getSet('fob_bind_mail:'.$email_id,300,function ($email_id){
return $this->fob_db->throw()->first("select `post_id`,`source` from `e_mail_binds` where `email_id` = '{$email_id}' and `deleted_at` is null order by `id` desc limit 1");
},$email_id);
return $this->postids[$h][$email_id];
return 0;
return [
'postid' => $data['postid']??0,
'source' => $data['source']??0,
];
}
... ...
... ... @@ -51,6 +51,30 @@ trait RedisQuery {
}
/**
* 读取缓存 没有就设置
* @param string $key
* @param int $ttl
* @param mixed|null $default
* @param array $params
* @return mixed|string|null
* @author:dc
* @time 2025/5/29 11:40
*/
public function getSet(string $key,int $ttl, mixed $default=null,...$params){
$data = $this->getClient()->get($key);
if($data === false){
if($default instanceof \Closure){
$data = $default(...$params);
}else{
$data = $default;
}
$this->set($key,$data,$ttl);
return $data;
}
return $this->unserialize($data);
}
/**
* 获取原数据
* @param $key
* @return false|mixed|string
... ...