diff --git a/lightrag/api/routers/document_routes.py b/lightrag/api/routers/document_routes.py index 6cc33fa5..84eaed4d 100644 --- a/lightrag/api/routers/document_routes.py +++ b/lightrag/api/routers/document_routes.py @@ -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()) diff --git a/lightrag/lightrag.py b/lightrag/lightrag.py index 5fae4226..467265f0 100644 --- a/lightrag/lightrag.py +++ b/lightrag/lightrag.py @@ -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 diff --git a/lightrag_webui/src/api/lightrag.ts b/lightrag_webui/src/api/lightrag.ts index b2bf1bf5..98fc59ca 100644 --- a/lightrag_webui/src/api/lightrag.ts +++ b/lightrag_webui/src/api/lightrag.ts @@ -586,11 +586,11 @@ export const clearDocuments = async (): Promise => { 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 }