Skip to content

Commit fc84daa

Browse files
fix: enhance file store with fallback memory cache when aiocache is n… (#4283)
* fix: enhance file store with fallback memory cache when aiocache is not installed - Added a simple in-memory cache implementation to serve as a fallback when the aiocache library is unavailable. - Improved error handling for the aiocache import, ensuring that the application can still function without it. - This change enhances the robustness of the file store utility by providing a reliable caching mechanism in various environments. * drop fallback * Potential fix for pull request finding 'Unused global variable' Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> --------- Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
1 parent 58b866a commit fc84daa

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

lib/crewai/src/crewai/utilities/file_store.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,29 @@
55
import asyncio
66
from collections.abc import Coroutine
77
import concurrent.futures
8+
import logging
89
from typing import TYPE_CHECKING, TypeVar
910
from uuid import UUID
1011

11-
from aiocache import Cache # type: ignore[import-untyped]
12-
from aiocache.serializers import PickleSerializer # type: ignore[import-untyped]
13-
1412

1513
if TYPE_CHECKING:
14+
from aiocache import Cache
1615
from crewai_files import FileInput
1716

18-
_file_store = Cache(Cache.MEMORY, serializer=PickleSerializer())
17+
logger = logging.getLogger(__name__)
18+
19+
_file_store: Cache | None = None
20+
21+
try:
22+
from aiocache import Cache
23+
from aiocache.serializers import PickleSerializer
24+
25+
_file_store = Cache(Cache.MEMORY, serializer=PickleSerializer())
26+
except ImportError:
27+
logger.debug(
28+
"aiocache is not installed. File store features will be disabled. "
29+
"Install with: uv add aiocache"
30+
)
1931

2032
T = TypeVar("T")
2133

@@ -59,6 +71,8 @@ async def astore_files(
5971
files: Dictionary mapping names to file inputs.
6072
ttl: Time-to-live in seconds.
6173
"""
74+
if _file_store is None:
75+
return
6276
await _file_store.set(f"{_CREW_PREFIX}{execution_id}", files, ttl=ttl)
6377

6478

@@ -71,6 +85,8 @@ async def aget_files(execution_id: UUID) -> dict[str, FileInput] | None:
7185
Returns:
7286
Dictionary of files or None if not found.
7387
"""
88+
if _file_store is None:
89+
return None
7490
result: dict[str, FileInput] | None = await _file_store.get(
7591
f"{_CREW_PREFIX}{execution_id}"
7692
)
@@ -83,6 +99,8 @@ async def aclear_files(execution_id: UUID) -> None:
8399
Args:
84100
execution_id: Unique identifier for the crew execution.
85101
"""
102+
if _file_store is None:
103+
return
86104
await _file_store.delete(f"{_CREW_PREFIX}{execution_id}")
87105

88106

@@ -98,6 +116,8 @@ async def astore_task_files(
98116
files: Dictionary mapping names to file inputs.
99117
ttl: Time-to-live in seconds.
100118
"""
119+
if _file_store is None:
120+
return
101121
await _file_store.set(f"{_TASK_PREFIX}{task_id}", files, ttl=ttl)
102122

103123

@@ -110,6 +130,8 @@ async def aget_task_files(task_id: UUID) -> dict[str, FileInput] | None:
110130
Returns:
111131
Dictionary of files or None if not found.
112132
"""
133+
if _file_store is None:
134+
return None
113135
result: dict[str, FileInput] | None = await _file_store.get(
114136
f"{_TASK_PREFIX}{task_id}"
115137
)
@@ -122,6 +144,8 @@ async def aclear_task_files(task_id: UUID) -> None:
122144
Args:
123145
task_id: Unique identifier for the task.
124146
"""
147+
if _file_store is None:
148+
return
125149
await _file_store.delete(f"{_TASK_PREFIX}{task_id}")
126150

127151

0 commit comments

Comments
 (0)