mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
Merge bbdf3824f3 into e63fc85b7b
This commit is contained in:
commit
a631ec842e
2 changed files with 21 additions and 1 deletions
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue