mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-05 07:27:15 +08:00
Merge pull request #2884 from InfinityPacer/feature/db
This commit is contained in:
+12
-10
@@ -54,16 +54,18 @@ class ConfigModel(BaseModel):
|
|||||||
DEV: bool = False
|
DEV: bool = False
|
||||||
# 是否在控制台输出 SQL 语句,默认关闭
|
# 是否在控制台输出 SQL 语句,默认关闭
|
||||||
DB_ECHO: bool = False
|
DB_ECHO: bool = False
|
||||||
# 是否在获取连接时进行预先 ping 操作,默认开启
|
# 数据库连接池类型,QueuePool, NullPool
|
||||||
DB_POOL_PRE_PING: bool = True
|
DB_POOL_TYPE: str = "QueuePool"
|
||||||
# 数据库连接池的大小,默认 100
|
# 是否在获取连接时进行预先 ping 操作,默认关闭
|
||||||
DB_POOL_SIZE: int = 100
|
DB_POOL_PRE_PING: bool = False
|
||||||
# 数据库连接的回收时间(秒),默认 3600 秒(1 小时)
|
# 数据库连接池的大小,默认 20
|
||||||
DB_POOL_RECYCLE: int = 3600
|
DB_POOL_SIZE: int = 20
|
||||||
# 数据库连接池获取连接的超时时间(秒),默认 180 秒
|
# 数据库连接的回收时间(秒),默认 1800 秒
|
||||||
DB_POOL_TIMEOUT: int = 180
|
DB_POOL_RECYCLE: int = 1800
|
||||||
# 数据库连接池最大溢出连接数,默认 5
|
# 数据库连接池获取连接的超时时间(秒),默认 30 秒
|
||||||
DB_MAX_OVERFLOW: int = 5
|
DB_POOL_TIMEOUT: int = 30
|
||||||
|
# 数据库连接池最大溢出连接数,默认 10
|
||||||
|
DB_MAX_OVERFLOW: int = 10
|
||||||
# SQLite 的 busy_timeout 参数,默认为 60 秒
|
# SQLite 的 busy_timeout 参数,默认为 60 秒
|
||||||
DB_TIMEOUT: int = 60
|
DB_TIMEOUT: int = 60
|
||||||
# 配置文件目录
|
# 配置文件目录
|
||||||
|
|||||||
+23
-15
@@ -1,24 +1,32 @@
|
|||||||
from typing import Any, Self, List, Tuple, Optional, Generator
|
from typing import Any, Generator, List, Optional, Self, Tuple
|
||||||
|
|
||||||
from sqlalchemy import create_engine, QueuePool, and_, inspect
|
from sqlalchemy import NullPool, QueuePool, and_, create_engine, inspect
|
||||||
from sqlalchemy.orm import declared_attr, sessionmaker, Session, scoped_session, as_declarative
|
from sqlalchemy.orm import Session, as_declarative, declared_attr, scoped_session, sessionmaker
|
||||||
|
|
||||||
from app.core.config import settings
|
from app.core.config import settings
|
||||||
|
|
||||||
# 数据库引擎
|
# 根据池类型设置 poolclass 和相关参数
|
||||||
Engine = create_engine(
|
pool_class = NullPool if settings.DB_POOL_TYPE == "NullPool" else QueuePool
|
||||||
url=f"sqlite:///{settings.CONFIG_PATH}/user.db",
|
kwargs = {
|
||||||
pool_pre_ping=settings.DB_POOL_PRE_PING,
|
"url": f"sqlite:///{settings.CONFIG_PATH}/user.db",
|
||||||
echo=settings.DB_ECHO,
|
"pool_pre_ping": settings.DB_POOL_PRE_PING,
|
||||||
poolclass=QueuePool,
|
"echo": settings.DB_ECHO,
|
||||||
pool_size=settings.DB_POOL_SIZE,
|
"poolclass": pool_class,
|
||||||
pool_recycle=settings.DB_POOL_RECYCLE,
|
"pool_recycle": settings.DB_POOL_RECYCLE,
|
||||||
pool_timeout=settings.DB_POOL_TIMEOUT,
|
"connect_args": {
|
||||||
max_overflow=settings.DB_MAX_OVERFLOW,
|
# "check_same_thread": False,
|
||||||
connect_args={
|
|
||||||
"timeout": settings.DB_TIMEOUT
|
"timeout": settings.DB_TIMEOUT
|
||||||
}
|
}
|
||||||
)
|
}
|
||||||
|
# 当使用 QueuePool 时,添加 QueuePool 特有的参数
|
||||||
|
if pool_class == QueuePool:
|
||||||
|
kwargs.update({
|
||||||
|
"pool_size": settings.DB_POOL_SIZE,
|
||||||
|
"pool_timeout": settings.DB_POOL_TIMEOUT,
|
||||||
|
"max_overflow": settings.DB_MAX_OVERFLOW
|
||||||
|
})
|
||||||
|
# 创建数据库引擎
|
||||||
|
Engine = create_engine(**kwargs)
|
||||||
|
|
||||||
# 会话工厂
|
# 会话工厂
|
||||||
SessionFactory = sessionmaker(bind=Engine)
|
SessionFactory = sessionmaker(bind=Engine)
|
||||||
|
|||||||
+3
-3
@@ -9,10 +9,10 @@ DEBUG=false
|
|||||||
DEV=false
|
DEV=false
|
||||||
# 日志级别(DEBUG、INFO、WARNING、ERROR等),当DEBUG=true时,此配置项将被忽略,日志级别始终为DEBUG
|
# 日志级别(DEBUG、INFO、WARNING、ERROR等),当DEBUG=true时,此配置项将被忽略,日志级别始终为DEBUG
|
||||||
LOG_LEVEL=INFO
|
LOG_LEVEL=INFO
|
||||||
# 数据库连接池的大小,可适当降低如10-50以减少I/O压力
|
# 数据库连接池的大小,可适当降低如5以减少I/O压力
|
||||||
DB_POOL_SIZE=100
|
DB_POOL_SIZE=20
|
||||||
# 数据库连接池最大溢出连接数,可适当降低如0以减少I/O压力
|
# 数据库连接池最大溢出连接数,可适当降低如0以减少I/O压力
|
||||||
DB_MAX_OVERFLOW=5
|
DB_MAX_OVERFLOW=10
|
||||||
# SQLite 的 busy_timeout 参数,可适当增加如180以减少锁定错误
|
# SQLite 的 busy_timeout 参数,可适当增加如180以减少锁定错误
|
||||||
DB_TIMEOUT=60
|
DB_TIMEOUT=60
|
||||||
# 【*】超级管理员,设置后一但重启将固化到数据库中,修改将无效(初始化超级管理员密码仅会生成一次,请在日志中查看并自行登录系统修改)
|
# 【*】超级管理员,设置后一但重启将固化到数据库中,修改将无效(初始化超级管理员密码仅会生成一次,请在日志中查看并自行登录系统修改)
|
||||||
|
|||||||
Reference in New Issue
Block a user