grass账号数据库设计:accounts_db.py实现与优化
【免费下载链接】grass grass-mining 项目地址: https://gitcode.com/GitHub_Trending/gras/grass
在grass-mining项目中,账号数据管理是核心功能之一。本文将深入解析core/utils/accounts_db.py的设计理念、实现细节及优化策略,帮助开发者理解如何高效管理用户账号与代理信息。
数据库架构设计
核心表结构
AccountsDB类通过SQLite实现了三个主要数据表,采用异步IO设计确保高并发场景下的数据安全:
CREATE TABLE IF NOT EXISTS Accounts (
id INTEGER PRIMARY KEY,
email TEXT NOT NULL,
proxies TEXT NOT NULL
)
CREATE TABLE IF NOT EXISTS ProxyList (
id INTEGER PRIMARY KEY,
proxy TEXT NOT NULL
)
CREATE TABLE IF NOT EXISTS PointStats (
id INTEGER PRIMARY KEY,
email TEXT NOT NULL,
points TEXT NOT NULL
)
- Accounts表:存储用户邮箱与关联代理列表,采用CSV格式存储多个代理
- ProxyList表:维护可用代理池,支持动态更新与提取
- PointStats表:记录用户积分数据,支持聚合统计功能
实体关系模型
核心功能实现
连接管理与线程安全
core/utils/accounts_db.py实现了异步连接管理与并发控制:
def __init__(self, db_path):
self.db_path = db_path
self.cursor = None
self.connection = None
self.db_lock = asyncio.Lock() # 确保并发安全
async def connect(self):
self.connection = await aiosqlite.connect(self.db_path)
self.cursor = await self.connection.cursor()
await self.create_tables()
通过asyncio.Lock()实现数据库操作的互斥访问,防止并发写入冲突。
账号与代理管理
add_account方法实现了账号代理的添加与更新逻辑:
async def add_account(self, email, new_proxy):
async with self.db_lock:
await self.cursor.execute("SELECT proxies FROM Accounts WHERE email=?", (email,))
existing_proxies = await self.cursor.fetchone()
if existing_proxies:
# 更新现有账号的代理列表
existing_proxies = existing_proxies[0].split(",")
if new_proxy not in existing_proxies:
updated_proxies = ",".join(existing_proxies + [new_proxy])
async with self.db_lock:
await self.cursor.execute("UPDATE Accounts SET proxies=? WHERE email=?", (updated_proxies, email))
await self.connection.commit()
else:
# 创建新账号记录
async with self.db_lock:
await self.cursor.execute("INSERT INTO Accounts(email, proxies) VALUES(?, ?)", (email, new_proxy))
await self.connection.commit()
积分统计功能
get_total_points方法实现了用户积分的聚合计算:
async def get_total_points(self):
async with self.db_lock:
await self.cursor.execute(
'SELECT SUM(CAST(points AS INTEGER)) '
'FROM (SELECT email, MAX(CAST(points AS INTEGER)) as points '
'FROM PointStats WHERE points NOT LIKE "%[^0-9]%" '
'GROUP BY email)'
)
result = await self.cursor.fetchone()
return result[0] if result else 0
该实现通过子查询先获取每个用户的最高积分,再进行求和,确保统计准确性。
性能优化策略
代理池管理优化
get_new_from_extra_proxies方法实现了高效的代理提取机制:
async def get_new_from_extra_proxies(self, table="ProxyList"):
async with self.db_lock:
await self.cursor.execute(f"SELECT proxy FROM {table} ORDER BY id DESC LIMIT 1")
proxy = await self.cursor.fetchone()
if proxy and len(proxy) == 1:
await self.cursor.execute(f"DELETE FROM {table} WHERE proxy=?", proxy)
await self.connection.commit()
return proxy[0]
return None
通过"后进先出"策略提取最新代理,并在提取后立即删除,避免重复使用。
批量操作优化
push_extra_proxies方法使用executemany实现批量插入:
async def push_extra_proxies(self, proxies):
async with self.db_lock:
await self.cursor.executemany("INSERT INTO ProxyList(proxy) VALUES(?)", [(proxy,) for proxy in proxies])
await self.connection.commit()
相比单条插入,批量操作显著提升了代理池初始化效率。
实际应用场景
账号代理绑定流程
积分统计应用
系统通过PointStats表记录用户积分变化,支持多种统计需求:
- 单个用户积分查询
- 所有用户积分总和
- 用户积分历史趋势分析
项目资源与扩展阅读
- 数据库工具类:core/utils/file_manager.py
- 异常处理:core/utils/exception.py
- 配置文件:data/config.py
- 项目图标:

通过以上设计与实现,accounts_db.py为grass-mining项目提供了高效、安全的账号数据管理解决方案。开发人员可根据实际需求,基于此模块扩展更多数据管理功能。
【免费下载链接】grass grass-mining 项目地址: https://gitcode.com/GitHub_Trending/gras/grass
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



