refactor(redis): strengthen shared cache invalidation

This commit is contained in:
3252a8
2026-05-21 06:26:48 +03:00
parent 3405d12696
commit 044cb4de7e
10 changed files with 237 additions and 61 deletions
+30 -22
View File
@@ -496,7 +496,7 @@ class PanelApiService:
"POST", "/users", json=payload, log_full_response=log_response
)
if response and not response.get("error") and "response" in response:
self._invalidate_all_users_cache()
await self._invalidate_all_users_cache()
logging.info(
f"Panel user '{username_on_panel}' created successfully (UUID: {response.get('response', {}).get('uuid')})." # noqa: E501
)
@@ -518,8 +518,8 @@ class PanelApiService:
)
if full_response and not full_response.get("error") and "response" in full_response:
logging.debug("User %s details updated on panel.", user_uuid)
self._invalidate_user_cache(user_uuid)
self._invalidate_all_users_cache()
await self._invalidate_user_cache(user_uuid)
await self._invalidate_all_users_cache()
return full_response.get("response")
logging.error(
@@ -535,8 +535,8 @@ class PanelApiService:
response_data = await self._request("POST", endpoint, log_full_response=log_response)
if response_data and not response_data.get("error") and "response" in response_data:
self._invalidate_user_cache(user_uuid)
self._invalidate_all_users_cache()
await self._invalidate_user_cache(user_uuid)
await self._invalidate_all_users_cache()
actual_status = response_data.get("response", {}).get("status")
expected_status = "ACTIVE" if enable else "DISABLED"
if actual_status == expected_status:
@@ -573,17 +573,17 @@ class PanelApiService:
logging.info(
f"Panel user {user_uuid} already absent (errorCode {error_code}). Treating as deleted." # noqa: E501
)
self._invalidate_user_cache(user_uuid)
self._invalidate_devices_cache(user_uuid)
self._invalidate_all_users_cache()
await self._invalidate_user_cache(user_uuid)
await self._invalidate_devices_cache(user_uuid)
await self._invalidate_all_users_cache()
return True
logging.error(f"Failed to delete user {user_uuid} on panel. Response: {response_data}")
return False
logging.info(f"Panel user {user_uuid} deleted successfully.")
self._invalidate_user_cache(user_uuid)
self._invalidate_devices_cache(user_uuid)
self._invalidate_all_users_cache()
await self._invalidate_user_cache(user_uuid)
await self._invalidate_devices_cache(user_uuid)
await self._invalidate_all_users_cache()
return True
async def get_subscription_link(
@@ -618,7 +618,7 @@ class PanelApiService:
payload = {"userUuid": user_uuid, "hwid": hwid}
response_data = await self._request("POST", endpoint, json=payload, log_full_response=False)
if response_data and not response_data.get("error") and "response" in response_data:
self._invalidate_devices_cache(user_uuid)
await self._invalidate_devices_cache(user_uuid)
return True
logging.error(
f"Failed to disconnect device {hwid} for user {user_uuid}. Payload: {payload}, Response: {response_data}" # noqa: E501
@@ -720,21 +720,21 @@ class PanelApiService:
)
return None
def _invalidate_squad_caches(self) -> None:
self._squads_cache.invalidate()
async def _invalidate_squad_caches(self) -> None:
await self._squads_cache.invalidate_remote()
def _invalidate_user_cache(self, user_uuid: Optional[str]) -> None:
async def _invalidate_user_cache(self, user_uuid: Optional[str]) -> None:
if not user_uuid:
return
self._users_cache.invalidate(f"uuid:{user_uuid}")
await self._users_cache.invalidate_remote(f"uuid:{user_uuid}")
def _invalidate_all_users_cache(self) -> None:
self._all_users_cache.invalidate()
async def _invalidate_all_users_cache(self) -> None:
await self._all_users_cache.invalidate_remote()
def _invalidate_devices_cache(self, user_uuid: Optional[str]) -> None:
async def _invalidate_devices_cache(self, user_uuid: Optional[str]) -> None:
if not user_uuid:
return
self._devices_cache.invalidate(f"user:{user_uuid}")
await self._devices_cache.invalidate_remote(f"user:{user_uuid}")
async def get_internal_squads(self) -> Optional[List[Dict[str, Any]]]:
return await self._squads_cache.get_or_load("list", self._get_internal_squads_uncached)
@@ -835,6 +835,8 @@ class PanelApiService:
endpoint = f"/users/{user_uuid}/actions/reset-traffic"
response_data = await self._request("POST", endpoint, log_full_response=False)
if response_data and not response_data.get("error"):
await self._invalidate_user_cache(user_uuid)
await self._invalidate_all_users_cache()
return True
logging.error("Failed to reset traffic for user %s. Response: %s", user_uuid, response_data)
return False
@@ -848,7 +850,10 @@ class PanelApiService:
log_full_response=False,
)
if response_data and not response_data.get("error"):
self._invalidate_squad_caches()
await self._invalidate_squad_caches()
for user_uuid in user_uuids:
await self._invalidate_user_cache(user_uuid)
await self._invalidate_all_users_cache()
return True
logging.error("Failed to add users to squad %s. Response: %s", squad_uuid, response_data)
return False
@@ -864,7 +869,10 @@ class PanelApiService:
log_full_response=False,
)
if response_data and not response_data.get("error"):
self._invalidate_squad_caches()
await self._invalidate_squad_caches()
for user_uuid in user_uuids:
await self._invalidate_user_cache(user_uuid)
await self._invalidate_all_users_cache()
return True
logging.error(
"Failed to remove users from squad %s. Response: %s", squad_uuid, response_data