Revert "Fix LLM cache handling for Redis to address document deletion scenarios."
Browse files- lightrag/kg/redis_impl.py +7 -46
    	
        lightrag/kg/redis_impl.py
    CHANGED
    
    | @@ -79,52 +79,13 @@ class RedisKVStorage(BaseKVStorage): | |
| 79 | 
             
                    await self.close()
         | 
| 80 |  | 
| 81 | 
             
                async def get_by_id(self, id: str) -> dict[str, Any] | None:
         | 
| 82 | 
            -
                     | 
| 83 | 
            -
                         | 
| 84 | 
            -
             | 
| 85 | 
            -
                             | 
| 86 | 
            -
             | 
| 87 | 
            -
             | 
| 88 | 
            -
             | 
| 89 | 
            -
                                
         | 
| 90 | 
            -
                                while True:
         | 
| 91 | 
            -
                                    cursor, keys = await redis.scan(cursor, match=pattern, count=100)
         | 
| 92 | 
            -
                                    
         | 
| 93 | 
            -
                                    if keys:
         | 
| 94 | 
            -
                                        # Batch get values for these keys
         | 
| 95 | 
            -
                                        pipe = redis.pipeline()
         | 
| 96 | 
            -
                                        for key in keys:
         | 
| 97 | 
            -
                                            pipe.get(key)
         | 
| 98 | 
            -
                                        values = await pipe.execute()
         | 
| 99 | 
            -
                                        
         | 
| 100 | 
            -
                                        # Check each value for cache_type == "extract"
         | 
| 101 | 
            -
                                        for key, value in zip(keys, values):
         | 
| 102 | 
            -
                                            if value:
         | 
| 103 | 
            -
                                                try:
         | 
| 104 | 
            -
                                                    data = json.loads(value)
         | 
| 105 | 
            -
                                                    if isinstance(data, dict) and data.get("cache_type") == "extract":
         | 
| 106 | 
            -
                                                        # Extract cache key (remove namespace prefix)
         | 
| 107 | 
            -
                                                        cache_key = key.replace(f"{self.namespace}:", "")
         | 
| 108 | 
            -
                                                        result[cache_key] = data
         | 
| 109 | 
            -
                                                except json.JSONDecodeError:
         | 
| 110 | 
            -
                                                    continue
         | 
| 111 | 
            -
                                    
         | 
| 112 | 
            -
                                    if cursor == 0:
         | 
| 113 | 
            -
                                        break
         | 
| 114 | 
            -
                                
         | 
| 115 | 
            -
                                return result if result else None
         | 
| 116 | 
            -
                            except Exception as e:
         | 
| 117 | 
            -
                                logger.error(f"Error scanning Redis for extract cache entries: {e}")
         | 
| 118 | 
            -
                                return None
         | 
| 119 | 
            -
                    else:
         | 
| 120 | 
            -
                        # Original behavior for non-"default" ids
         | 
| 121 | 
            -
                        async with self._get_redis_connection() as redis:
         | 
| 122 | 
            -
                            try:
         | 
| 123 | 
            -
                                data = await redis.get(f"{self.namespace}:{id}")
         | 
| 124 | 
            -
                                return json.loads(data) if data else None
         | 
| 125 | 
            -
                            except json.JSONDecodeError as e:
         | 
| 126 | 
            -
                                logger.error(f"JSON decode error for id {id}: {e}")
         | 
| 127 | 
            -
                                return None
         | 
| 128 |  | 
| 129 | 
             
                async def get_by_ids(self, ids: list[str]) -> list[dict[str, Any]]:
         | 
| 130 | 
             
                    async with self._get_redis_connection() as redis:
         | 
|  | |
| 79 | 
             
                    await self.close()
         | 
| 80 |  | 
| 81 | 
             
                async def get_by_id(self, id: str) -> dict[str, Any] | None:
         | 
| 82 | 
            +
                    async with self._get_redis_connection() as redis:
         | 
| 83 | 
            +
                        try:
         | 
| 84 | 
            +
                            data = await redis.get(f"{self.namespace}:{id}")
         | 
| 85 | 
            +
                            return json.loads(data) if data else None
         | 
| 86 | 
            +
                        except json.JSONDecodeError as e:
         | 
| 87 | 
            +
                            logger.error(f"JSON decode error for id {id}: {e}")
         | 
| 88 | 
            +
                            return None
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 89 |  | 
| 90 | 
             
                async def get_by_ids(self, ids: list[str]) -> list[dict[str, Any]]:
         | 
| 91 | 
             
                    async with self._get_redis_connection() as redis:
         | 

