From a68fa32e3220606bf2fe12a4cd537e50be319050 Mon Sep 17 00:00:00 2001 From: JianFeeeee <109188060+JianFeeeee@users.noreply.github.com> Date: Sun, 17 Aug 2025 08:58:35 +0800 Subject: [PATCH] =?UTF-8?q?=E9=92=88=E5=AF=B9=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E8=BF=9B=E8=A1=8C=E4=BC=98=E5=8C=96=EF=BC=8C?= =?UTF-8?q?=E5=90=AF=E7=94=A8sqlite=E7=9A=84wal=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/file_store_api.py | 45 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/src/file_store_api.py b/src/file_store_api.py index 880cb09..b23d0e1 100644 --- a/src/file_store_api.py +++ b/src/file_store_api.py @@ -19,6 +19,39 @@ logging.basicConfig( ) logger = logging.getLogger(__name__) + +def configure_wal(cursor, cache_size=-50000, busy_timeout=5000): + """ + 配置SQLite WAL选项(线程安全) + + :param cursor: 数据库连接的cursor对象 + :param cache_size: 页面缓存大小(负数为KB表示) + :param busy_timeout: 锁等待超时时间(毫秒) + """ + logger = logging.getLogger(__name__) + + try: + # 1. 开启WAL模式 + cursor.execute("PRAGMA journal_mode=WAL;") + mode = cursor.fetchone()[0].lower() + + # 2. 核心配置项 + cursor.execute("PRAGMA synchronous=NORMAL;") + cursor.execute(f"PRAGMA busy_timeout={busy_timeout};") + cursor.execute(f"PRAGMA cache_size={cache_size};") + cursor.execute(f"PRAGMA wal_autocheckpoint=1000;") + + # 3. 其他优化配置 + cursor.execute("PRAGMA journal_size_limit=32768;") # 32MB WAL文件限制 + cursor.execute("PRAGMA mmap_size=268435456;") # 256MB内存映射 + + if mode == "wal": + logger.debug(f"WAL配置成功: cache_size={cache_size} busy_timeout={busy_timeout}ms") + else: + logger.warning(f"WAL配置部分成功,当前模式: {mode}") + except sqlite3.Error as e: + logger.error(f"WAL配置错误: {str(e)}") + class ConfigManager: """配置管理类,处理应用配置""" def __init__(self, config_path="config"): @@ -72,12 +105,12 @@ class MainDatabase: def __init__(self,db_path): self.db_path = db_path self._init_db() - def _ini_db(self): + def _init_db(self): os.makedirs(os.path.dirname(self.db_path), exist_ok=True) conn = sqlite3.connect(self.db_path) cursor = conn.cursor() + configure_wal(cursor) cursor.execute(""" - """) configm = ConfigManager() @@ -95,6 +128,7 @@ class ChatDatabase: os.makedirs(os.path.dirname(self.db_path), exist_ok=True) conn = sqlite3.connect(self.db_path) cursor = conn.cursor() + configure_wal(cursor) # 创建消息表 cursor.execute(""" @@ -120,7 +154,7 @@ class ChatDatabase: cursor.execute(""" INSERT INTO messages (role, content, sender_id, timestamp) VALUES (?, ?, ?, ?) - """, ('system', '你是一个qq助手,名叫”the real“需要根据消息回答私信或者群聊消息,注意语言要尽可能的精炼,尽量不要超过20字,同时尽量不要换行,尽量不要让其他用户认出你是ai,同时注意,当聊天中出现[CQ:at,qq=1456128421]时,证明用户在针对你发言,你的uid是1456128421', None, timestamp)) + """, ('system', basecontent, None, timestamp)) logger.info(f"初始化系统消息已添加到数据库: {self.db_path}") conn.commit() @@ -130,9 +164,8 @@ class ChatDatabase: """保存消息到数据库""" conn = sqlite3.connect(self.db_path) cursor = conn.cursor() - + configure_wal(cursor) timestamp = datetime.now().timestamp() - cursor.execute(""" INSERT INTO messages (role, content, sender_id, timestamp) VALUES (?, ?, ?, ?) @@ -145,7 +178,7 @@ class ChatDatabase: """从数据库加载消息""" conn = sqlite3.connect(self.db_path) cursor = conn.cursor() - + configure_wal(cursor) query = "SELECT role, content, sender_id, timestamp FROM messages" params = []