funkycache 1.0.0__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.0/src/funkycache.egg-info → funkycache-1.0.2}/PKG-INFO +1 -1
- {funkycache-1.0.0 → funkycache-1.0.2}/pyproject.toml +1 -0
- {funkycache-1.0.0 → funkycache-1.0.2}/src/funkycache/storage.py +62 -17
- {funkycache-1.0.0 → funkycache-1.0.2}/src/funkycache/version.py +1 -1
- {funkycache-1.0.0 → funkycache-1.0.2/src/funkycache.egg-info}/PKG-INFO +1 -1
- {funkycache-1.0.0 → 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.0 → funkycache-1.0.2}/tests/test_storage.py +80 -3
- {funkycache-1.0.0 → funkycache-1.0.2}/LICENSE +0 -0
- {funkycache-1.0.0 → funkycache-1.0.2}/README.md +0 -0
- {funkycache-1.0.0 → funkycache-1.0.2}/setup.cfg +0 -0
- {funkycache-1.0.0 → funkycache-1.0.2}/src/funkycache/__init__.py +0 -0
- {funkycache-1.0.0 → funkycache-1.0.2}/src/funkycache/manager.py +0 -0
- {funkycache-1.0.0 → funkycache-1.0.2}/src/funkycache/py.typed +0 -0
- {funkycache-1.0.0 → funkycache-1.0.2}/src/funkycache.egg-info/dependency_links.txt +0 -0
- {funkycache-1.0.0 → funkycache-1.0.2}/src/funkycache.egg-info/requires.txt +0 -0
- {funkycache-1.0.0 → funkycache-1.0.2}/src/funkycache.egg-info/top_level.txt +0 -0
- {funkycache-1.0.0 → funkycache-1.0.2}/tests/test_manager.py +0 -0
|
@@ -6,7 +6,7 @@ a collection of arguments to the return value.
|
|
|
6
6
|
"""
|
|
7
7
|
|
|
8
8
|
from abc import ABC, abstractmethod
|
|
9
|
-
|
|
9
|
+
import dataclasses
|
|
10
10
|
from datetime import datetime, timezone
|
|
11
11
|
from functools import cache
|
|
12
12
|
import json
|
|
@@ -15,7 +15,7 @@ import sqlite3
|
|
|
15
15
|
from types import TracebackType
|
|
16
16
|
from typing import Any, cast, Self
|
|
17
17
|
|
|
18
|
-
from pydantic import TypeAdapter
|
|
18
|
+
from pydantic import BaseModel, TypeAdapter
|
|
19
19
|
import xxhash
|
|
20
20
|
|
|
21
21
|
from .version import __version__
|
|
@@ -34,7 +34,7 @@ class CannotOverwriteValueException(ValueError):
|
|
|
34
34
|
cached function, and cannot be fixed by retrying.
|
|
35
35
|
"""
|
|
36
36
|
|
|
37
|
-
def __init__(self, key:
|
|
37
|
+
def __init__(self, key: dict[str, Any]) -> None:
|
|
38
38
|
super().__init__(f"Key already exists: {key!r}")
|
|
39
39
|
|
|
40
40
|
|
|
@@ -45,7 +45,7 @@ class CacheStorage[V](ABC):
|
|
|
45
45
|
"""
|
|
46
46
|
|
|
47
47
|
@abstractmethod
|
|
48
|
-
def set(self, key:
|
|
48
|
+
def set(self, key: dict[str, Any], value: V) -> None:
|
|
49
49
|
"""
|
|
50
50
|
Save a key-value pair in the cache.
|
|
51
51
|
|
|
@@ -54,7 +54,7 @@ class CacheStorage[V](ABC):
|
|
|
54
54
|
"""
|
|
55
55
|
|
|
56
56
|
@abstractmethod
|
|
57
|
-
def get(self, key:
|
|
57
|
+
def get(self, key: dict[str, Any]) -> V:
|
|
58
58
|
"""
|
|
59
59
|
Look up the value of a key in the cache.
|
|
60
60
|
|
|
@@ -73,7 +73,7 @@ class MemoryCacheStorage[V](CacheStorage[V]):
|
|
|
73
73
|
def __init__(self):
|
|
74
74
|
self._underlying: dict[str, V] = {}
|
|
75
75
|
|
|
76
|
-
def set(self, key:
|
|
76
|
+
def set(self, key: dict[str, Any], value: V) -> None:
|
|
77
77
|
"""
|
|
78
78
|
Save a key-value pair in the cache.
|
|
79
79
|
"""
|
|
@@ -85,7 +85,7 @@ class MemoryCacheStorage[V](CacheStorage[V]):
|
|
|
85
85
|
except KeyError:
|
|
86
86
|
self._underlying[enc_key] = value
|
|
87
87
|
|
|
88
|
-
def get(self, key:
|
|
88
|
+
def get(self, key: dict[str, Any]) -> V:
|
|
89
89
|
"""
|
|
90
90
|
Look up the value of a key in the cache.
|
|
91
91
|
"""
|
|
@@ -105,7 +105,7 @@ class TypedCacheStorage[V](CacheStorage[V]):
|
|
|
105
105
|
self.adapter = TypeAdapter(data_type)
|
|
106
106
|
|
|
107
107
|
@abstractmethod
|
|
108
|
-
def _set_str(self, key:
|
|
108
|
+
def _set_str(self, key: dict[str, Any], value: str) -> None:
|
|
109
109
|
"""
|
|
110
110
|
Save a key-value pair in the cache, where the value is a string.
|
|
111
111
|
|
|
@@ -113,7 +113,7 @@ class TypedCacheStorage[V](CacheStorage[V]):
|
|
|
113
113
|
a different value.
|
|
114
114
|
"""
|
|
115
115
|
|
|
116
|
-
def set(self, key:
|
|
116
|
+
def set(self, key: dict[str, Any], value: V) -> None:
|
|
117
117
|
"""
|
|
118
118
|
Save a key-value pair in the cache.
|
|
119
119
|
|
|
@@ -124,14 +124,14 @@ class TypedCacheStorage[V](CacheStorage[V]):
|
|
|
124
124
|
self._set_str(key, json_str)
|
|
125
125
|
|
|
126
126
|
@abstractmethod
|
|
127
|
-
def _get_str(self, key:
|
|
127
|
+
def _get_str(self, key: dict[str, Any]) -> str:
|
|
128
128
|
"""
|
|
129
129
|
Look up the string value of a key in the cache.
|
|
130
130
|
|
|
131
131
|
Throws a KeyError if you look up a key which isn't in the cache.
|
|
132
132
|
"""
|
|
133
133
|
|
|
134
|
-
def get(self, key:
|
|
134
|
+
def get(self, key: dict[str, Any]) -> V:
|
|
135
135
|
"""
|
|
136
136
|
Look up the value of a key in the cache.
|
|
137
137
|
|
|
@@ -245,7 +245,7 @@ class SQLiteCacheStorage[V](TypedCacheStorage[V]):
|
|
|
245
245
|
"""
|
|
246
246
|
self.close()
|
|
247
247
|
|
|
248
|
-
def _encode_key(self, key:
|
|
248
|
+
def _encode_key(self, key: dict[str, Any]) -> str:
|
|
249
249
|
"""
|
|
250
250
|
Encode a mapping as a string key.
|
|
251
251
|
|
|
@@ -256,7 +256,7 @@ class SQLiteCacheStorage[V](TypedCacheStorage[V]):
|
|
|
256
256
|
json_key = _encode_key_as_json(key)
|
|
257
257
|
return _hash(json_key)
|
|
258
258
|
|
|
259
|
-
def _set_str(self, key:
|
|
259
|
+
def _set_str(self, key: dict[str, Any], value: str) -> None:
|
|
260
260
|
"""
|
|
261
261
|
Save a key-value pair in the cache, where the value is a string.
|
|
262
262
|
|
|
@@ -280,7 +280,7 @@ class SQLiteCacheStorage[V](TypedCacheStorage[V]):
|
|
|
280
280
|
else:
|
|
281
281
|
raise
|
|
282
282
|
|
|
283
|
-
def _get_str(self, key:
|
|
283
|
+
def _get_str(self, key: dict[str, Any]) -> str:
|
|
284
284
|
"""
|
|
285
285
|
Look up the string value of a key in the cache.
|
|
286
286
|
|
|
@@ -311,16 +311,61 @@ def _make_json_compatible(value: Any) -> Any:
|
|
|
311
311
|
return value
|
|
312
312
|
|
|
313
313
|
|
|
314
|
-
|
|
314
|
+
@cache
|
|
315
|
+
def _get_type_adapter(target_type: Any) -> TypeAdapter:
|
|
316
|
+
"""
|
|
317
|
+
Return a TypeAdapter for the given Pydantic type.
|
|
318
|
+
|
|
319
|
+
The type adapter is cached, so schema generation only runs once
|
|
320
|
+
per unique type.
|
|
321
|
+
"""
|
|
322
|
+
return TypeAdapter(target_type)
|
|
323
|
+
|
|
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
|
+
|
|
333
|
+
def _encode_key_as_json(key: dict[str, Any]) -> str:
|
|
315
334
|
"""
|
|
316
335
|
Encode a set of arguments to a Python function as a JSON string.
|
|
317
336
|
|
|
318
337
|
This is a lossy transformation that is designed to get a reasonable
|
|
319
338
|
serialisation as JSON. It is possible for values of different types
|
|
320
339
|
to collide.
|
|
340
|
+
|
|
341
|
+
This function must not mutate its input.
|
|
321
342
|
"""
|
|
322
|
-
|
|
323
|
-
|
|
343
|
+
overrides: dict[str, Any] = {}
|
|
344
|
+
|
|
345
|
+
for name, val in key.items():
|
|
346
|
+
if _should_hash(val):
|
|
347
|
+
overrides[name] = _hash(val)
|
|
348
|
+
|
|
349
|
+
elif isinstance(val, dict):
|
|
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
|
+
}
|
|
354
|
+
|
|
355
|
+
elif isinstance(val, BaseModel) or dataclasses.is_dataclass(val):
|
|
356
|
+
adapter = _get_type_adapter(type(val))
|
|
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}
|
|
363
|
+
|
|
364
|
+
try:
|
|
365
|
+
return json.dumps(reduced_key, sort_keys=True, default=str)
|
|
366
|
+
except TypeError:
|
|
367
|
+
clean_key = _make_json_compatible(reduced_key)
|
|
368
|
+
return json.dumps(clean_key, sort_keys=True, default=str)
|
|
324
369
|
|
|
325
370
|
|
|
326
371
|
@cache
|
|
@@ -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)
|
|
@@ -3,20 +3,25 @@ Tests for backend storage implementations.
|
|
|
3
3
|
"""
|
|
4
4
|
|
|
5
5
|
from abc import ABC, abstractmethod
|
|
6
|
-
from collections.abc import Generator
|
|
6
|
+
from collections.abc import Generator
|
|
7
7
|
from contextlib import closing, contextmanager
|
|
8
|
+
import copy
|
|
9
|
+
from dataclasses import dataclass
|
|
8
10
|
from pathlib import Path
|
|
9
11
|
import sqlite3
|
|
10
12
|
from typing import Any
|
|
11
13
|
from unittest.mock import MagicMock
|
|
12
14
|
|
|
15
|
+
from pydantic import BaseModel
|
|
13
16
|
import pytest
|
|
17
|
+
from _pytest.mark.structures import ParameterSet
|
|
14
18
|
|
|
15
19
|
from funkycache.storage import (
|
|
16
20
|
CacheStorage,
|
|
17
21
|
MemoryCacheStorage,
|
|
18
22
|
SQLiteCacheStorage,
|
|
19
23
|
CannotOverwriteValueException,
|
|
24
|
+
_encode_key_as_json,
|
|
20
25
|
)
|
|
21
26
|
|
|
22
27
|
|
|
@@ -50,14 +55,23 @@ class CacheStorageTestSuite(ABC):
|
|
|
50
55
|
# A dict with Path keys isn't serialisable by json.dumps(…),
|
|
51
56
|
# even if you pass default=str.
|
|
52
57
|
pytest.param({Path("n"): "numbers"}, id="path_key"),
|
|
53
|
-
pytest.param(
|
|
58
|
+
pytest.param(
|
|
59
|
+
{"args": [{Path("n"): "numbers"}]}, id="path_key_nested_in_list"
|
|
60
|
+
),
|
|
54
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"),
|
|
55
65
|
],
|
|
56
66
|
)
|
|
57
|
-
def test_cache(self, key:
|
|
67
|
+
def test_cache(self, key: dict[str, Any], tmp_path: Path) -> None:
|
|
58
68
|
"""
|
|
59
69
|
Test the basic properties of a cache.
|
|
60
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
|
+
|
|
61
75
|
with self.create_storage(tmp_path, data_type=int) as storage:
|
|
62
76
|
fib10 = 55
|
|
63
77
|
fib20 = 6765
|
|
@@ -66,10 +80,18 @@ class CacheStorageTestSuite(ABC):
|
|
|
66
80
|
with pytest.raises(KeyError):
|
|
67
81
|
storage.get(key)
|
|
68
82
|
|
|
83
|
+
assert key == orig_key, (
|
|
84
|
+
f"key changed after get: key={key!r}, orig_key={orig_key!r}"
|
|
85
|
+
)
|
|
86
|
+
|
|
69
87
|
# Store and retrieve a value
|
|
70
88
|
storage.set(key, value=fib10)
|
|
71
89
|
assert storage.get(key) == fib10
|
|
72
90
|
|
|
91
|
+
assert key == orig_key, (
|
|
92
|
+
f"key changed after set: key={key!r}, orig_key={orig_key!r}"
|
|
93
|
+
)
|
|
94
|
+
|
|
73
95
|
# Overwriting an existing value is an error.
|
|
74
96
|
with pytest.raises(CannotOverwriteValueException) as exc:
|
|
75
97
|
storage.set(key, value=fib20)
|
|
@@ -224,3 +246,58 @@ class TestSQLiteCacheStorage(CacheStorageTestSuite):
|
|
|
224
246
|
|
|
225
247
|
with pytest.raises(sqlite3.OperationalError, match="no such column: key"):
|
|
226
248
|
SQLiteCacheStorage[int](db_path, data_type="int")
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
class Shape(BaseModel):
|
|
252
|
+
"""A basic model that uses Pydantic."""
|
|
253
|
+
|
|
254
|
+
sides: int
|
|
255
|
+
colour: str
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
@dataclass
|
|
259
|
+
class Greeting:
|
|
260
|
+
"""A basic model that uses dataclasses."""
|
|
261
|
+
|
|
262
|
+
name: str
|
|
263
|
+
message: str
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
KEY_ENCODING_TEST_CASES: list[ParameterSet] = [
|
|
267
|
+
pytest.param({"n": "one"}, '{"n": "one"}', id="str"),
|
|
268
|
+
pytest.param({"n": 1}, '{"n": 1}', id="int"),
|
|
269
|
+
pytest.param(
|
|
270
|
+
{"n": Shape(sides=4, colour="red")},
|
|
271
|
+
'{"n": {"colour": "red", "sides": 4}}',
|
|
272
|
+
id="pydantic",
|
|
273
|
+
),
|
|
274
|
+
pytest.param(
|
|
275
|
+
{"n": Greeting(name="alex", message="hello")},
|
|
276
|
+
'{"n": {"message": "hello", "name": "alex"}}',
|
|
277
|
+
id="dataclass",
|
|
278
|
+
),
|
|
279
|
+
pytest.param(
|
|
280
|
+
{"long": "a" * 5000, "short": "a"},
|
|
281
|
+
'{"long": "xxh64:b2b67943d4803cd6", "short": "a"}',
|
|
282
|
+
id="long_str",
|
|
283
|
+
),
|
|
284
|
+
pytest.param(
|
|
285
|
+
{"n": {"long": "a" * 5000, "short": "a"}},
|
|
286
|
+
'{"n": {"long": "xxh64:b2b67943d4803cd6", "short": "a"}}',
|
|
287
|
+
id="long_str",
|
|
288
|
+
),
|
|
289
|
+
pytest.param(
|
|
290
|
+
{"n": {Path("1"): "one"}}, '{"n": {"1": "one"}}', id="nested_path_key"
|
|
291
|
+
),
|
|
292
|
+
]
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
@pytest.mark.parametrize(
|
|
296
|
+
"key, json_key",
|
|
297
|
+
KEY_ENCODING_TEST_CASES,
|
|
298
|
+
)
|
|
299
|
+
def test_encode_key_as_json(key: dict[str, Any], json_key: str) -> None:
|
|
300
|
+
"""
|
|
301
|
+
Tests for _encode_key_as_json.
|
|
302
|
+
"""
|
|
303
|
+
assert _encode_key_as_json(key) == json_key
|
|
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
|