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 <itestmycode@gmail.com>
This commit is contained in:
Ítalo Johnny 2024-09-06 13:36:59 -03:00 committed by GitHub
commit 82b6daee53
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4,7 +4,6 @@ from typing import TYPE_CHECKING
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