funkycache 1.0.1__tar.gz → 1.0.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {funkycache-1.0.1/src/funkycache.egg-info → funkycache-1.0.2}/PKG-INFO +1 -1
- {funkycache-1.0.1 → funkycache-1.0.2}/src/funkycache/storage.py +26 -8
- {funkycache-1.0.1 → funkycache-1.0.2}/src/funkycache/version.py +1 -1
- {funkycache-1.0.1 → funkycache-1.0.2/src/funkycache.egg-info}/PKG-INFO +1 -1
- {funkycache-1.0.1 → funkycache-1.0.2}/src/funkycache.egg-info/SOURCES.txt +1 -0
- funkycache-1.0.2/tests/test_integration.py +36 -0
- {funkycache-1.0.1 → funkycache-1.0.2}/tests/test_storage.py +16 -0
- {funkycache-1.0.1 → funkycache-1.0.2}/LICENSE +0 -0
- {funkycache-1.0.1 → funkycache-1.0.2}/README.md +0 -0
- {funkycache-1.0.1 → funkycache-1.0.2}/pyproject.toml +0 -0
- {funkycache-1.0.1 → funkycache-1.0.2}/setup.cfg +0 -0
- {funkycache-1.0.1 → funkycache-1.0.2}/src/funkycache/__init__.py +0 -0
- {funkycache-1.0.1 → funkycache-1.0.2}/src/funkycache/manager.py +0 -0
- {funkycache-1.0.1 → funkycache-1.0.2}/src/funkycache/py.typed +0 -0
- {funkycache-1.0.1 → funkycache-1.0.2}/src/funkycache.egg-info/dependency_links.txt +0 -0
- {funkycache-1.0.1 → funkycache-1.0.2}/src/funkycache.egg-info/requires.txt +0 -0
- {funkycache-1.0.1 → funkycache-1.0.2}/src/funkycache.egg-info/top_level.txt +0 -0
- {funkycache-1.0.1 → funkycache-1.0.2}/tests/test_manager.py +0 -0
|
@@ -322,6 +322,14 @@ def _get_type_adapter(target_type: Any) -> TypeAdapter:
|
|
|
322
322
|
return TypeAdapter(target_type)
|
|
323
323
|
|
|
324
324
|
|
|
325
|
+
def _should_hash(v: Any) -> bool:
|
|
326
|
+
"""
|
|
327
|
+
Return whether the value is a long string that should be hashed
|
|
328
|
+
before JSON-encoding the key.
|
|
329
|
+
"""
|
|
330
|
+
return isinstance(v, str) and len(v) > 512
|
|
331
|
+
|
|
332
|
+
|
|
325
333
|
def _encode_key_as_json(key: dict[str, Any]) -> str:
|
|
326
334
|
"""
|
|
327
335
|
Encode a set of arguments to a Python function as a JSON string.
|
|
@@ -329,24 +337,34 @@ def _encode_key_as_json(key: dict[str, Any]) -> str:
|
|
|
329
337
|
This is a lossy transformation that is designed to get a reasonable
|
|
330
338
|
serialisation as JSON. It is possible for values of different types
|
|
331
339
|
to collide.
|
|
340
|
+
|
|
341
|
+
This function must not mutate its input.
|
|
332
342
|
"""
|
|
343
|
+
overrides: dict[str, Any] = {}
|
|
344
|
+
|
|
333
345
|
for name, val in key.items():
|
|
334
|
-
if
|
|
335
|
-
|
|
346
|
+
if _should_hash(val):
|
|
347
|
+
overrides[name] = _hash(val)
|
|
336
348
|
|
|
337
349
|
elif isinstance(val, dict):
|
|
338
|
-
for
|
|
339
|
-
|
|
340
|
-
|
|
350
|
+
if any(_should_hash(v) for v in val.values()):
|
|
351
|
+
overrides[name] = {
|
|
352
|
+
k: _hash(v) if _should_hash(v) else v for k, v in val.items()
|
|
353
|
+
}
|
|
341
354
|
|
|
342
355
|
elif isinstance(val, BaseModel) or dataclasses.is_dataclass(val):
|
|
343
356
|
adapter = _get_type_adapter(type(val))
|
|
344
|
-
|
|
357
|
+
overrides[name] = {
|
|
358
|
+
k: _hash(v) if _should_hash(v) else v
|
|
359
|
+
for k, v in adapter.dump_python(val, mode="json").items()
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
reduced_key = {**key, **overrides}
|
|
345
363
|
|
|
346
364
|
try:
|
|
347
|
-
return json.dumps(
|
|
365
|
+
return json.dumps(reduced_key, sort_keys=True, default=str)
|
|
348
366
|
except TypeError:
|
|
349
|
-
clean_key = _make_json_compatible(
|
|
367
|
+
clean_key = _make_json_compatible(reduced_key)
|
|
350
368
|
return json.dumps(clean_key, sort_keys=True, default=str)
|
|
351
369
|
|
|
352
370
|
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Integration tests for the library.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from datetime import datetime
|
|
6
|
+
import time
|
|
7
|
+
|
|
8
|
+
from pydantic import BaseModel
|
|
9
|
+
|
|
10
|
+
from funkycache import FunkyCache
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def test_long_mutable_key() -> None:
|
|
14
|
+
"""
|
|
15
|
+
We can cache a function argument which includes a long key that
|
|
16
|
+
gets reduced during key encoding.
|
|
17
|
+
|
|
18
|
+
This is a regression test for a bug in 1.0.1, where the key would be
|
|
19
|
+
mutated when we checked if a value was already cached, which led to
|
|
20
|
+
cache misses and errors about overwriting values.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
class MyType(BaseModel):
|
|
24
|
+
content: str = ""
|
|
25
|
+
|
|
26
|
+
cache = FunkyCache(storage="memory")
|
|
27
|
+
|
|
28
|
+
@cache.register
|
|
29
|
+
def upcase(tp: MyType) -> str:
|
|
30
|
+
return str(datetime.now())
|
|
31
|
+
|
|
32
|
+
t = MyType(content="a" * 2500)
|
|
33
|
+
|
|
34
|
+
upcase(t)
|
|
35
|
+
time.sleep(0.1)
|
|
36
|
+
upcase(t)
|
|
@@ -5,6 +5,7 @@ Tests for backend storage implementations.
|
|
|
5
5
|
from abc import ABC, abstractmethod
|
|
6
6
|
from collections.abc import Generator
|
|
7
7
|
from contextlib import closing, contextmanager
|
|
8
|
+
import copy
|
|
8
9
|
from dataclasses import dataclass
|
|
9
10
|
from pathlib import Path
|
|
10
11
|
import sqlite3
|
|
@@ -58,12 +59,19 @@ class CacheStorageTestSuite(ABC):
|
|
|
58
59
|
{"args": [{Path("n"): "numbers"}]}, id="path_key_nested_in_list"
|
|
59
60
|
),
|
|
60
61
|
pytest.param({"1": {Path("n"): "numbers"}}, id="path_key_nested_in_obj"),
|
|
62
|
+
#
|
|
63
|
+
# A long string which gets truncated during hashing
|
|
64
|
+
pytest.param({"a": {"n": "n" * 5000}}, id="long_key"),
|
|
61
65
|
],
|
|
62
66
|
)
|
|
63
67
|
def test_cache(self, key: dict[str, Any], tmp_path: Path) -> None:
|
|
64
68
|
"""
|
|
65
69
|
Test the basic properties of a cache.
|
|
66
70
|
"""
|
|
71
|
+
# Take a copy of the original key, to check that get/set
|
|
72
|
+
# doesn't mutate the key.
|
|
73
|
+
orig_key = copy.deepcopy(key)
|
|
74
|
+
|
|
67
75
|
with self.create_storage(tmp_path, data_type=int) as storage:
|
|
68
76
|
fib10 = 55
|
|
69
77
|
fib20 = 6765
|
|
@@ -72,10 +80,18 @@ class CacheStorageTestSuite(ABC):
|
|
|
72
80
|
with pytest.raises(KeyError):
|
|
73
81
|
storage.get(key)
|
|
74
82
|
|
|
83
|
+
assert key == orig_key, (
|
|
84
|
+
f"key changed after get: key={key!r}, orig_key={orig_key!r}"
|
|
85
|
+
)
|
|
86
|
+
|
|
75
87
|
# Store and retrieve a value
|
|
76
88
|
storage.set(key, value=fib10)
|
|
77
89
|
assert storage.get(key) == fib10
|
|
78
90
|
|
|
91
|
+
assert key == orig_key, (
|
|
92
|
+
f"key changed after set: key={key!r}, orig_key={orig_key!r}"
|
|
93
|
+
)
|
|
94
|
+
|
|
79
95
|
# Overwriting an existing value is an error.
|
|
80
96
|
with pytest.raises(CannotOverwriteValueException) as exc:
|
|
81
97
|
storage.set(key, value=fib20)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|