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:
parent
5eaebe58f3
commit
82b6daee53
1 changed files with 7 additions and 37 deletions
|
|
@ -4,7 +4,6 @@ from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from langflow.custom.utils import abuild_custom_components, build_custom_components
|
from langflow.custom.utils import abuild_custom_components, build_custom_components
|
||||||
from langflow.services.cache.base import AsyncBaseCacheService
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from langflow.services.cache.base import CacheService
|
from langflow.services.cache.base import CacheService
|
||||||
|
|
@ -57,47 +56,18 @@ def get_all_components(components_paths, as_dict=False):
|
||||||
return components
|
return components
|
||||||
|
|
||||||
|
|
||||||
|
all_types_dict_cache = None
|
||||||
|
|
||||||
|
|
||||||
async def get_and_cache_all_types_dict(
|
async def get_and_cache_all_types_dict(
|
||||||
settings_service: "SettingsService",
|
settings_service: "SettingsService",
|
||||||
cache_service: "CacheService",
|
cache_service: "CacheService",
|
||||||
force_refresh: bool = False,
|
force_refresh: bool = False,
|
||||||
lock: asyncio.Lock | None = None,
|
lock: asyncio.Lock | None = None,
|
||||||
):
|
):
|
||||||
async def get_from_cache(key):
|
global all_types_dict_cache
|
||||||
"""
|
if all_types_dict_cache is None:
|
||||||
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:
|
|
||||||
logger.debug("Building langchain types dict")
|
logger.debug("Building langchain types dict")
|
||||||
all_types_dict = await aget_all_types_dict(settings_service.settings.components_path)
|
all_types_dict_cache = await aget_all_types_dict(settings_service.settings.components_path)
|
||||||
await set_in_cache("all_types_dict", all_types_dict)
|
|
||||||
|
|
||||||
return all_types_dict
|
return all_types_dict_cache
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue