Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use permanent cache for all types dict and build only once #3711

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 7 additions & 37 deletions src/backend/base/langflow/interface/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Loading