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):
"""Request model for clearing cache
Attributes:
modes: Optional list of cache modes to clear
This model is kept for API compatibility but no longer accepts any parameters.
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:
json_schema_extra = {"example": {"modes": ["default", "naive"]}}
json_schema_extra = {"example": {}}
class ClearCacheResponse(BaseModel):
@ -1820,47 +1813,28 @@ def create_document_routes(
)
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.
Valid modes include: "default", "naive", "local", "global", "hybrid", "mix".
- "default" represents extraction cache.
- Other modes correspond to different query modes.
This endpoint clears all cached LLM responses regardless of mode.
The request body is accepted for API compatibility but is ignored.
Args:
request (ClearCacheRequest): The request body containing optional modes to clear.
request (ClearCacheRequest): The request body (ignored for compatibility).
Returns:
ClearCacheResponse: A response object containing the status and message.
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:
# Validate modes if provided
valid_modes = ["default", "naive", "local", "global", "hybrid", "mix"]
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)
# Call the aclear_cache method (no modes parameter)
await rag.aclear_cache()
# Prepare success message
if request.modes:
message = f"Successfully cleared cache for modes: {request.modes}"
else:
message = "Successfully cleared all cache"
message = "Successfully cleared all cache"
return ClearCacheResponse(status="success", message=message)
except HTTPException:
# Re-raise HTTP exceptions
raise
except Exception as e:
logger.error(f"Error clearing cache: {str(e)}")
logger.error(traceback.format_exc())

View file

@ -1915,58 +1915,35 @@ class LightRAG:
async def _query_done(self):
await self.llm_response_cache.index_done_callback()
async def aclear_cache(self, modes: list[str] | None = None) -> None:
"""Clear cache data from the LLM response cache storage.
async def aclear_cache(self) -> None:
"""Clear all cache data from the LLM response cache storage.
Args:
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.
This method clears all cached LLM responses regardless of mode.
Example:
# Clear all 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:
logger.warning("No cache storage configured")
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:
# Reset the cache storage for specified mode
if modes:
success = await self.llm_response_cache.drop_cache_by_modes(modes)
if success:
logger.info(f"Cleared cache for modes: {modes}")
else:
logger.warning(f"Failed to clear cache for modes: {modes}")
# Clear all cache using drop method
success = await self.llm_response_cache.drop()
if success:
logger.info("Cleared all cache")
else:
# Clear all modes
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")
logger.warning("Failed to clear all cache")
await self.llm_response_cache.index_done_callback()
except Exception as 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."""
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(
self, status: DocStatus

View file

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