Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion dspy/clients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def configure_cache(
enable_memory_cache: bool | None = True,
disk_cache_dir: str | None = DISK_CACHE_DIR,
disk_size_limit_bytes: int | None = DISK_CACHE_LIMIT,
memory_max_entries: int | None = 1000000,
memory_max_entries: int = 1000000,
):
"""Configure the cache for DSPy.

Expand Down
4 changes: 3 additions & 1 deletion dspy/clients/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(
enable_memory_cache: bool,
disk_cache_dir: str,
disk_size_limit_bytes: int | None = 1024 * 1024 * 10,
memory_max_entries: int | None = 1000000,
memory_max_entries: int = 1000000,
):
"""
Args:
Expand All @@ -43,6 +43,8 @@ def __init__(
self.enable_disk_cache = enable_disk_cache
self.enable_memory_cache = enable_memory_cache
if self.enable_memory_cache:
if memory_max_entries <= 0:
raise ValueError("memory_max_entries must be a positive integer")
self.memory_cache = LRUCache(maxsize=memory_max_entries)
else:
self.memory_cache = {}
Expand Down
10 changes: 10 additions & 0 deletions tests/clients/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ def test_initialization(tmp_path):
assert memory_cache.memory_cache.maxsize == 50
assert memory_cache.disk_cache == {}

# Test memory-only cache with invalid memory_max_entries
with pytest.raises(ValueError):
memory_cache = Cache(
enable_disk_cache=False,
enable_memory_cache=True,
disk_cache_dir="",
disk_size_limit_bytes=0,
memory_max_entries=-1,
)

# Test disk-only cache
disk_cache = Cache(
enable_disk_cache=True,
Expand Down