mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-08 21:30:01 +08:00
✨feat(core): 优化数据库调用表现 (#2089)
This commit is contained in:
@@ -62,8 +62,9 @@ class BotConsole(Model):
|
||||
list[tuple[str, bool]] | bool: bot状态
|
||||
"""
|
||||
if not bot_id:
|
||||
return await cls.all().values_list("bot_id", "status")
|
||||
result = await cls.get_or_none(bot_id=bot_id)
|
||||
data = await BotMemoryCache.get_all()
|
||||
return [(bot_id, snapshot.status) for bot_id, snapshot in data.items()]
|
||||
result = await BotMemoryCache.get(bot_id)
|
||||
return result.status if result else False
|
||||
|
||||
@overload
|
||||
@@ -95,12 +96,13 @@ class BotConsole(Model):
|
||||
list[tuple[str, str]] | str: 被动技能
|
||||
"""
|
||||
if not bot_id:
|
||||
task_field: Literal["available_tasks", "block_tasks"] = (
|
||||
"available_tasks" if status else "block_tasks"
|
||||
)
|
||||
data_list = await cls.all().values_list("bot_id", task_field)
|
||||
return {k: cls.convert_module_format(v) for k, v in data_list}
|
||||
result = await cls.get_or_none(bot_id=bot_id)
|
||||
data = await BotMemoryCache.get_all()
|
||||
task_attr = "available_tasks" if status else "block_tasks"
|
||||
return {
|
||||
bot_id: cls.convert_module_format(getattr(snapshot, task_attr))
|
||||
for bot_id, snapshot in data.items()
|
||||
}
|
||||
result = await BotMemoryCache.get(bot_id)
|
||||
if result:
|
||||
tasks = result.available_tasks if status else result.block_tasks
|
||||
return cls.convert_module_format(tasks)
|
||||
@@ -135,11 +137,14 @@ class BotConsole(Model):
|
||||
list[tuple[str, str]] | str: 插件
|
||||
"""
|
||||
if not bot_id:
|
||||
plugin_field = "available_plugins" if status else "block_plugins"
|
||||
data_list = await cls.all().values_list("bot_id", plugin_field)
|
||||
return {k: cls.convert_module_format(v) for k, v in data_list}
|
||||
data = await BotMemoryCache.get_all()
|
||||
plugin_attr = "available_plugins" if status else "block_plugins"
|
||||
return {
|
||||
bot_id: cls.convert_module_format(getattr(snapshot, plugin_attr))
|
||||
for bot_id, snapshot in data.items()
|
||||
}
|
||||
|
||||
result = await cls.get_or_none(bot_id=bot_id)
|
||||
result = await BotMemoryCache.get(bot_id)
|
||||
if result:
|
||||
plugins = result.available_plugins if status else result.block_plugins
|
||||
return cls.convert_module_format(plugins)
|
||||
@@ -419,7 +424,9 @@ class BotConsole(Model):
|
||||
返回:
|
||||
bool: 是否被禁用
|
||||
"""
|
||||
bot_data, _ = await cls.get_or_create(bot_id=bot_id)
|
||||
bot_data = await BotMemoryCache.get(bot_id)
|
||||
if not bot_data:
|
||||
return False
|
||||
return cls.format(plugin_name) in bot_data.block_plugins
|
||||
|
||||
@classmethod
|
||||
@@ -434,7 +441,9 @@ class BotConsole(Model):
|
||||
返回:
|
||||
bool: 是否被禁用
|
||||
"""
|
||||
bot_data, _ = await cls.get_or_create(bot_id=bot_id)
|
||||
bot_data = await BotMemoryCache.get(bot_id)
|
||||
if not bot_data:
|
||||
return False
|
||||
return cls.format(task_name) in bot_data.block_tasks
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -40,7 +40,7 @@ class LevelUser(Model):
|
||||
"""
|
||||
if not group_id:
|
||||
return 0
|
||||
if user := await cls.get_or_none(user_id=user_id, group_id=group_id):
|
||||
if user := await LevelUserMemoryCache.get(user_id, group_id):
|
||||
return user.user_level
|
||||
return 0
|
||||
|
||||
@@ -103,11 +103,11 @@ class LevelUser(Model):
|
||||
if level == 0:
|
||||
return True
|
||||
if group_id:
|
||||
if user := await cls.get_or_none(user_id=user_id, group_id=group_id):
|
||||
if user := await LevelUserMemoryCache.get(user_id, group_id):
|
||||
return user.user_level >= level
|
||||
elif user_list := await cls.filter(user_id=user_id).all():
|
||||
user = max(user_list, key=lambda x: x.user_level)
|
||||
return user.user_level >= level
|
||||
return False
|
||||
max_level = await LevelUserMemoryCache.get_max_level(user_id)
|
||||
return max_level >= level
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
@@ -121,7 +121,7 @@ class LevelUser(Model):
|
||||
返回:
|
||||
bool: 是否会被自动更新权限刷新
|
||||
"""
|
||||
if user := await cls.get_or_none(user_id=user_id, group_id=group_id):
|
||||
if user := await LevelUserMemoryCache.get(user_id, group_id):
|
||||
return user.group_flag == 1
|
||||
return False
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from tortoise import fields
|
||||
|
||||
from zhenxun.services.cache.runtime_cache import TaskInfoMemoryCache
|
||||
from zhenxun.services.db_context import Model
|
||||
|
||||
|
||||
@@ -25,6 +26,27 @@ class TaskInfo(Model):
|
||||
table = "task_info"
|
||||
table_description = "被动技能基本信息"
|
||||
|
||||
@classmethod
|
||||
async def create(cls, *args, **kwargs):
|
||||
result = await super().create(*args, **kwargs)
|
||||
await TaskInfoMemoryCache.upsert_from_model(result)
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
async def update_or_create(cls, *args, **kwargs):
|
||||
result = await super().update_or_create(*args, **kwargs)
|
||||
await TaskInfoMemoryCache.upsert_from_model(result[0])
|
||||
return result
|
||||
|
||||
async def save(self, *args, **kwargs):
|
||||
await super().save(*args, **kwargs)
|
||||
await TaskInfoMemoryCache.upsert_from_model(self)
|
||||
|
||||
async def delete(self, *args, **kwargs):
|
||||
module = self.module
|
||||
await super().delete(*args, **kwargs)
|
||||
await TaskInfoMemoryCache.remove(module)
|
||||
|
||||
@classmethod
|
||||
async def _run_script(cls):
|
||||
return [
|
||||
|
||||
Reference in New Issue
Block a user