Repository: LMCache/LMCache
File: lmcache/v1/storage_backend/storage_manager.py (Python)

Code before:
            self.storage_manager = storage_manager

        def on_evict(
            self, backend: StorageBackendInterface, keys: List[CacheEngineKey]
        ):
            """
            remove keys from lookup server only if they don't exist in local backends.

            :param StorageBackendInterface backend: The backend that evicted the keys.
            :param List[CacheEngineKey] keys: The keys that were evicted.
            """
            if self.storage_manager.lookup_server is None:
                return

            search_range = ["LocalCPUBackend", "LocalDiskBackend"]
            if str(backend) in search_range:
                search_range.remove(str(backend))

            keys_to_remove = []
            for key in keys:
                if not self.storage_manager.contains(key, search_range=search_range):
                    keys_to_remove.append(key)

            if keys_to_remove:
                self.storage_manager.lookup_server.batched_remove(keys_to_remove)

    def __init__(
        self,
        config: LMCacheEngineConfig,
        metadata: LMCacheEngineMetadata,
        allocator: MemoryAllocatorInterface,
        lmcache_worker: Optional["LMCacheWorker"] = None,
        lookup_server: Optional[LookupServerInterface] = None,
    ):
        self.loop = asyncio.new_event_loop()

        self.thread = threading.Thread(
            target=start_loop_in_thread_with_exceptions,
            args=(self.loop,),
            name="storage-manger-event-loop",
        )
        self.thread.start()

        dst_device = "cuda"
        self.storage_backends: OrderedDict[str, StorageBackendInterface] = (
            CreateStorageBackends(
                config,
                metadata,
                self.loop,
                allocator,
                dst_device,

Review comment (suggestion):
Rather than defining an internal class, we can have this class defined in `storage_backend_listener.py`.

Code after:
            self, backend: StorageBackendInterface, keys: List[CacheEngineKey]
        ):
            """
            remove keys from lookup server only if they don't exist in local backends.

            :param StorageBackendInterface backend: The backend that evicted the keys.
            :param List[CacheEngineKey] keys: The keys that were evicted.
            """
            if self.storage_manager.lookup_server is None:
                return

            search_range = ["LocalCPUBackend", "LocalDiskBackend"]
            if str(backend) in search_range:
                search_range.remove(str(backend))

            keys_to_remove = []
            for key in keys:
                if not self.storage_manager.contains(key, search_range=search_range):
                    keys_to_remove.append(key)

            if keys_to_remove:
                self.storage_manager.lookup_server.batched_remove(keys_to_remove)

    def __init__(
        self,
        config: LMCacheEngineConfig,
        metadata: LMCacheEngineMetadata,
        allocator: MemoryAllocatorInterface,
        lmcache_worker: Optional["LMCacheWorker"] = None,
        lookup_server: Optional[LookupServerInterface] = None,
    ):
        self.loop = asyncio.new_event_loop()

        self.thread = threading.Thread(
            target=start_loop_in_thread_with_exceptions,
            args=(self.loop,),
            name="storage-manger-event-loop",
        )
        self.thread.start()

        dst_device = "cuda"
        self.storage_backends: OrderedDict[str, StorageBackendInterface] = (
            CreateStorageBackends(
                config,
                metadata,
                self.loop,
                allocator,
                dst_device,
                lmcache_worker,
                lookup_server,
            )