Merge pull request 'fix: 数据库连接池添加超时配置,优化用户列表查询性能' (#6) from fix/db-pool into main
Reviewed-on: http://47.253.189.47:3000/coolbot/jiachenlong/pulls/6
This commit is contained in:
commit
fcb8108734
|
|
@ -5,8 +5,8 @@ from sqlalchemy.orm import sessionmaker
|
|||
|
||||
# 支持 MySQL, PostgreSQL
|
||||
DATABASE_URL = os.getenv(
|
||||
"DATABASE_URL",
|
||||
"postgresql://postgres:postgres@localhost:5432/zodiac"
|
||||
DATABASE_URL,
|
||||
postgresql://postgres:postgres@localhost:5432/zodiac
|
||||
)
|
||||
|
||||
# 数据库引擎配置
|
||||
|
|
@ -15,6 +15,8 @@ engine = create_engine(
|
|||
pool_pre_ping=True,
|
||||
pool_size=10,
|
||||
max_overflow=20,
|
||||
pool_recycle=3600, # 1小时回收连接
|
||||
pool_timeout=30, # 获取连接超时30秒
|
||||
echo=False
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -5,34 +5,32 @@ import string
|
|||
import time
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Optional
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# 加载环境变量
|
||||
load_dotenv()
|
||||
|
||||
# 阿里云短信配置
|
||||
SMS_CONFIG = {
|
||||
"access_key_id": os.getenv("SMS_ACCESS_KEY_ID", "LTAI5t6HUnpFBLEK9194kPVG"),
|
||||
"access_key_secret": os.getenv("SMS_ACCESS_KEY_SECRET", "LEr4Q8yRxb8D5b24cKfCwlt4MMoke1"),
|
||||
"sign_name": "阿里云",
|
||||
"template_code": "100001",
|
||||
"access_key_id": os.getenv("SMS_ACCESS_KEY_ID", "LTAI5tQAx5niD7JQVqGE5acE"),
|
||||
"access_key_secret": os.getenv("SMS_ACCESS_KEY_SECRET", "QsQFAEKBkaNynIoKyvdIi3BUyWVZu1"),
|
||||
"sign_name": os.getenv("SMS_SIGN_NAME", "苏州算力"),
|
||||
"template_code": os.getenv("SMS_TEMPLATE_CODE", "SMS_501590956"),
|
||||
}
|
||||
|
||||
# 验证码缓存(生产环境建议用Redis)
|
||||
# 格式: { phone: { code: "123456", expire: 1234567890 } }
|
||||
# 验证码缓存
|
||||
VERIFICATION_CODES = {}
|
||||
|
||||
|
||||
def generate_code(length: int = 6) -> str:
|
||||
"""生成6位数字验证码"""
|
||||
return ''.join(random.choices(string.digits, k=length))
|
||||
|
||||
|
||||
def send_verification_code(phone: str) -> dict:
|
||||
"""发送短信验证码"""
|
||||
from alibabacloud_dysmsapi20170525 import models
|
||||
from alibabacloud_dysmsapi20170525.client import Client
|
||||
from alibabacloud_tea_openapi import models as open_models
|
||||
|
||||
try:
|
||||
# 生成验证码
|
||||
code = generate_code(6)
|
||||
code = generate_code()
|
||||
|
||||
# 配置客户端
|
||||
config = open_models.Config(
|
||||
|
|
@ -57,10 +55,9 @@ def send_verification_code(phone: str) -> dict:
|
|||
|
||||
# 检查结果
|
||||
if response.body.code == "OK":
|
||||
# 保存验证码
|
||||
VERIFICATION_CODES[phone] = {
|
||||
"code": code,
|
||||
"expire": int(time.time()) + 300 # 5分钟有效
|
||||
"expire": int(time.time()) + 300
|
||||
}
|
||||
return {
|
||||
"success": True,
|
||||
|
|
@ -72,35 +69,23 @@ def send_verification_code(phone: str) -> dict:
|
|||
"success": False,
|
||||
"message": f"发送失败: {response.body.message}"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
return {
|
||||
"success": False,
|
||||
"message": f"发送失败: {str(e)}"
|
||||
}
|
||||
|
||||
|
||||
def verify_code(phone: str, code: str) -> bool:
|
||||
"""验证验证码"""
|
||||
if phone not in VERIFICATION_CODES:
|
||||
return False
|
||||
|
||||
stored = VERIFICATION_CODES[phone]
|
||||
|
||||
# 检查是否过期
|
||||
if int(time.time()) > stored["expire"]:
|
||||
cached = VERIFICATION_CODES[phone]
|
||||
if int(time.time()) > cached["expire"]:
|
||||
del VERIFICATION_CODES[phone]
|
||||
return False
|
||||
|
||||
# 验证码匹配
|
||||
if stored["code"] == code:
|
||||
# 验证成功,删除验证码
|
||||
if cached["code"] == code:
|
||||
del VERIFICATION_CODES[phone]
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def check_code_exists(phone: str) -> bool:
|
||||
"""检查是否已发送过验证码"""
|
||||
return phone in VERIFICATION_CODES
|
||||
|
|
|
|||
Loading…
Reference in New Issue