From 82b6daee532f627bc10620f966ee4713ed0aea37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=8Dtalo=20Johnny?= Date: Fri, 6 Sep 2024 13:36:59 -0300 Subject: [PATCH] fix: use permanent cache for all types dict and build only once (#3711) * use permanent cache for all types dict and build only once --------- Co-authored-by: ming --- src/backend/base/langflow/interface/types.py | 44 ++++---------------- 1 file changed, 7 insertions(+), 37 deletions(-) diff --git a/src/backend/base/langflow/interface/types.py b/src/backend/base/langflow/interface/types.py index efb516ee174c..5429ffa26105 100644 --- a/src/backend/base/langflow/interface/types.py +++ b/src/backend/base/langflow/interface/types.py @@ -4,7 +4,6 @@ from loguru import logger from langflow.custom.utils import abuild_custom_components, build_custom_components -from langflow.services.cache.base import AsyncBaseCacheService if TYPE_CHECKING: from langflow.services.cache.base import CacheService @@ -57,47 +56,18 @@ def get_all_components(components_paths, as_dict=False): return components +all_types_dict_cache = None + + async def get_and_cache_all_types_dict( settings_service: "SettingsService", cache_service: "CacheService", force_refresh: bool = False, lock: asyncio.Lock | None = None, ): - async def get_from_cache(key): - """ - Retrieves a value from the cache based on the given key. - - Args: - key: The key to retrieve the value from the cache. - - Returns: - The value associated with the given key in the cache. - - Raises: - None. - """ - return await cache_service.get(key=key, lock=lock) - - async def set_in_cache(key, value): - """ - Sets the given key-value pair in the cache. - - Parameters: - - key: The key to set in the cache. - - value: The value to associate with the key in the cache. - - Returns: - None - """ - if isinstance(cache_service, AsyncBaseCacheService): - await cache_service.set(key=key, value=value, lock=lock) - else: - cache_service.set(key=key, value=value, lock=lock) - - all_types_dict = await get_from_cache("all_types_dict") - if not all_types_dict or force_refresh: + global all_types_dict_cache + if all_types_dict_cache is None: logger.debug("Building langchain types dict") - all_types_dict = await aget_all_types_dict(settings_service.settings.components_path) - await set_in_cache("all_types_dict", all_types_dict) + all_types_dict_cache = await aget_all_types_dict(settings_service.settings.components_path) - return all_types_dict + return all_types_dict_cache