作者 邓超

同步到es脚本优化

@@ -90,7 +90,7 @@ class SyncToEsCmd { @@ -90,7 +90,7 @@ class SyncToEsCmd {
90 // 为文件夹打标 方便查询 90 // 为文件夹打标 方便查询
91 $data['folder_as_int'] = folder2int($this->folders[$data['folder_id']]); 91 $data['folder_as_int'] = folder2int($this->folders[$data['folder_id']]);
92 // postid ai邮箱要用 这个是查询黑格 92 // postid ai邮箱要用 这个是查询黑格
93 - $data['postid'] = $this->getPostid($data['email_id']); 93 + list($data['postid'],$data['source']) = $this->getPostid($data['email_id']);
94 }catch (Throwable $e){ 94 }catch (Throwable $e){
95 redis()->rPush('sync_to_es'.S_V2,$id); 95 redis()->rPush('sync_to_es'.S_V2,$id);
96 _echo('sync to es '.$e->getMessage()); 96 _echo('sync to es '.$e->getMessage());
@@ -115,28 +115,23 @@ class SyncToEsCmd { @@ -115,28 +115,23 @@ class SyncToEsCmd {
115 115
116 } 116 }
117 117
118 - protected $postids = [];  
119 118
120 /** 119 /**
121 - * 项目id 120 + * 查询项目id 和 邮件来源
  121 + * @param int $email_id 邮箱id
  122 + * @return array
122 * @author:dc 123 * @author:dc
123 - * @time 2025/5/20 15:44 124 + * @time 2025/5/29 11:47
124 */ 125 */
125 public function getPostid($email_id){ 126 public function getPostid($email_id){
126 - $h = date('dh');  
127 - if(!isset($this->postids[$h][$email_id])){  
128 - // 未删除状态  
129 - $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 ");  
130 - if(!$id){  
131 - // 已删状态  
132 - $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 ");  
133 - }  
134 - $this->postids[$h][$email_id] = $id;  
135 - } 127 + $data = redis()->getSet('fob_bind_mail:'.$email_id,300,function ($email_id){
  128 + 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");
  129 + },$email_id);
136 130
137 - return $this->postids[$h][$email_id];  
138 -  
139 - return 0; 131 + return [
  132 + 'postid' => $data['postid']??0,
  133 + 'source' => $data['source']??0,
  134 + ];
140 } 135 }
141 136
142 137
@@ -51,6 +51,30 @@ trait RedisQuery { @@ -51,6 +51,30 @@ trait RedisQuery {
51 } 51 }
52 52
53 /** 53 /**
  54 + * 读取缓存 没有就设置
  55 + * @param string $key
  56 + * @param int $ttl
  57 + * @param mixed|null $default
  58 + * @param array $params
  59 + * @return mixed|string|null
  60 + * @author:dc
  61 + * @time 2025/5/29 11:40
  62 + */
  63 + public function getSet(string $key,int $ttl, mixed $default=null,...$params){
  64 + $data = $this->getClient()->get($key);
  65 + if($data === false){
  66 + if($default instanceof \Closure){
  67 + $data = $default(...$params);
  68 + }else{
  69 + $data = $default;
  70 + }
  71 + $this->set($key,$data,$ttl);
  72 + return $data;
  73 + }
  74 + return $this->unserialize($data);
  75 + }
  76 +
  77 + /**
54 * 获取原数据 78 * 获取原数据
55 * @param $key 79 * @param $key
56 * @return false|mixed|string 80 * @return false|mixed|string