From 125eb12a8fb174ff01b4423752af30305a64034e Mon Sep 17 00:00:00 2001 From: Franklin-F Date: Tue, 1 Jul 2025 19:01:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96DB.py=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E6=93=8D=E4=BD=9C=E8=AE=B0=E5=BD=95=E5=92=8C=E8=A7=86?= =?UTF-8?q?=E9=A2=91=E8=A1=A8=E7=9A=84UPSERT=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86=E5=92=8C?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DB.py | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/DB.py b/DB.py index 8f8976d..6e3eaa3 100644 --- a/DB.py +++ b/DB.py @@ -771,6 +771,7 @@ class DBSA: logger.debug(f"flush 操作记录数: {len(op_rows)}") logger.debug(f"flush 视频记录数: {len(vid_rows)}") + # 作者表处理 now_ts = int(time.time()) authors_map = {} for data in payloads: @@ -809,19 +810,34 @@ class DBSA: cls._execute_with_deadlock_retry(ondup) except Exception as e: logger.error(f"写作者表失败: {e}") - cls.push_record_many(payloads) + try: + cls.push_record_many(payloads) + except Exception as re: + logger.error("[Redis 回退失败]", re) return + # 操作记录 UPSERT(解决唯一索引冲突) try: - stmt = video_op.insert().values(op_rows) + stmt = mysql_insert(video_op).values(op_rows) + ondup = stmt.on_duplicate_key_update( + updatetime=stmt.inserted.updatetime, + operatetime=stmt.inserted.operatetime, + ts_status=stmt.inserted.ts_status, + is_repeat=stmt.inserted.is_repeat, + history_status=stmt.inserted.history_status, + ) with _engine.begin() as conn: - conn.execute(stmt) - logger.info(f"插入操作记录: {len(op_rows)} 条") + conn.execute(ondup) + logger.info(f"插入/更新操作记录: {len(op_rows)} 条") except Exception as e: - logger.error(f"插入操作记录失败: {e}") - cls.push_record_many(payloads) + logger.error(f"操作记录 UPSERT 失败: {e}") + try: + cls.push_record_many(payloads) + except Exception as re: + logger.error("[Redis 回退失败]", re) return + # 视频表 UPSERT try: stmt = mysql_insert(video).values(vid_rows) upd = { @@ -833,14 +849,20 @@ class DBSA: "watch_number": stmt.inserted.watch_number, "follow_number": stmt.inserted.follow_number, "video_number": stmt.inserted.video_number, + "is_repeat": stmt.inserted.is_repeat, + "ts_status": stmt.inserted.ts_status, } ondup = stmt.on_duplicate_key_update(**upd) with _engine.begin() as conn: conn.execute(ondup) - logger.info(f"[DBSA] flush 完成:authors={len(author_rows)} 条, op={len(op_rows)} 条, video={len(vid_rows)} 条") + logger.info( + f"[DBSA] flush 完成:authors={len(author_rows)} 条, op={len(op_rows)} 条, video={len(vid_rows)} 条") except Exception as e: logger.error(f"写视频表失败: {e}") - cls.push_record_many(payloads) + try: + cls.push_record_many(payloads) + except Exception as re: + logger.error("[Redis 回退失败]", re) @classmethod def _execute_with_deadlock_retry(cls, statement):