针对数据库操作进行优化,启用sqlite的wal优化
This commit is contained in:
@ -19,6 +19,39 @@ logging.basicConfig(
|
|||||||
)
|
)
|
||||||
logger = logging.getLogger(__name__)
|
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:
|
class ConfigManager:
|
||||||
"""配置管理类,处理应用配置"""
|
"""配置管理类,处理应用配置"""
|
||||||
def __init__(self, config_path="config"):
|
def __init__(self, config_path="config"):
|
||||||
@ -72,12 +105,12 @@ class MainDatabase:
|
|||||||
def __init__(self,db_path):
|
def __init__(self,db_path):
|
||||||
self.db_path = db_path
|
self.db_path = db_path
|
||||||
self._init_db()
|
self._init_db()
|
||||||
def _ini_db(self):
|
def _init_db(self):
|
||||||
os.makedirs(os.path.dirname(self.db_path), exist_ok=True)
|
os.makedirs(os.path.dirname(self.db_path), exist_ok=True)
|
||||||
conn = sqlite3.connect(self.db_path)
|
conn = sqlite3.connect(self.db_path)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
configure_wal(cursor)
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
|
|
||||||
""")
|
""")
|
||||||
configm = ConfigManager()
|
configm = ConfigManager()
|
||||||
|
|
||||||
@ -95,6 +128,7 @@ class ChatDatabase:
|
|||||||
os.makedirs(os.path.dirname(self.db_path), exist_ok=True)
|
os.makedirs(os.path.dirname(self.db_path), exist_ok=True)
|
||||||
conn = sqlite3.connect(self.db_path)
|
conn = sqlite3.connect(self.db_path)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
configure_wal(cursor)
|
||||||
|
|
||||||
# 创建消息表
|
# 创建消息表
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
@ -120,7 +154,7 @@ class ChatDatabase:
|
|||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
INSERT INTO messages (role, content, sender_id, timestamp)
|
INSERT INTO messages (role, content, sender_id, timestamp)
|
||||||
VALUES (?, ?, ?, ?)
|
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}")
|
logger.info(f"初始化系统消息已添加到数据库: {self.db_path}")
|
||||||
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
@ -130,9 +164,8 @@ class ChatDatabase:
|
|||||||
"""保存消息到数据库"""
|
"""保存消息到数据库"""
|
||||||
conn = sqlite3.connect(self.db_path)
|
conn = sqlite3.connect(self.db_path)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
configure_wal(cursor)
|
||||||
timestamp = datetime.now().timestamp()
|
timestamp = datetime.now().timestamp()
|
||||||
|
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
INSERT INTO messages (role, content, sender_id, timestamp)
|
INSERT INTO messages (role, content, sender_id, timestamp)
|
||||||
VALUES (?, ?, ?, ?)
|
VALUES (?, ?, ?, ?)
|
||||||
@ -145,7 +178,7 @@ class ChatDatabase:
|
|||||||
"""从数据库加载消息"""
|
"""从数据库加载消息"""
|
||||||
conn = sqlite3.connect(self.db_path)
|
conn = sqlite3.connect(self.db_path)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
configure_wal(cursor)
|
||||||
query = "SELECT role, content, sender_id, timestamp FROM messages"
|
query = "SELECT role, content, sender_id, timestamp FROM messages"
|
||||||
params = []
|
params = []
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user