Skip to main content

storage: closing a lazy cache shouldn't create the underlying storage

ID
13dbfce
date
2026-11-21 14:49:22+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
b0506ed
message
storage: closing a lazy cache shouldn't create the underlying storage
changed files
5 files, 47 additions, 4 deletions

Changed files

CHANGELOG.md (1023 → 1156)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2fc4c53..fa1e69d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
 # CHANGELOG
 
+## v1.0.6 - 2026-11-21
+
+Fix a bug in v1.0.5 where closing an unused lazy SQLite cache would cause the database file to be created.
+
 ## v1.0.5 - 2026-11-21
 
 Change the behaviour of an SQLite-backed cache so the SQLite database is only created when it's actually used.
@@ -19,7 +23,7 @@ Improve the handling of cached generators:
 
 ## v1.0.2 - 2026-11-12
 
-Fix a bug in 1.0.1 where the manager could fail to find already cached values, if the function arguments contained large strings.
+Fix a bug in v1.0.1 where the manager could fail to find already cached values, if the function arguments contained large strings.
 
 ## v1.0.1 - 2026-11-11
 

src/funkycache/storage.py (14516 → 14781)

diff --git a/src/funkycache/storage.py b/src/funkycache/storage.py
index 07267fd..88a64a5 100644
--- a/src/funkycache/storage.py
+++ b/src/funkycache/storage.py
@@ -347,6 +347,16 @@ class LazyCacheStorage[V](CacheStorage[V]):
             self._underlying = self._create_underlying()
         return self._underlying.get(key)
 
+    def close(self) -> None:
+        """
+        Close the underlying storage.
+        """
+        if self._underlying is None:
+            pass
+        if not hasattr(self._underlying, "close"):
+            return
+        self._underlying.close()  # type: ignore
+
 
 def _encode_key_as_json(key: dict[str, Any]) -> str:
     """

src/funkycache/version.py (55 → 55)

diff --git a/src/funkycache/version.py b/src/funkycache/version.py
index 97635e8..d066fba 100644
--- a/src/funkycache/version.py
+++ b/src/funkycache/version.py
@@ -2,4 +2,4 @@
 The FunkyCache version.
 """
 
-__version__ = "1.0.5"
+__version__ = "1.0.6"

tests/test_manager.py (32580 → 33369)

diff --git a/tests/test_manager.py b/tests/test_manager.py
index adff752..5cbc0b4 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -709,6 +709,34 @@ class TestFunkyCache:
         assert greet("Alex") == "Bonjour Alex!"
         assert calls == {"Alex": 2}
 
+    def test_lazy_sqlite_cache(self, tmp_path: Path) -> None:
+        """
+        If you cache a function with SQLite but never call it, the underlying
+        database is never created.
+
+        This is a regression test for a bug in v1.0.5 where closing the
+        storage would create the database.
+        """
+        p = tmp_path / "example.py"
+
+        p.write_text(
+            textwrap.dedent("""
+            from funkycache import FunkyCache
+
+            GREETING = "hello world"
+
+            cache = FunkyCache(storage="sqlite", root=".cache")
+
+            @cache.register
+            def greet() -> str:
+                return GREETING
+        """)
+        )
+
+        subprocess.check_call(["python3", str(p)], cwd=tmp_path)
+
+        assert not (tmp_path / ".cache").exists()
+
 
 class TestMemoryFunkyCache(FunkyCacheTestSuite):
     """

tests/test_storage.py (13197 → 13276)

diff --git a/tests/test_storage.py b/tests/test_storage.py
index bcf9c5a..5dc64ba 100644
--- a/tests/test_storage.py
+++ b/tests/test_storage.py
@@ -289,8 +289,9 @@ class LazyCacheStorageTestSuite(CacheStorageTestSuite):
             assert isinstance(storage, LazyCacheStorage)
             assert storage._underlying is None
 
-            with pytest.raises(AttributeError):
-                getattr(storage, "does_not_exist")
+            for name in ("does_not_exist1", "does_not_exist2", "does_not_exist3"):
+                with pytest.raises(AttributeError):
+                    getattr(storage, name)
 
             assert storage._underlying is not None