feat: 更新SQLite连接配置,启用共享缓存和WAL模式,设置忙等待和内存映射参数

This commit is contained in:
TsMask
2025-10-20 16:07:05 +08:00
parent c15e773e2b
commit 9603dd9188
2 changed files with 10 additions and 19 deletions

View File

@@ -1,9 +0,0 @@
-- ----------------------------
-- Configuration
-- ----------------------------
-- 启用完整的自动空间回收
PRAGMA auto_vacuum = FULL;
-- 日志模式设置为 WAL 并发写入
PRAGMA journal_mode = WAL;
PRAGMA synchronous = NORMAL;

View File

@@ -5,6 +5,7 @@ import (
"log" "log"
"os" "os"
"regexp" "regexp"
"strings"
"time" "time"
"github.com/glebarez/sqlite" "github.com/glebarez/sqlite"
@@ -36,7 +37,15 @@ func loadDialect() map[string]dialectInfo {
// 数据库类型对应的数据库连接 // 数据库类型对应的数据库连接
switch item["type"] { switch item["type"] {
case "sqlite": case "sqlite":
dsn := fmt.Sprintf("%s", item["database"]) pragmas := []string{
"cache=shared", // Enable shared cache
"_pragma=journal_mode(WAL)", // Enable WAL mode
"_pragma=busy_timeout(5000)", // Set busy timeout 抛出 SQLITE_BUSY 错误 默认0立即设置等待 5 秒5000 毫秒)
"_pragma=synchronous(NORMAL)", // Set synchronous mode
"_pragma=wal_autocheckpoint=5000", // Set WAL auto-checkpoint threshold 设置较大的自动检查点阈值
"_pragma=mmap_size(67108864)", // Set mmap size 内存映射 I/O 最大字节数为 64MB
}
dsn := fmt.Sprintf("%s?%s", item["database"], strings.Join(pragmas, "&"))
dialects[key] = dialectInfo{ dialects[key] = dialectInfo{
dialectic: sqlite.Open(dsn), dialectic: sqlite.Open(dsn),
logging: parse.Boolean(item["logging"]), logging: parse.Boolean(item["logging"]),
@@ -108,15 +117,6 @@ func Connect() {
sqlDB.SetMaxOpenConns(100) sqlDB.SetMaxOpenConns(100)
// SetConnMaxLifetime 设置了连接可复用的最大时间。 // SetConnMaxLifetime 设置了连接可复用的最大时间。
sqlDB.SetConnMaxLifetime(time.Hour) sqlDB.SetConnMaxLifetime(time.Hour)
// 执行初始连接配置
if db.Name() == "sqlite" {
// -- 设置较大的自动检查点阈值
sqlDB.Exec("PRAGMA wal_autocheckpoint = 5000;")
// -- 内存映射 I/O 最大字节数为 64MB
sqlDB.Exec("PRAGMA mmap_size = 67108864;")
// 抛出 SQLITE_BUSY 错误 默认0立即设置等待 5 秒5000 毫秒)
sqlDB.Exec("PRAGMA busy_timeout = 5000;")
}
logger.Infof("database %s connection is successful.", key) logger.Infof("database %s connection is successful.", key)
dbMap[key] = db dbMap[key] = db
} }