Skip to content

Commit

Permalink
fix: Add diskcache autoreload
Browse files Browse the repository at this point in the history
  • Loading branch information
thomass-dev committed Dec 9, 2024
1 parent 9e29541 commit 939bfc7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
27 changes: 17 additions & 10 deletions skore/src/skore/persistence/disk_cache_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def __init__(self, directory: Path):
"""
if not directory.exists():
raise DirectoryDoesNotExist(f"Directory {directory} does not exist.")
self.storage = Cache(directory)

self.directory = directory

def __getitem__(self, key: str) -> Any:
"""
Expand All @@ -63,7 +64,8 @@ def __getitem__(self, key: str) -> Any:
KeyError
If the key is not found in the storage.
"""
return self.storage[key]
with Cache(self.directory) as storage:
return storage[key]

def __setitem__(self, key: str, value: Any):
"""
Expand All @@ -76,7 +78,8 @@ def __setitem__(self, key: str, value: Any):
value : Any
The value to store.
"""
self.storage[key] = value
with Cache(self.directory) as storage:
storage[key] = value

def __delitem__(self, key: str):
"""
Expand All @@ -92,7 +95,8 @@ def __delitem__(self, key: str):
KeyError
If the key is not found in the storage.
"""
del self.storage[key]
with Cache(self.directory) as storage:
del storage[key]

def keys(self) -> Iterator[str]:
"""
Expand All @@ -103,7 +107,8 @@ def keys(self) -> Iterator[str]:
Iterator[str]
An iterator yielding all keys in the storage.
"""
return self.storage.iterkeys()
with Cache(self.directory) as storage:
return storage.iterkeys()

def values(self) -> Iterator[Any]:
"""
Expand All @@ -114,8 +119,9 @@ def values(self) -> Iterator[Any]:
Iterator[Any]
An iterator yielding all values in the storage.
"""
for key in self.storage.iterkeys():
yield self.storage[key]
with Cache(self.directory) as storage:
for key in storage.iterkeys():
yield storage[key]

def items(self) -> Iterator[tuple[str, Any]]:
"""
Expand All @@ -126,8 +132,9 @@ def items(self) -> Iterator[tuple[str, Any]]:
Iterator[tuple[str, Any]]
An iterator yielding all (key, value) pairs in the storage.
"""
for key in self.storage.iterkeys():
yield (key, self.storage[key])
with Cache(self.directory) as storage:
for key in storage.iterkeys():
yield (key, storage[key])

def __repr__(self) -> str:
"""
Expand All @@ -138,4 +145,4 @@ def __repr__(self) -> str:
str
A string representation of the storage.
"""
return f"DiskCacheStorage(directory='{self.storage.directory}')"
return f"DiskCacheStorage(directory='{self.directory}')"
22 changes: 22 additions & 0 deletions skore/tests/unit/persistence/test_disk.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os
import shutil
from pathlib import Path

from skore.persistence.disk_cache_storage import DiskCacheStorage
Expand All @@ -22,3 +24,23 @@ def test_disk_storage(tmp_path: Path):
assert list(storage.items()) == []

assert repr(storage) == f"DiskCacheStorage(directory='{tmp_path}')"


def test_autoreload(tmp_path: Path):
os.mkdir(tmp_path / "test1/")
storage1 = DiskCacheStorage(tmp_path / "test1/")
storage1["key"] = "test1"

assert storage1["key"] == "test1"

os.mkdir(tmp_path / "test2/")
storage2 = DiskCacheStorage(tmp_path / "test2/")
storage2["key"] = "test2"

assert storage2["key"] == "test2"

shutil.rmtree(tmp_path / "test1/")
os.symlink(tmp_path / "test2/", tmp_path / "test1/")

assert storage1["key"] == "test2"
assert storage2["key"] == "test2"

0 comments on commit 939bfc7

Please sign in to comment.