Repository: LMCache/LMCache
File: lmcache/v1/lookup_client/factory.py (Python)

Code before:
            from lmcache.v1.lookup_client.lmcache_lookup_client import (
                LMCacheLookupClient,
            )

            return LMCacheLookupClient(vllm_config, config)

    @staticmethod
    def create_lookup_server(
        lmcache_engine: LMCacheEngine,
        vllm_config: "VllmConfig",
    ) -> Optional["LMCacheLookupServer"]:
        """
        Create a lookup server based on the configuration.

        Args:
            lmcache_engine: The LMCache engine instance
            vllm_config: The vLLM configuration

        Returns:
            A lookup server instance, or None if no server should be created
        """
        config = lmcache_get_config()

        # Only create the KV lookup API server on worker rank 0
        # when there are multiple workers and when not using external lookup client
        create_lookup_server_only_on_worker_0_for_mla = (
            config.extra_config.get(
                "create_lookup_server_only_on_worker_0_for_mla",
                lmcache_engine.metadata.use_mla,
            )
            if config.extra_config
            else lmcache_engine.metadata.use_mla
        )

        if config.external_lookup_client is None and (
            not create_lookup_server_only_on_worker_0_for_mla
            or lmcache_engine.metadata.worker_id == 0
        ):
            # First Party
            from lmcache.v1.lookup_client.lmcache_lookup_client import (
                LMCacheLookupServer,
            )

            return LMCacheLookupServer(lmcache_engine, vllm_config)

        return None

    @staticmethod
    def _create_external_lookup_client(
        external_lookup_uri: str,
        vllm_config: "VllmConfig",

Review comment (refactor):
Suggest to abstract a util method `get_extra_config_value(config, default_value)`, this can helps to reduce changes line a lot.

Code after:
            from lmcache.v1.lookup_client.lmcache_lookup_client import (
                LMCacheLookupClient,
            )

            return LMCacheLookupClient(vllm_config, config)

    @staticmethod
    def create_lookup_server(
        lmcache_engine: LMCacheEngine,
        vllm_config: "VllmConfig",
    ) -> Optional["LMCacheLookupServer"]:
        """
        Create a lookup server based on the configuration.

        Args:
            lmcache_engine: The LMCache engine instance
            vllm_config: The vLLM configuration

        Returns:
            A lookup server instance, or None if no server should be created
        """
        config = lmcache_get_config()

        # Only create the KV lookup API server on worker rank 0
        # when there are multiple workers and when not using external lookup client
        create_lookup_server_only_on_worker_0_for_mla = config.get_extra_config_value(
            "create_lookup_server_only_on_worker_0_for_mla",
            lmcache_engine.metadata.use_mla,
        )

        if config.external_lookup_client is None and (
            not create_lookup_server_only_on_worker_0_for_mla
            or lmcache_engine.metadata.worker_id == 0
        ):
            # First Party
            from lmcache.v1.lookup_client.lmcache_lookup_client import (
                LMCacheLookupServer,
            )

            return LMCacheLookupServer(lmcache_engine, vllm_config)

        return None

    @staticmethod
    def _create_external_lookup_client(
        external_lookup_uri: str,
        vllm_config: "VllmConfig",
    ) -> LookupClientInterface:
        """
        Create an external lookup client based on the URI format.