Skip to content

Commit 7f859e7

Browse files
committed
refactor: update Cache class initialization and open method; adjust cache path handling and improve cache management
1 parent 09051d8 commit 7f859e7

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

logseq_analyzer/app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ def setup_cache(args: Args) -> tuple[Cache, FileIndex]:
202202
index = FileIndex()
203203
cache_path = CacheFile(Constants.CACHE_FILE.value).path
204204
cache = Cache(cache_path)
205+
cache.open(protocol=5)
205206
cache.initialize(args.graph_cache, index)
206207
logger.debug("run_app: setup_cache")
207208
return cache, index

logseq_analyzer/io/cache.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ class Cache:
2727
def __init__(self, cache_path: Path = None) -> None:
2828
"""Initialize the class."""
2929
self.cache_path: Path = cache_path
30-
self.cache: shelve.Shelf[Any] = self.open(protocol=5)
30+
self.cache: shelve.Shelf[Any] = None
3131

3232
def __repr__(self) -> str:
3333
return f'{self.__class__.__qualname__}(cache_path="{self.cache_path}")'
3434

3535
def __str__(self) -> str:
3636
return f"{self.__class__.__qualname__}: {self.cache_path}"
3737

38-
def open(self, protocol: int = 5) -> shelve.Shelf[Any]:
38+
def open(self, protocol: int = 5) -> None:
3939
"""Open the cache file."""
40-
return shelve.open(self.cache_path, protocol=protocol)
40+
self.cache = shelve.open(self.cache_path, protocol=protocol)
4141

4242
def close(self) -> None:
4343
"""Close the cache file."""
@@ -63,8 +63,8 @@ def initialize(self, clear: bool, index: "FileIndex") -> None:
6363
def clear(self) -> None:
6464
"""Clear the cache."""
6565
self.close()
66-
self.cache_path.unlink()
67-
self.cache = self.open(protocol=5)
66+
self.cache_path.unlink(missing_ok=True)
67+
self.open()
6868

6969
def clear_deleted_files(self, index: "FileIndex") -> None:
7070
"""Clear the deleted files from the cache."""

0 commit comments

Comments
 (0)