This commit is contained in:
Abdulmalik Alquwayfili 2026-10-30 01:40:22 +08:00 • committed by GitHub
commit a631ec842e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 1 deletions

View file

@ -46,7 +46,7 @@ class DiskCache(BaseCache):
def get_cache(self, key, **kwargs):
original_cached_response: Final = self.disk_cache.get(key)
if original_cached_response:
if original_cached_response is not None:
try:
cached_response = _DECODED_DISK_VALUE.validate_python(json.loads(original_cached_response))
except Exception:

View file

@ -1,8 +1,10 @@
import threading
import time
from concurrent.futures import ThreadPoolExecutor
from typing import Final
import pytest
from pydantic import JsonValue
pytest.importorskip("diskcache")
@ -21,6 +23,18 @@ def cache(tmp_path):
return DiskCache(disk_cache_dir=str(tmp_path))
@pytest.mark.parametrize("value", (0, False, "", [], {}, 1, {"answer": "cached"}))
async def test_cache_preserves_falsy_values(cache: DiskCache, value: JsonValue) -> None:
await cache.async_set_cache("result", value)
result: Final = cache.get_cache("result")
assert result == value
assert type(result) is type(value)
assert await cache.async_get_cache("result") == value
assert cache.batch_get_cache(["result", "missing"]) == [value, None]
assert await cache.async_batch_get_cache(["result", "missing"]) == [value, None]
def test_increment_cache_starts_from_zero_when_key_missing(cache):
assert cache.increment_cache("counter", 3) == 3
assert cache.get_cache("counter") == 3
@ -32,6 +46,12 @@ def test_increment_cache_adds_to_existing_int(cache):
assert cache.get_cache("counter") == 12
def test_increment_cache_preserves_zero_result(cache: DiskCache) -> None:
assert cache.increment_cache("counter", 1) == 1
assert cache.increment_cache("counter", -1) == 0
assert cache.get_cache("counter") == 0
def test_increment_cache_treats_non_int_cached_value_as_zero(cache):
cache.set_cache("counter", "not-a-number")
assert cache.increment_cache("counter", 4) == 4