refactor: remove selective LLM cache clearing functionality

- Remove optional 'modes' parameter from aclear_cache() and clear_cache() methods
- Replace deprecated drop_cache_by_modes() with drop() method for complete cache clearing
- Update API endpoint to ignore mode-specific parameters and clear all cache
- Simplify frontend clearCache() function to send empty request body

This change ensures all LLM cache is cleared together.
This commit is contained in:
yangdx 2025-08-05 23:51:51 +08:00
parent cc1f7118e7
commit c22315ea6d
3 changed files with 23 additions and 72 deletions

View file

@ -258,19 +258,12 @@ class ClearDocumentsResponse(BaseModel):
class ClearCacheRequest(BaseModel): class ClearCacheRequest(BaseModel):
"""Request model for clearing cache """Request model for clearing cache
Attributes: This model is kept for API compatibility but no longer accepts any parameters.
modes: Optional list of cache modes to clear All cache will be cleared regardless of the request content.
""" """
modes: Optional[
List[Literal["default", "naive", "local", "global", "hybrid", "mix"]]
] = Field(
default=None,
description="Modes of cache to clear. If None, clears all cache.",
)
class Config: class Config:
json_schema_extra = {"example": {"modes": ["default", "naive"]}} json_schema_extra = {"example": {}}
class ClearCacheResponse(BaseModel): class ClearCacheResponse(BaseModel):
@ -1820,47 +1813,28 @@ def create_document_routes(
) )
async def clear_cache(request: ClearCacheRequest): async def clear_cache(request: ClearCacheRequest):
""" """
Clear cache data from the LLM response cache storage. Clear all cache data from the LLM response cache storage.
This endpoint allows clearing specific modes of cache or all cache if no modes are specified. This endpoint clears all cached LLM responses regardless of mode.
Valid modes include: "default", "naive", "local", "global", "hybrid", "mix". The request body is accepted for API compatibility but is ignored.
- "default" represents extraction cache.
- Other modes correspond to different query modes.
Args: Args:
request (ClearCacheRequest): The request body containing optional modes to clear. request (ClearCacheRequest): The request body (ignored for compatibility).
Returns: Returns:
ClearCacheResponse: A response object containing the status and message. ClearCacheResponse: A response object containing the status and message.
Raises: Raises:
HTTPException: If an error occurs during cache clearing (400 for invalid modes, 500 for other errors). HTTPException: If an error occurs during cache clearing (500).
""" """
try: try:
# Validate modes if provided # Call the aclear_cache method (no modes parameter)
valid_modes = ["default", "naive", "local", "global", "hybrid", "mix"] await rag.aclear_cache()
if request.modes and not all(mode in valid_modes for mode in request.modes):
invalid_modes = [
mode for mode in request.modes if mode not in valid_modes
]
raise HTTPException(
status_code=400,
detail=f"Invalid mode(s): {invalid_modes}. Valid modes are: {valid_modes}",
)
# Call the aclear_cache method
await rag.aclear_cache(request.modes)
# Prepare success message # Prepare success message
if request.modes: message = "Successfully cleared all cache"
message = f"Successfully cleared cache for modes: {request.modes}"
else:
message = "Successfully cleared all cache"
return ClearCacheResponse(status="success", message=message) return ClearCacheResponse(status="success", message=message)
except HTTPException:
# Re-raise HTTP exceptions
raise
except Exception as e: except Exception as e:
logger.error(f"Error clearing cache: {str(e)}") logger.error(f"Error clearing cache: {str(e)}")
logger.error(traceback.format_exc()) logger.error(traceback.format_exc())

View file

@ -1915,58 +1915,35 @@ class LightRAG:
async def _query_done(self): async def _query_done(self):
await self.llm_response_cache.index_done_callback() await self.llm_response_cache.index_done_callback()
async def aclear_cache(self, modes: list[str] | None = None) -> None: async def aclear_cache(self) -> None:
"""Clear cache data from the LLM response cache storage. """Clear all cache data from the LLM response cache storage.
Args: This method clears all cached LLM responses regardless of mode.
modes (list[str] | None): Modes of cache to clear. Options: ["default", "naive", "local", "global", "hybrid", "mix"].
"default" represents extraction cache.
If None, clears all cache.
Example: Example:
# Clear all cache # Clear all cache
await rag.aclear_cache() await rag.aclear_cache()
# Clear local mode cache
await rag.aclear_cache(modes=["local"])
# Clear extraction cache
await rag.aclear_cache(modes=["default"])
""" """
if not self.llm_response_cache: if not self.llm_response_cache:
logger.warning("No cache storage configured") logger.warning("No cache storage configured")
return return
valid_modes = ["default", "naive", "local", "global", "hybrid", "mix"]
# Validate input
if modes and not all(mode in valid_modes for mode in modes):
raise ValueError(f"Invalid mode. Valid modes are: {valid_modes}")
try: try:
# Reset the cache storage for specified mode # Clear all cache using drop method
if modes: success = await self.llm_response_cache.drop()
success = await self.llm_response_cache.drop_cache_by_modes(modes) if success:
if success: logger.info("Cleared all cache")
logger.info(f"Cleared cache for modes: {modes}")
else:
logger.warning(f"Failed to clear cache for modes: {modes}")
else: else:
# Clear all modes logger.warning("Failed to clear all cache")
success = await self.llm_response_cache.drop_cache_by_modes(valid_modes)
if success:
logger.info("Cleared all cache")
else:
logger.warning("Failed to clear all cache")
await self.llm_response_cache.index_done_callback() await self.llm_response_cache.index_done_callback()
except Exception as e: except Exception as e:
logger.error(f"Error while clearing cache: {e}") logger.error(f"Error while clearing cache: {e}")
def clear_cache(self, modes: list[str] | None = None) -> None: def clear_cache(self) -> None:
"""Synchronous version of aclear_cache.""" """Synchronous version of aclear_cache."""
return always_get_an_event_loop().run_until_complete(self.aclear_cache(modes)) return always_get_an_event_loop().run_until_complete(self.aclear_cache())
async def get_docs_by_status( async def get_docs_by_status(
self, status: DocStatus self, status: DocStatus

View file

@ -586,11 +586,11 @@ export const clearDocuments = async (): Promise<DocActionResponse> => {
return response.data return response.data
} }
export const clearCache = async (modes?: string[]): Promise<{ export const clearCache = async (): Promise<{
status: 'success' | 'fail' status: 'success' | 'fail'
message: string message: string
}> => { }> => {
const response = await axiosInstance.post('/documents/clear_cache', { modes }) const response = await axiosInstance.post('/documents/clear_cache', {})
return response.data return response.data
} }