@@ -130,10 +130,10 @@ def parse_plugin_config() -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
130130 current_redis_client = RedisCli ()
131131 run_await (current_redis_client .open )()
132132
133- run_await ( current_redis_client . delete_prefix )( f' { settings . PLUGIN_REDIS_PREFIX } :info' , exclude = plugins )
134- plugin_status = run_await (current_redis_client .hgetall )( f' { settings . PLUGIN_REDIS_PREFIX } :status' )
135- if not plugin_status :
136- plugin_status = {}
133+ # 清理未知插件信息
134+ run_await (current_redis_client .delete_prefix )(
135+ settings . PLUGIN_REDIS_PREFIX , exclude = [ f' { settings . PLUGIN_REDIS_PREFIX } : { key } ' for key in plugins ]
136+ )
137137
138138 for plugin in plugins :
139139 data = load_plugin_config (plugin )
@@ -164,16 +164,19 @@ def parse_plugin_config() -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
164164 app_plugins .append (data )
165165
166166 # 补充插件信息
167- data ['plugin' ]['enable' ] = plugin_status .setdefault (plugin , str (StatusType .enable .value ))
167+ plugin_cache_info = run_await (current_redis_client .get )(f'{ settings .PLUGIN_REDIS_PREFIX } :{ plugin } ' )
168+ if plugin_cache_info :
169+ data ['plugin' ]['enable' ] = json .loads (plugin_cache_info )['plugin' ]['enable' ]
170+ else :
171+ data ['plugin' ]['enable' ] = str (StatusType .enable .value )
168172 data ['plugin' ]['name' ] = plugin
169173
170- # 缓存插件信息
174+ # 缓存最新插件信息
171175 run_await (current_redis_client .set )(
172- f'{ settings .PLUGIN_REDIS_PREFIX } :info: { plugin } ' , json .dumps (data , ensure_ascii = False )
176+ f'{ settings .PLUGIN_REDIS_PREFIX } :{ plugin } ' , json .dumps (data , ensure_ascii = False )
173177 )
174178
175- # 缓存插件状态
176- run_await (current_redis_client .hset )(f'{ settings .PLUGIN_REDIS_PREFIX } :status' , mapping = plugin_status )
179+ # 重置插件变更状态
177180 run_await (current_redis_client .delete )(f'{ settings .PLUGIN_REDIS_PREFIX } :changed' )
178181
179182 return extend_plugins , app_plugins
@@ -380,13 +383,10 @@ async def __call__(self, request: Request) -> None:
380383 :param request: FastAPI 请求对象
381384 :return:
382385 """
383- plugin_status = await redis_client .hgetall (f'{ settings .PLUGIN_REDIS_PREFIX } :status ' )
384- if not plugin_status :
386+ plugin_info = await redis_client .get (f'{ settings .PLUGIN_REDIS_PREFIX } :{ self . plugin } ' )
387+ if not plugin_info :
385388 log .error ('插件状态未初始化或丢失,需重启服务自动修复' )
386389 raise PluginInjectError ('插件状态未初始化或丢失,请联系系统管理员' )
387390
388- if self .plugin not in plugin_status :
389- log .error (f'插件 { self .plugin } 状态未初始化或丢失,需重启服务自动修复' )
390- raise PluginInjectError (f'插件 { self .plugin } 状态未初始化或丢失,请联系系统管理员' )
391- if not int (plugin_status .get (self .plugin )):
391+ if not int (json .loads (plugin_info )['plugin' ]['enable' ]):
392392 raise errors .ServerError (msg = f'插件 { self .plugin } 未启用,请联系系统管理员' )
0 commit comments